opensoul.org

Load environment variables with dotenv

I released dotenv, a handy Ruby gem that makes loading environment variables 100% less painful in development, guaranteed or your money back.

Backstory

I love the emerging practice of storing application configuration in environment variables. It makes it ridiculously easy to keep secure data out of your code repository while still making it available at runtime. But it slightly complicates development. It’s not always practical to set all of those environment variables on your development machine or continuous integration server where you might run multiple projects.

Foreman provides this handy feature of loading variables from .env, which works great for anything that you want to put in your Procfile. But it doesn’t help you when you run your app manually, run a rake task, or open up the console. My co-workers got tired of me saying Works For Me™, so dotenv solves that problem.

Frontstory

For Rails 3 apps, dotenv will automagically load variables from .env into ENV whenever the environment is bootstrapped. For non-Rails apps, it’s a simple one-liner to load .env. Check out dotenv on GitHub for all the gory details.

gem July 24, 2012

8 Comments

  1. Raul Murciano Raul Murciano July 24, 2012

    Have you tried `foreman run`?

  2. Brandon Keepers Brandon Keepers July 24, 2012

    Raul: Yep. However, asking everyone on the development team to type bundle exec foreman run rails console is not exactly practical. This makes it just work without any other considerations.

  3. Raul Murciano Raul Murciano July 24, 2012

    Well, that’s why shell aliases are so useful, or do you really type `bundle exec` all the time? :)

    I mention `foreman run` because it actually helps “you when you run your app manually, run a rake task, or open up the console”.

    That said, it’s great to have different options out there, thanks!

  4. Brandon Keepers Brandon Keepers July 24, 2012

    Raul: Yep, I use aliases. But still, rails console should just work. It’s what everyone is used to and has caused our team enough pain that I put together this tiny gem.

    P.s. Sorry about the typo on your name on the first reply. Stinking OS X autocorrect. Fixed now.

  5. Nathan Colgate Nathan Colgate October 19, 2012

    Brandon: Superb gem. We use heroku and have been trying to get our dev environments to match production as much as possible. I fully agree with your “asking everyone on the development team to type bundle exec foreman run rails console is not exactly practical” premise.

    Internet high-five!

  6. Francesc Esplugas Francesc Esplugas November 16, 2012

    I tend to use `rbenv-vars` to load environment vars or foreman.

    This gem is a good attemp to make env files work with Rails application but you need to load those variables before any process starts, otherwise some variables are missing. See the issue I created at GitHub. https://github.com/bkeepers/dotenv/issues/10

  7. Cyril Mougel Cyril Mougel December 5, 2012

    Why don’t use simply `.pam_environement` file. This file is manage by your shell to set all variable you want. because we can’t use dot-env in our shell command :(

  8. Paul Paul May 16, 2013

    Downvote for using the word “automagically”.

Post a Comment

Comments use textile. Anonymous comments will be deleted.

I am Brandon Keepers. I build Internet things, usually with Ruby or JavaScript. I work at GitHub and live in Holland, MI.

Popular Posts