ENV variables in Rails 7.x

This might be silly but I need help in finding where my ENV file is. I am unable to view my ENV file.

When I started my rails app, some of the values were ENV[“var_name”], eg: ENV[" DATABASE_PASSWORD"] so I assumed those environment variables must be stored somewhere. but it is nowhere to be found.

When it comes to the “Figaro” gem we store it in config/application.yml. Another way is storing them in other YML files and loading them in the “config” folder. but there is no code added for it. since it is newly created, no code was added to the Rails app.

I also checked my Gemfile and Gemlock files. there was no “Dotenv” or “Figaro” present. Does Rails provide this ENV feature thing in-built?

I might be missing something or might not be aware of something. Enlighten me on how to view this file and update the ENV variables. Thanks, folks.

p.s: I use a Windows machine.

If you are using rbenv, you might have a .rbenv-var files with that.

If not, then ruby by default pulls the exported variables from your shell. Take a look at ~/.bashrc or ~/.zshrc

1 Like

ENV gets its values from the UNIX environment, which you can see from the command line (where you type rails s or rake):


In production, this is a decent place to store stuff like database credentials. For local development, it’s not a very good place, so you can use dotenv to allow them to be kept in a file. You want to use the dotenv-rails variant, which will set itself automatically.

Once you have that, create two files: .env.development and .env.test, and place your dev and test values, respectively, in there:

# .env.development

# .env.test

If those files have secrets you don’t want in verison control, don’t check them in or put safe-to-checkin-to-verdsion control stuff in those files, check those in, and put not-safe-to-checkin into .env.development.local and .env.test.local

I know that I am a “bit” late, but still …

While implicitly touched upon in other responses, the ENV syntax queries the underlying operating system for environment variables. How they came into being there is of no concern to Rails. And that is intentional, because it allows to “abstract away” the configuration and use whatever way a given system has to chosen to rely on.

It is also important to note that depending how the Rails server was started, some of the mechanisms mentioned here, may actually not apply. Because they assume a running Shell from which Rails was started. That is a frequent source of error, also in other situations. A classic example is that on Linux et al. a cron job does not work because the program called is not in the path, while it is in a shell from where the execution works.

In a nutshell, this topic is specific to the OS and also how the underlying program, in this case Rails, was started.

Then I would suggest to search for how to change the environment variable there. Please note that if you run things as a service that may need special attention.