Environment variables are a crucial part of any system as they are responsible for storing system configurations and other important data such as API keys, to make them accessible for other system processes.
In this guide, I will walk you through the step-by-step process of setting up and managing environment variables on Ubuntu version 24.04, Noble Numbat.
Before proceeding, it’s beneficial to take a brief moment to explore the concept of environment variables and understand its various types.
What are Environment Variables
Environment variables are pairs of keys and values stored within a computer system. They are primarily used by applications that rely on them to function properly. These variables store important information such as the path of binaries that are required by programs to execute, configurations, and secrets like encrypted user credentials or API keys.
Types of Environment Variables
There are two main categories of environment variables based on their scope.
- Local Variables
- Global Variables
Local Variables
Local environment variables or temporary environment variables are limited to the current terminal session by scope. Once the terminal session is ended the set variables will be removed.
Global Variables
As the name suggests global environment variables are system-wide variables that will be accessible anywhere on your system across users. These variables will persist even on rebooting the system.
View Environment Variables on Ubuntu
To list and view environment variables on Ubuntu the env command is used.
env
To list the specific environment variable printenv command is used with the variable name.
printenv <VAR_NAME>
For example, to print the SHELL environment variable value, use:
printenv SHELL
Add Environment Variable on Ubuntu
The export utility is primarily used to set the environment variable in Ubuntu. The general syntax for adding environment variables in Ubuntu is mentioned below:
export VARIABLE_NAME=Value
As a standard practice, it’s customary to use uppercase letters for variable names and to separate multiple words with underscores (_) in the variable name.
As previously mentioned, environment variables can be classified into two distinct scopes: local and global. Let’s understand the processes of setting both types of environment variables on Ubuntu.
Set Local Environment Variable
To set the temporary environment variable on Ubuntu, go through the steps given below:
1. Launch the terminal.
2. Use the export command line utility with variable name and its value.
export NEW_USER=john
Here, I am setting NEW_USER as the environment variable name and john as its value.
3. Verify whether the environment variable has been added or not.
printenv NEW_USER
The output indicates that the environment variable has been set.
Since this variable is temporary; on restarting the session, the variable will no longer be accessible.
Set Global Environment Variable
To set the system-wide environment variable on Ubuntu, go through the steps given below:
1. Open the environment variable file in any text editor.
sudo nano /etc/environment
2. Type the environment variable in the new line.
Save the file using the ctrl+O keys; press the Y/y key to approve the changes and exit using ctrl+X.
3. Source the file Or restart the terminal session.
source /etc/environment
4. Verify the global environment variable.
If you can’t verify, try to log out and log in back to the system.
Add User-Specific Environment Variable on Ubuntu
Since global environment variables are available for all users, the users can also add their own environment variable in the profile, bashrc, or zshrc file.
It is recommended that users utilize the ~/.profile file to add the environment variables because they will be executed for both login and non-login shells. On the other hand, adding environment variables in the ~/.bashrc or ~/.zshrc will execute them only for non-login shells, such as terminal windows.
1. Open the ~./profile file using the nano editor.
sudo nano ~/.profile
2. Add the environment variable at the end of the file.
export NEW_USER=john
Save and exit the editor.
3. Source the file.
source ~/.profile
4. Verify, using the printenv command or echo.
echo $NEW_USER
The ~/.bashrc or ~/.zshrc files are configuration files for bash and zsh, respectively. You can use these files to add user-specific environment variables using the given procedure.
Note: You can also use ~/.bash_profile or ~/.zprofile files but they will be shell-specific, however, ~/.profile is more general and applies to all shells.
Add Environment Variables Using Shell Script
Global or system-wide environment variables can also be added using the shell script. The /etc/profile.d/ directory contains the shell-specific scripts that are used to initialize the environment when a new shell session is started.
Follow these steps to set the environment variable using a shell script:
1. To add the environment variable in the /etc/profile.d directory; create a shell script.
sudo nano /etc/profile.d/env_script.sh
2. Type the environment variable using the export command.
3. Log out using the logout command and log back.
4. Verify the variable using the printenv command.
Configure the PATH Environment Variable
The PATH environment variable is one of the key variables that is used to set the path of the executable binaries. Before executing any command, the terminal searches through the directories specified in PATH to find the corresponding executable for the command being invoked. For example, instead of running /usr/bin/java you use the java command because /usr/bin is set as a PATH environment variable.
To view the PATH variable use the printenv utility.
printenv PATH
All the directories set as PATH variables are checked sequentially.
To append or modify the PATH variable to include the desired directory, use the export command.
export PATH=/path/of/directory/:$PATH
To add path at the end of the PATH variable.
export PATH=$PATH:/path/of/directory/
For example, I have installed the NodeJS using the nvm. To identify the path of a directory or executable, you can use the which command in the terminal.
which node
To add the path to an environment variable:
1. First open the ~/.profile file and at the bottom of the file add the line given below:
export PATH=$PATH:/home/sam/.nvm/versions/node/v22.0.0/bin
Save the file and exit the editor.
2. Source the file to apply the changes:
source ~/.profile
The user-specific variable can be added using various approaches; as discussed in the Add User-Specific Environment Variable section.
Add Environment Variable using Python
Developers use environment variables to hide sensitive data such as API keys and passwords.
Environment variables for Python can be set in Ubuntu using the methods mentioned above.
To access the environment variable in Python, use the os module.
import os api_key=os.environ.get(“API_KEY”) #Or api_key=os.getenv(“API_KEY”)
However, it is not recommended; to set environment variables using Python, use the code given below:
import os os.environ[“API_KEY”]=”AX08529XCD109sQSS”
The key has been set to print it use.
print(os.environ[“API_KEY”])
Remove Environment Variables on Ubuntu
To remove the temporary environment variable on Ubuntu the unset command is used with the variable name.
unset <VARIABLE_NAME>
If the variables have set the environment variables within the /etc/environment, ~/bashrc, ~/.zshrc or ~/.profile files then you manually have to open these files and remove the lines.
Best Practices to Set Environment Variables on Ubuntu
Environment variables play a crucial in configuring the applications and their operations, however, to enhance security, best practices should be followed:
- Avoid adding sensitive data directly in your code.
- Follow a uniform naming convention and use descriptive names for the environment variables to ensure clarity and consistency in your code.
- Put your /etc/environment file in the gitignore file to avoid accidental commits of crucial data.
- For improved security, use the configuration management tools to handle secrets and other confidential information effectively.
Conclusion
Setting and managing environment variables on Ubuntu is crucial, especially for developers. This guide explains environment variables their types and how to set and unset them on Ubuntu 24.04. It also discussed accessing environment variables using Python.
Check our LinkedIn company page