Setting up Dotenv in Rails

Got all your credentials in Rails App Code. Well, no one wants that 😛, but here we are.

In this article, we will set up separate env files for our server for different configurations (test, development, production). We will go through how to actually implement this using DOTENV in Rails.

First, Add dotenv in Gemfile

Next, Execute to install the above gem in your repository

Now, Add your application configuration to your .env file in the root of your project:

Whenever your application loads, these variables will be available in ENV:

Add .env to .gitignore, so that you don’t end up uploading env back in git

That’s It. We have successfully implemented DOTENV in Rails, Go and Put your credentials here and make your code credentials accessible.

I hope this helped 😄.

Let’s dive in a little more,

Setting up Multiple Environments using DOTENV

We can setup different environments in Rails using dotenv, by creating different files for each environment in the format .env.{environment}.

Let’s say I want to have different credentials in test and production, Our code will like as



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store