The Rubyist's Guide to Environment Variables

www.honeybadger.io
7 min read
fairly easy
If you want to be able to effectively manage web apps in development and in production, you have to understand environment variables. This post will show you how environment variables really work - and perhaps more importantly, how they DON'T work. We'll also explore some of the most common ways ...
If you want to be able to effectively manage web apps in development and in production, you have to understand environment variables.

This wasn't always the case. Just a few years ago, hardly anyone was configuring their Rails apps with environment variables. But then Heroku happened.

Heroku introduced developers to the 12-factor app approach. In their 12-factor app manifesto they lay out a lot of their best practices for creating apps that are easy to deploy. The section on environment variables has been particularly influential.

The twelve-factor app stores config in environment variables (often shortened to env vars or env). Env vars are easy to change between deploys without changing any code; unlike config files, there is little chance of them being checked into the code repo accidentally; and unlike custom config files, or other config mechanisms such as Java System Properties, they are a language- and OS-agnostic standard.

More Rubyists are using environment variables than ever. But often it's in a cargo-culty way. We're using these things without really understanding how they work.

This post will show you how environment variables really work - and perhaps more importantly, how they DON'T work. We'll also explore some of the most common ways to manage environment variables in your Rails apps. Let's get started!

NOTE: You can read about securing environment variables here.

Every process has its own set of environment variables

Every program you run on your server has at least one process. That process gets its own set of environment variables. Once it has them, nothing outside of that process can change them.

An understandable mistake that beginners make is to think that environment variables are somehow server-wide. Services like Heroku sure make it seem like setting the environment variables is the equivalent of editing a config file on disk. But environment variables are nothing like config files.

Every program you run on your server gets its…
Read full article