How to Set Environment Variables on a Linux Machine

How to Set Environment Variables on a Linux Machine
Photo by Markus Spiske / Unsplash

When building software, you start in a development environment (your local computer). You then move to another environment(s) (Staging, QA, etc.), and finally, the production environment where users can use the application.

While moving through each of these environments, there may be some configuration options that will be different. For example, in development, you may want to test CRUD operations with a dummy database with varying configuration values to the live database with real user data.

To ensure a seamless workflow and not have to regularly change the database configurations in code when moving to different environments, you can set environment variables for each.

In this tutorial, you will learn:

  • What environment variables are, and
  • How to set environment variables on a Linux machine.

Prerequisite

To follow along in this tutorial, you must have the following:

  • Basic knowledge of the terminal.
  • Access to a Linux machine — This article uses Ubuntu 22.04 (LTS) x64 distribution.

What are environment variables?

Environment variables are variables whose values are set outside the code of an application. They are typically set through the built-in functionality of an operating system. Environment variables are made up of name and value pairs, and you can create as many as you wish to be available for reference at a point in time.

Setting environment variables on a Linux machine

To set environment variables on a Linux machine, normally, in the shell session of your terminal, you would run the export command on each environment variable’s name and value like the following:

export ENVIRONMENT_VARIABLE_NAME = <value>

But doing so, if and when the particular shell session ends, all the environment variables will be lost. All the environment variables will be lost because the export command exports variables to the shell session’s environment, not the Linux machine environment.

To persist environment variables on a Linux machine, in any directory aside from your application’s directory, create an environment file with the vi editor using the following command:

$ vi .env

The above command will create and open a .env file, then to edit the file using the vi editor, press i and add your environment variables like in the image below.

After adding your environment variables, to save the file, press esc, then type :wq and press enter.

After saving the file, in the root directory of your Linux machine, run $ ls -la to view all files, including hidden ones, which should show you a .profile as in the image below.

Open up the profile file with $ vi .profile,  press i to edit the file, and at the end of the file, add the following configuration:

set -o allexport; source /<path_to_the_directory_of_.env_file>/.env; set +o allexport

The above configuration will loop through all the environment variables you added to the .env file and set them on the Linux machine.

To save the configuration, press esc, then type :wq and press enter as you did previously.

To confirm that the configuration took effect and your environment variables have been set, log out of your current shell session, log back in and then run:

$ printenv 

After running the above command, you should see your environment variables, as shown in the image below.

Conclusion

This tutorial explained environment variables and taught how to set them on a Linux machine. There is more to learn about environment variables in Linux. To learn more, check out the following resources: