.env · generator · Ruby on Rails

Ruby on Rails .env Generator

Rails ships with encrypted credentials, but dotenv-rails is still the path of least resistance for local dev. This generator produces a .env with database, Redis, Sidekiq, and secret keys.

.env · Ruby on Rails · 5 keys
Customize →
RAILS_ENV=development
RAILS_MASTER_KEY=7fa65557155fec8d1d6a36c121460e39
DATABASE_URL=postgres://user:password@localhost:5432/mydb
REDIS_URL=redis://localhost:6379/0
SECRET_KEY_BASE=5c9d8b935de3b0a06405829016338be0f44205f70406d5f4d41dff933f77b977447df3d61b8dc68ce031da37811829e4dd28eedbd52e5a16d690331f1a342425

Secrets regenerate on every page load. Want to combine stacks? Use the full generator.

How to use this .env in Ruby on Rails

  1. 1.Add gem 'dotenv-rails' to the :development, :test group in Gemfile.
  2. 2.Run bundle install.
  3. 3.Save the output as .env in the project root.
  4. 4.Access with ENV['KEY'] anywhere in the app.
  5. 5.For production, prefer Rails encrypted credentials (rails credentials:edit).

Gotchas

  • ⚠dotenv-rails loads AFTER boot — use config/application.rb for config-time values via credentials instead.
  • ⚠Don't commit config/master.key. It's auto-added to .gitignore on new apps.
  • ⚠RAILS_LOG_TO_STDOUT=1 is needed for Heroku/Fly to see logs.

Common keys explained

RAILS_ENV

Environment — development | test | production.

DATABASE_URL

Postgres connection string (Rails 7 reads this natively).

RAILS_MASTER_KEY

Key that decrypts config/credentials.yml.enc.

REDIS_URL

Redis used by Sidekiq + Action Cable.

SECRET_KEY_BASE

Fallback signing key; typically set via credentials.

Loading and validating the .env in Ruby on Rails

Gemfile + config/database.yml
# Gemfile — load .env only in development and test
gem "dotenv-rails", groups: [:development, :test]

# config/database.yml — DATABASE_URL overrides the block below
default: &default
  adapter: postgresql
  url: <%= ENV["DATABASE_URL"] %>
  pool: <%= ENV.fetch("RAILS_MAX_THREADS", 5) %>

# Anywhere in app code: fail loudly if a key is missing
ENV.fetch("REDIS_URL")   # KeyError if unset, unlike ENV["REDIS_URL"] => nil

Ruby on Rails: env vars in development, staging and production

Rails has two places for configuration. .env (via dotenv-rails) is for development values. Encrypted credentials (config/credentials.yml.enc) are for secrets you want in the repo. RAILS_MASTER_KEY decrypts the credentials file. It is the one value that must reach production as a real environment variable.

In production, Rails reads SECRET_KEY_BASE from the environment first and from credentials second. Rails 7.1+ can derive it in development, but production needs a real 128-character hex value. Generate it with bin/rails secret or the generator above.

Ruby on Rails: .env errors and fixes

ActiveSupport::MessageEncryptor::InvalidMessage

RAILS_MASTER_KEY does not match config/master.key used to encrypt the credentials. Copy the exact contents of config/master.key into the host's environment.

Missing secret_key_base for 'production' environment

Set SECRET_KEY_BASE in the production environment, or add secret_key_base to the production credentials.

ENV values are nil in rails console on the server

dotenv-rails is limited to development/test in the Gemfile, as it should be. Set real environment variables on the server instead of copying the .env.

Ruby on Rails .env FAQ

Should I use dotenv or Rails credentials?

Both. Credentials for secrets that must stay with the code (API keys shared by the team), .env for per-developer settings. In production, prefer real environment variables from the host.

Is RAILS_MASTER_KEY the same as SECRET_KEY_BASE?

No. RAILS_MASTER_KEY decrypts credentials.yml.enc. SECRET_KEY_BASE signs cookies and sessions. They are different values with different jobs.

Where does Sidekiq get REDIS_URL?

Sidekiq reads REDIS_URL (or REDIS_PROVIDER) automatically. Set it in .env locally and as an environment variable on the worker process in production: the worker needs the same env as the web process.

Related tools

Other stacks