Skip to main content
RapidDev - Software Development Agency
replit-tutorial

How to manage Rails dependencies in Replit

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.

What you'll learn

  • Configure replit.nix with Ruby and system dependencies for Rails
  • Manage gems using Gemfile and Bundler from the Replit Shell
  • Handle native extensions that require system libraries via Nix packages
  • Set up the .replit file for Rails server startup with proper port binding
Book a free consultation
4.9Clutch rating
600+Happy partners
17+Countries served
190+Team members
Beginner9 min read20-30 minutesReplit Core or Pro plan recommended for sufficient RAM (8 GiB). Starter plan (2 GiB) may struggle with larger Rails apps.March 2026RapidDev Engineering Team
TL;DR

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

1

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.

typescript
1# replit.nix
2{ pkgs }: {
3 deps = [
4 pkgs.ruby_3_2
5 pkgs.bundler
6 pkgs.postgresql
7 pkgs.libffi
8 pkgs.zlib
9 pkgs.openssl
10 pkgs.libyaml
11 pkgs.pkg-config
12 pkgs.nodejs-20_x
13 ];
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.

2

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.

typescript
1# Gemfile
2source 'https://rubygems.org'
3
4ruby '3.2.2'
5
6gem 'rails', '~> 7.1'
7gem 'pg', '~> 1.5'
8gem 'puma', '~> 6.0'
9gem 'jbuilder', '~> 2.11'
10gem 'bcrypt', '~> 3.1'
11gem 'bootsnap', require: false
12
13group :development, :test do
14 gem 'debug', platforms: [:mri]
15 gem 'rspec-rails', '~> 6.0'
16end
17
18group :development do
19 gem 'web-console'
20end

Expected result: Running bundle install in the Shell downloads all gems and their dependencies. The output shows 'Bundle complete!' with the number of gems installed.

3

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.

typescript
1# If you see: "Can't find the 'libpq-fe.h header"
2# Add to replit.nix: pkgs.postgresql
3
4# If you see: "Could not find libxml2"
5# Add to replit.nix: pkgs.libxml2 and pkgs.libxslt
6
7# 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-libraries
11
12# Then retry:
13bundle install

Expected result: Native gems compile successfully after adding the required system libraries to replit.nix. bundle install completes without compilation errors.

4

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.

typescript
1# .replit
2entrypoint = "config/routes.rb"
3run = "bundle exec rails server -b 0.0.0.0 -p 3000"
4
5[nix]
6channel = "stable-23_11"
7
8[[ports]]
9localPort = 3000
10externalPort = 80
11
12[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.

5

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.

typescript
1# Check for outdated gems
2bundle outdated
3
4# Update a specific gem
5bundle update puma
6
7# Update all gems (use carefully)
8bundle update
9
10# Run tests after updating
11bundle exec rspec
12
13# Check for security vulnerabilities
14bundle audit check --update

Expected 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

.replit
1# Replit configuration for Ruby on Rails
2entrypoint = "config/routes.rb"
3run = "bundle exec rails server -b 0.0.0.0 -p 3000"
4
5[nix]
6channel = "stable-23_11"
7packages = ["ruby_3_2", "bundler", "postgresql", "nodejs-20_x", "yarn", "libffi", "zlib", "openssl", "libyaml", "pkg-config"]
8
9hidden = [".config", "vendor", "tmp", "log"]
10
11[[ports]]
12localPort = 3000
13externalPort = 80
14
15[run.env]
16RAILS_ENV = "development"
17
18[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"]
21
22# Database URL and SECRET_KEY_BASE must be set in
23# 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.

ChatGPT Prompt

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.

Replit Prompt

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.

RapidDev

Talk to an Expert

Our team has built 600+ apps. Get personalized help with your project.

Book a free consultation

Need help with your project?

Our experts have built 600+ apps and can accelerate your development. Book a free consultation — no strings attached.

Book a free consultation

We put the rapid in RapidDev

Need a dedicated strategic tech and growth partner? Discover what RapidDev can do for your business! Book a call with our team to schedule a free, no-obligation consultation. We'll discuss your project and provide a custom quote at no cost.