Manage Ruby on Rails dependencies in Replit by configuring Ruby and Bundler through the replit.nix file for system-level packages, editing the Gemfile for Rails gems, and running bundle install from the Shell. Configure the .replit file with the correct run command and port binding to 0.0.0.0 for the Rails server. Replit's Nix environment gives you full control over Ruby versions and native dependencies.
Set Up and Manage Rails Gem Dependencies on Replit
Ruby on Rails applications depend on a rich ecosystem of gems managed by Bundler. Running Rails on Replit requires configuring both the Nix system environment for Ruby and native libraries, and the Gemfile for application-level gems. This tutorial walks you through setting up a complete Rails development environment on Replit, managing gems with Bundler, handling native extensions that require system libraries, and configuring the .replit file for correct server startup.
Prerequisites
- A Replit account (Core or Pro recommended for RAM)
- A Ruby on Rails Repl or a blank Repl you will configure for Rails
- Basic familiarity with Ruby syntax and Gemfile format
- Understanding of Replit's Shell tab and file tree
Step-by-step guide
Configure replit.nix with Ruby and system dependencies
Configure replit.nix with Ruby and system dependencies
Replit uses Nix to manage system-level packages including the Ruby interpreter itself. Open the file tree, enable Show hidden files from the three-dot menu, and edit the replit.nix file. Add the Ruby package, Bundler, and any system libraries your gems need (such as PostgreSQL client libraries for the pg gem, or ImageMagick for image processing). After editing replit.nix, type exit in the Shell to restart the environment and load the new packages. Search for available packages at search.nixos.org/packages if you need additional system dependencies.
1# replit.nix2{ pkgs }: {3 deps = [4 pkgs.ruby_3_25 pkgs.bundler6 pkgs.postgresql7 pkgs.libffi8 pkgs.zlib9 pkgs.openssl10 pkgs.libyaml11 pkgs.pkg-config12 pkgs.nodejs-20_x13 ];14}Expected result: After typing exit in the Shell and reopening it, running ruby --version shows the installed Ruby version and bundle --version shows Bundler is available.
Create and configure the Gemfile
Create and configure the Gemfile
The Gemfile lists all Ruby gems your Rails application depends on. If you are starting from scratch, create a Gemfile in your project root. If you imported an existing Rails project, the Gemfile already exists. Add your gems with version constraints. Use pessimistic version constraints (~>) for most gems to allow minor updates while preventing breaking major changes. After editing the Gemfile, run bundle install in the Shell to download and install all listed gems. Bundler creates a Gemfile.lock that pins exact versions for reproducibility.
1# Gemfile2source 'https://rubygems.org'34ruby '3.2.2'56gem 'rails', '~> 7.1'7gem 'pg', '~> 1.5'8gem 'puma', '~> 6.0'9gem 'jbuilder', '~> 2.11'10gem 'bcrypt', '~> 3.1'11gem 'bootsnap', require: false1213group :development, :test do14 gem 'debug', platforms: [:mri]15 gem 'rspec-rails', '~> 6.0'16end1718group :development do19 gem 'web-console'20endExpected result: Running bundle install in the Shell downloads all gems and their dependencies. The output shows 'Bundle complete!' with the number of gems installed.
Handle gems with native extensions
Handle gems with native extensions
Some gems like pg (PostgreSQL), nokogiri (XML parsing), and mini_magick (image processing) require native C libraries to compile their extensions. When bundle install fails with an error about missing headers or libraries, you need to add the corresponding Nix package to replit.nix. The error message usually tells you which library is missing. Common mappings: pg needs pkgs.postgresql, nokogiri needs pkgs.libxml2 and pkgs.libxslt, mini_magick needs pkgs.imagemagick, and bcrypt needs pkgs.libffi. After adding the Nix package, type exit in the Shell, reopen it, and run bundle install again.
1# If you see: "Can't find the 'libpq-fe.h header"2# Add to replit.nix: pkgs.postgresql34# If you see: "Could not find libxml2"5# Add to replit.nix: pkgs.libxml2 and pkgs.libxslt67# If bundle install still fails after adding Nix packages,8# try configuring the build with bundle config:9bundle config build.pg --with-pg-config=$(which pg_config)10bundle config build.nokogiri --use-system-libraries1112# Then retry:13bundle installExpected result: Native gems compile successfully after adding the required system libraries to replit.nix. bundle install completes without compilation errors.
Configure the .replit file for Rails server
Configure the .replit file for Rails server
The .replit file tells Replit how to run your application. For Rails, set the run command to start the Puma server bound to 0.0.0.0 on port 3000. Binding to 0.0.0.0 is critical because Replit's preview panel and deployments cannot reach a server bound to localhost or 127.0.0.1. Configure the port mapping so Replit's external port 80 maps to your local port 3000. Also set the deployment commands for production builds.
1# .replit2entrypoint = "config/routes.rb"3run = "bundle exec rails server -b 0.0.0.0 -p 3000"45[nix]6channel = "stable-23_11"78[[ports]]9localPort = 300010externalPort = 801112[deployment]13build = "bundle install && bundle exec rails assets:precompile"14run = ["bundle", "exec", "rails", "server", "-b", "0.0.0.0", "-p", "3000", "-e", "production"]Expected result: Clicking the Run button starts the Rails server on port 3000. The Replit Preview panel loads your Rails application. Deployment commands build assets and start the production server.
Update gems and manage versions over time
Update gems and manage versions over time
Keep your gems current by periodically running bundle outdated in the Shell to see which gems have newer versions available. Update specific gems with bundle update gem-name or update all gems with bundle update (use with caution). For major Rails version upgrades, follow the official Rails upgrade guide and update one major version at a time. Always test your application after gem updates by running your test suite with bundle exec rspec or bundle exec rails test, and check the Console for deprecation warnings.
1# Check for outdated gems2bundle outdated34# Update a specific gem5bundle update puma67# Update all gems (use carefully)8bundle update910# Run tests after updating11bundle exec rspec1213# Check for security vulnerabilities14bundle audit check --updateExpected result: bundle outdated shows gems with available updates. After updating, bundle exec rspec confirms all tests still pass. The Gemfile.lock reflects the new gem versions.
Complete working example
1# Replit configuration for Ruby on Rails2entrypoint = "config/routes.rb"3run = "bundle exec rails server -b 0.0.0.0 -p 3000"45[nix]6channel = "stable-23_11"7packages = ["ruby_3_2", "bundler", "postgresql", "nodejs-20_x", "yarn", "libffi", "zlib", "openssl", "libyaml", "pkg-config"]89hidden = [".config", "vendor", "tmp", "log"]1011[[ports]]12localPort = 300013externalPort = 801415[run.env]16RAILS_ENV = "development"1718[deployment]19build = "bundle install --without development test && bundle exec rails assets:precompile && bundle exec rails db:migrate"20run = ["bundle", "exec", "rails", "server", "-b", "0.0.0.0", "-p", "3000", "-e", "production"]2122# Database URL and SECRET_KEY_BASE must be set in23# Tools > Secrets for deployment to work.24# Never hardcode these values in the .replit file.Common mistakes when managing Rails dependencies in Replit
Why it's a problem: Running rails server without -b 0.0.0.0, causing the Preview panel to show a blank page or connection refused error
How to avoid: Always include -b 0.0.0.0 in the run command: bundle exec rails server -b 0.0.0.0 -p 3000. This binds the server to all network interfaces.
Why it's a problem: Not adding system libraries to replit.nix before running bundle install, causing native gem compilation to fail with missing header errors
How to avoid: Read the error message to identify the missing library, add the corresponding Nix package to replit.nix, type exit in Shell to reload the environment, then run bundle install again.
Why it's a problem: Forgetting to set deployment secrets separately from workspace secrets, causing Rails to fail with 'Missing secret_key_base' in production
How to avoid: Add SECRET_KEY_BASE and DATABASE_URL in both the workspace Secrets and the Deployment pane secrets. Workspace secrets do not automatically carry to deployments.
Why it's a problem: Running gem install instead of adding gems to the Gemfile and running bundle install, causing version conflicts and unreproducible environments
How to avoid: Always add gems to the Gemfile with a version constraint and use bundle install. This ensures Bundler manages versions consistently and creates an accurate Gemfile.lock.
Why it's a problem: Using an outdated Nix channel that does not have the Ruby version needed, leading to installation failures
How to avoid: Update the channel in .replit to a recent stable version like stable-23_11 or newer. Check search.nixos.org for available Ruby versions in each channel.
Best practices
- Always configure replit.nix with the required system libraries before running bundle install to avoid native extension compilation failures
- Bind the Rails server to 0.0.0.0 instead of localhost so Replit's Preview panel and deployments can access it
- Commit both Gemfile and Gemfile.lock to Git for reproducible gem installations across environments
- Use pessimistic version constraints (~>) in the Gemfile to allow minor updates while preventing breaking major changes
- Store DATABASE_URL, SECRET_KEY_BASE, and RAILS_MASTER_KEY in Replit Secrets (Tools > Secrets), never in code
- Run bundle exec before Rails commands to ensure you use the exact gem versions specified in your Gemfile.lock
- Add the vendor/bundle directory to .gitignore since gems should be installed fresh from the Gemfile, not committed
- Use bundle audit regularly to check your dependencies for known security vulnerabilities
Still stuck?
Copy one of these prompts to get a personalized, step-by-step explanation.
I need to set up Ruby on Rails on Replit with PostgreSQL. Show me the replit.nix configuration with all required system dependencies, a basic Gemfile, and the .replit file with correct run command and port binding for the Rails server.
Set up this project for Ruby on Rails 7.1 with PostgreSQL. Configure replit.nix with Ruby 3.2 and all required system libraries, create a Gemfile with standard Rails gems, and set up the .replit file to run the Rails server on 0.0.0.0:3000 with proper deployment commands.
Frequently asked questions
Technically yes, but the Starter plan's 2 GiB RAM is tight for Rails applications. Rails itself uses significant memory, and adding gems like Devise or Sidekiq can push you past the limit. Core or Pro plans with 8 GiB RAM are recommended.
Replit provides a built-in PostgreSQL database. Add it from the Cloud tab (click + next to Preview). The DATABASE_URL environment variable is automatically created. Add gem 'pg' to your Gemfile, run bundle install, and configure config/database.yml to use ENV['DATABASE_URL'].
The gem requires system libraries that are not installed in the Nix environment. Read the error output to identify the missing library (e.g., libpq-fe.h for pg, libxml2 for nokogiri), add the corresponding package to replit.nix, exit and reopen the Shell, then run bundle install again.
Run bundle exec rails db:migrate in the Shell tab. For deployment, add it to your deployment build command in .replit: build = 'bundle install && bundle exec rails db:migrate && bundle exec rails assets:precompile'.
Replit Agent supports Rails, though React + Tailwind + ShadCN UI is the best-supported stack. Agent can scaffold Rails projects, generate models and controllers, and configure database connections. For complex Rails configurations, RapidDev's engineering team provides hands-on development support.
Open the Shell tab and run bundle exec rails console. This gives you an interactive Ruby session with your Rails application loaded, useful for testing queries, debugging data, and running one-off scripts.
Use Replit Secrets (Tools > Secrets in the left sidebar). Add SECRET_KEY_BASE, DATABASE_URL, and any API keys there. Access them in Rails code with ENV['SECRET_KEY_BASE']. Never put secrets in config files or code.
Set the Ruby version in replit.nix using the appropriate Nix package name. For example, pkgs.ruby_3_2 for Ruby 3.2.x. Check search.nixos.org/packages to find available Ruby version packages for your selected Nix channel.
Talk to an Expert
Our team has built 600+ apps. Get personalized help with your project.
Book a free consultation