Loading .env in Ruby on Rails application

15,032

Solution 1

I recommend using https://github.com/bkeepers/dotenv gem if you want to load .env file with ENV variables. If you are already using it then according to dotenv readme you should load as soon as possible on boot, for example in config/application.rb add:

require 'dotenv'
Dotenv.load

If you use PUMA or Unicorn, in config/puma.rb, config/unicorn.rb add:

  require "dotenv"
  Dotenv.load("/path/to/env/.env")

Solution 2

.env file is meant for use in development and test environments. When using heroku you are talking about production environment. You don't want to put your .env file in your repo (it has your secrets in it people could possibly steal). So on heroku there isn't a .env file to reference ... Instead you now use Heroku's environment variables while naming the variables and their values EXACTLY as you did in the .env file then it will work

Share:
15,032
npresco
Author by

npresco

Updated on June 08, 2022

Comments

  • npresco
    npresco almost 2 years

    I am setting up a heroku and I am using using this guide to set up my local environment for development. I have a '.env' file in the root of my application with the variable set up as noted in the guide. When I run

    heroku local:run rails runner "puts ENV['S3_BUCKET']"
    

    it returns a blank. How do I get it to recognize/load the '.env' file?