How to Set Environment Variables on a Linux Machine
Tags: linux, beginners, tutorial, cloud
Published: 2022-09-14 15:41:38

This article was originally posted on [Everything DevOps](https://everythingdevops.dev/how-to-set-environment-variables-on-a-linux-machine/).

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](https://en.wikipedia.org/wiki/Create,_read,_update_and_delete) 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](https://ubuntu.com/blog/ubuntu-22-04-lts-released) 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. 

![](https://paper-attachments.dropbox.com/s_85BD5A817F94A5B2D9B888E1F8B318AAFBA3F079552683D255DD8AB9C1448DA1_1660603799811_Screenshot+2022-08-15+at+23.49.56.png)


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


![](https://paper-attachments.dropbox.com/s_85BD5A817F94A5B2D9B888E1F8B318AAFBA3F079552683D255DD8AB9C1448DA1_1660603956339_annotely_image+48.png)


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.


![](https://paper-attachments.dropbox.com/s_85BD5A817F94A5B2D9B888E1F8B318AAFBA3F079552683D255DD8AB9C1448DA1_1660602926799_annotely_image+45.png)


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.

![](https://paper-attachments.dropbox.com/s_85BD5A817F94A5B2D9B888E1F8B318AAFBA3F079552683D255DD8AB9C1448DA1_1660603418687_annotely_image+47.png)


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. 


![](https://paper-attachments.dropbox.com/s_85BD5A817F94A5B2D9B888E1F8B318AAFBA3F079552683D255DD8AB9C1448DA1_1660607459982_annotely_image+50.png)

# 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:


- [Environment Variables in Linux/Unix](https://www.geeksforgeeks.org/environment-variables-in-linux-unix/)
- [How to Set and List Environment Variables in Linux](https://linuxize.com/post/how-to-set-and-list-environment-variables-in-linux/)
- [List of Environment Variables in Linux/Unix](https://www.guru99.com/linux-environment-variables.html)