explaingit

bkeepers/dotenv

6,746RubyAudience · developerComplexity · 1/5LicenseSetup · easy

TLDR

A Ruby gem that loads environment variables from a .env file at app startup, so secrets like API keys and passwords stay out of your code and version control.

Mindmap

mindmap
  root((dotenv))
    What it does
      Loads env vars
      From .env file
    File hierarchy
      .env shared defaults
      .env.development
      .env.local overrides
    Features
      Variable interpolation
      Shell command output
      Multi-line values
      Required var checks
    Framework support
      Rails auto-load
      Sinatra support
      Plain Ruby
    Testing
      Auto env restore
      Per-test isolation
Click or tap to explore — scroll the page freely

Code map

Detail Auto

An interactive map of this repo's files and how they connect — its source is parsed live in your browser. Click Visualize to build it.

filefunction / class

Things people build with this

USE CASE 1

Keep database passwords and API keys out of your Rails codebase by storing them in a .env file that is excluded from git.

USE CASE 2

Use separate .env.development and .env.production files so the app picks up the right settings in each environment automatically.

USE CASE 3

Add dotenv to your test suite so environment variable changes made during a test are rolled back after it finishes, preventing test pollution.

USE CASE 4

Load a .env file before running a command-line script without modifying the script itself, using the included CLI tool.

Tech stack

RubyRailsSinatra

Getting it running

Difficulty · easy Time to first run · 5min
Use freely for any purpose including commercial use, keep the copyright notice.

In plain English

Dotenv is a Ruby gem that loads configuration values from a plain text file called .env into your application's environment when it starts up. The problem it solves is common: you have settings like database passwords, API keys, or storage bucket names that change between your laptop, your test server, and your production server. Putting those values in a .env file and keeping that file off version control means each environment can have its own settings without touching the actual code. In a Rails application, dotenv loads automatically when the app boots and follows a specific priority order across multiple files. A file like .env.development.local takes precedence over .env.development, which takes precedence over the shared .env file. Local override files are meant to stay out of source control, while the shared environment files can be committed to the repository as safe defaults. The gem also supports a few convenient features in the .env file itself. You can reference other variables using $VAR syntax, substitute in the output of a shell command using $(command), write multi-line values with double quotes, and add comments with #. If a required variable is missing, you can configure dotenv to raise an error rather than silently continuing. For testing, dotenv version 3 added automatic environment restoration: any changes made to environment variables during a test are rolled back after that test finishes, preventing one test from affecting another. Outside of Rails, it works with Sinatra and plain Ruby apps by requiring the library early in the startup process. A command-line tool is also included so you can load a .env file before running any script without modifying the script itself.

Copy-paste prompts

Prompt 1
I'm adding dotenv to a Rails app. Show me the correct load order for .env.env.development, and .env.development.local so I know which file wins when they conflict.
Prompt 2
My Rails app uses dotenv and I need it to raise an error at boot if a required environment variable is missing. How do I configure dotenv to enforce required variables?
Prompt 3
In my RSpec tests with dotenv, I want env var changes inside one test to not affect other tests. How does dotenv 3 handle this and what do I need to enable?
Prompt 4
Help me write a .env file that references other variables using $VAR syntax and includes a multi-line value, show me the correct dotenv syntax for both.
Open on GitHub → Explain another repo

← bkeepers on gitmyhub — every repo by this author, as a profile.

Verify against the repo before relying on details.