Skip to content

Welcome to Tech by Example

Menu
  • Home
  • Posts
  • System Design Questions
Menu

Load a .env or environment file in Ruby Language

Posted on August 13, 2021August 13, 2021 by admin

Overview

dotenv gem can be used to load .env or environment file in ruby

https://github.com/bkeepers/dotenv

It takes a variable number of arguments where each argument can be the .env filenames that it needs to load

Program

Create a file local.env with below contents

STACK=DEV
DATABASE=SQL

Here is the program

require 'dotenv'

Dotenv.load("local.env")
puts ENV["STACK"]
puts ENV["DATABASE"]

Output

DEV
SQL

It loads the local.env file and gives the correct output

It can also be used to load multiple .env files. Create a new file test.env with below contents

TEST=UNIT
require 'dotenv'

Dotenv.load("local.env", "test.env")
puts ENV["STACK"]
puts ENV["DATABASE"]
puts ENV["TEST"]

Output

DEV
SQL
UNIT

This was about loading .env files in ruby

©2025 Welcome to Tech by Example | Design: Newspaperly WordPress Theme