GICT knowledge base
How to Set Environment Variables Permanently in Linux
Published
Environment variables help your Linux system and applications know where to find files, how to run programs, and how to behave in specific situations. For example, you might use environment variables to set the default editor, configure language preferences, or set up the correct path for custom software. When you set an environment variable permanently, it stays set every time you log in or restart your computer, making your setup reliable and saving you from repeating the same command over and over.
This detailed, beginner-friendly guide explains what environment variables are, why you might want to set them permanently, and how you can do this in several common ways. By following these steps, you can make your Linux experience more convenient, consistent, and personalized.
Prerequisites
- Access to a Linux computer (any distribution, such as Ubuntu, Fedora, Debian, or Arch)
- Ability to open and use the terminal
- Basic knowledge of editing text files with nano, vim, or another editor
- Sudo (administrator) access if you want to set variables for all users
What Are Environment Variables?
Environment variables are key-value pairs stored by your operating system. Programs and scripts use them to get information about your environment, like where to find libraries, which language you want to use, or which editor you prefer. Some common environment variables include:
- PATH: Tells the system where to look for executable files when you run a command.
- HOME: Points to your home directory.
- LANG: Sets your language and regional settings.
- JAVA_HOME, PYTHONPATH: Used by programming tools to find the right version of Java or Python.
You can set environment variables temporarily (for your current session only) or permanently (so they are always set, even after logout or reboot).
Why Set Environment Variables Permanently?
You might want to set environment variables permanently for reasons like:
- Making sure your PATH always includes the directory for custom scripts or applications.
- Setting JAVA_HOME so Java-based programs work without extra configuration.
- Defining API keys or credentials for development work.
- Setting up development environments for languages like Python, Ruby, or Node.js.
- Customizing your system’s language, editor, or timezone.
If you set a variable only for your current session, it disappears when you close the terminal or log out. Setting it permanently avoids this hassle.
How to Set Environment Variables Permanently
You can set environment variables permanently in several ways:
- For just your user account: Edit your shell configuration file (like .bashrc, .bash_profile, or .zshrc).
- For all users: Edit the global
/etc/environmentfile or/etc/profile. - For system services: Use systemd service overrides.
Let’s cover each method step by step.
Set Environment Variables Permanently for Your User Only
Using .bashrc or .bash_profile
Most Linux users run the Bash shell by default. The .bashrc file is read every time you open a new terminal window, while .bash_profile is read when you log in. For most desktop users, .bashrc is a good choice.
- Open your terminal.
- Open
.bashrcin your home directory with nano (or your preferred editor):
nano ~/.bashrc - Scroll to the end of the file and add a line for each environment variable:
export VARIABLE_NAME="value"
For example:
export PATH="$PATH:/home/youruser/my-scripts"
export EDITOR="nano"
export JAVA_HOME="/usr/lib/jvm/java-11-openjdk-amd64" - Save the file (in nano, press Ctrl+O, then Enter, then Ctrl+X to exit).
- Reload the configuration so the changes work right away, or close and reopen the terminal:
source ~/.bashrc - Test your variable:
echo $VARIABLE_NAME
Replace VARIABLE_NAME with your variable’s name.
Note: If your system uses Zsh, use ~/.zshrc instead of .bashrc.
Using .profile or .bash_profile
If you want the variable to be set for login shells (for example, when you log in over SSH), use .profile or .bash_profile:
- Open
.profileor.bash_profile:
nano ~/.profile - Add your export line, save, and close.
- Log out and log back in to apply the changes.
Set Environment Variables Permanently for All Users
To apply an environment variable to every user on your system (including root and system services), use the /etc/environment file.
- Open the
/etc/environmentfile with sudo:
sudo nano /etc/environment - Add a new line for each variable, using VARIABLE_NAME=value (no export needed):
NODE_ENV=production
COMPANY_NAME="YourCompany" - Save and close the file.
- Log out and log back in, or restart your system, to apply the changes.
Note: Do not use shell features like $PATH or command substitution in /etc/environment. Enter simple key-value pairs only.
Using /etc/profile
You can also use /etc/profile for variables that need shell features (like appending to PATH):
- Edit the file:
sudo nano /etc/profile - Add lines like this:
export PATH="$PATH:/opt/global-tools" - Save, close, and reboot or log out and back in.
Set Environment Variables for Systemd Services
If you want to set environment variables for a specific system service (for example, a web server or database), use a systemd override:
- Create a drop-in directory for the service:
sudo mkdir -p /etc/systemd/system/myservice.service.d - Create a configuration file:
sudo nano /etc/systemd/system/myservice.service.d/environment.conf - Add the following:
[Service] Environment="API_KEY=yourapikey" "ENVIRONMENT=production" - Reload systemd and restart the service:
sudo systemctl daemon-reload
sudo systemctl restart myservice
Tips and Troubleshooting
- Always spell your variable names in all capital letters for clarity.
- Double-check for typos, as a small mistake can break your shell configuration.
- You can add comments by starting a line with
#in shell files. - If a variable does not appear set, check for typos, run
source ~/.bashrcagain, or log out and back in. - For scripts or cron jobs, remember that they might not load your user’s
.bashrc. Use/etc/environmentor set variables directly in the script.
Examples of Common Environment Variables
- Add a custom scripts folder to your PATH:
export PATH="$PATH:$HOME/scripts" - Set the default editor to vim:
export EDITOR="vim" - Define the default locale:
export LANG="en_US.UTF-8" - Configure a Java home directory:
export JAVA_HOME="/usr/lib/jvm/java-17-openjdk-amd64"
Conclusion
Setting environment variables permanently in Linux is a practical way to make your system more personal, flexible, and ready for your day-to-day work. Whether you want to simplify your workflow, make tools easier to use, or configure software, these changes help you avoid repeating the same setup every time you use your computer. Edit your .bashrc (or similar) for per-user settings, or /etc/environment for system-wide changes, and you’re all set.
Related guides
How to Set Up zram on a Small VPS
Configure zram on Ubuntu with kernel and swap checks. Understand compressed memory limits, monitor real use and avoid unsafe swap-file changes.
LinuxHow to Find Services That Need a Restart After Updates with needrestart
Find services using outdated libraries with needrestart on Ubuntu. Read restart reports, review automatic restart policy and plan maintenance safely.
LinuxHow to Limit a Linux Service’s Memory with systemd
Limit a Linux service with systemd MemoryHigh, MemoryMax and swap controls. Check cgroup support, diagnose pressure and remove only your own policy.
LinuxHow to Protect SSH with CrowdSec on Ubuntu
Protect SSH with CrowdSec on Ubuntu. Connect log detection to firewall enforcement and test temporary blocking without losing administrator access.
