An Introduction to Environment variables and how to use them
During software development, there are things we shouldn't share with our code. These are often configurations like secret keys, database credentials, AWS keys, API keys/tokens, or server-specific values.
According to 12 Factor methodology, it is wrong to store config as constants in the code because config varies substantially across deploys, code does not.
What Are Environment Variables?
When I started writing code it took quite some time to figure out what an environment variable is and how they can be set-up.
In a nutshell, an environment variable is a combination of values, called key/pair values. They hold information that other programs and applications can use.
One common environment variable described by the operating system is PATH which contains a set of directories where executable programs are located.
Using Git Bash shell:
echo $PATH
/c/Users/MICHAEL/bin:/mingw64/bin:/usr/local/bin:/usr/bin:/bin:/mingw64/bin:/usr/bin:/c/Users/MICHAEL/bin:/c/WINDOWS/system32:/c/WINDOWS:/c/WINDOWS/System32/Wbem:/c/WINDOWS/System32/WindowsPowerShell/v1.0:/c/WINDOWS/System32/OpenSSH:
How to Set-up Environment Variables
Aside from built-in environment variables defined by our operating system or third party programs, there are several ways to create environment variables:
- the Windows Environment Variables setup: you can use this to configure global variables
- .bashrc file via Bash shell
- the export command in Bash like environment or set command in windows command-line:
#in bash shell
export API_KEY=https://dev.to/mojemoron #key=value
echo $API_KEY #echo key
https://dev.to/mojemoron #value
#in windows cmd
set API_KEY=https://dev.to/mojemoron
echo %API_KEY%
-
.env file
: this is the most common way to create environment variables per project especially during development. To do this, you create a.env file
in your project's root directory and set the various environment variables you need in your project and afterward, you use a library for your respective programming language to load the file which will dynamically define these variables. For example, using python-decouple in a Django project to load environment variables.
touch .env #create this file in the root directory
#add this to the .env file
DB_HOST=localhost
DB_NAME=root
DB_USER=root
DB_PWD=root
DB_PORT=5432
#in your settings.py file, load the environment variables like so
DATABASES = {
'default': {
'ENGINE': 'django_postgres_extensions.backends.postgresql',
'HOST': config('DB_HOST'),
'NAME': config('DB_NAME'),
'USER': config('DB_USER'),
'PASSWORD':config('DB_PWD'),
'PORT': config('DB_PORT'),
}
}
There are several libraries that can help you achieve this:
- Node js
- Python
- Ruby
- Java
- Cloud service providers like AWS, GCP, Heroku, Azure, and Digital Ocean recommend the use of environment variables for configuration and have built-in features for setting and managing them.
Summary
It's very important to note that separating application configurations such as credentials, API keys from code will remove unnecessary roadblocks/headaches when deploying your apps to multiple environments.
Finally, remember not to check in your .env file
into a version control system by using git .ignore file.
You don't want to expose your environment variables to the world!
How do you configure environment variables for your projects?
Kindly follow me and turn on your notification.
Thank you! Happy coding! β
Top comments (4)
Hi thanks for the post, yes env variable is an absolute must!
There's some history behind them in the Early days of Unix video by the person who made sh and env vars too :D
Also please check out my package envariable that will read the
.env
file into yourprocess.env
object to be able to develop locally! I use it for every web server during local development. When deployed, the file won't be there and they'll be read from the actual environment (like heroku, etc)Great, I will check it out
Really thorough article. Thanks Michael π
You are welcome π