How to set environment variable in Linux permanently
Categories:
How to Set Environment Variables Permanently in Linux

Learn the various methods to set environment variables in Linux, ensuring they persist across sessions and reboots for different scopes.
Environment variables are dynamic named values that affect the way processes run on a computer. They can store information like the default text editor, the path to executable files, or system-wide configurations. While you can set them temporarily for a single session, often you need them to persist. This article will guide you through the most common and effective ways to set environment variables permanently in Linux, covering different scopes from user-specific to system-wide.
Understanding Environment Variable Scope
Before diving into the methods, it's crucial to understand the scope of environment variables. Variables can be set for a single shell session, for a specific user, or globally for all users on the system. The method you choose depends on who needs access to the variable and for how long.
flowchart TD A[Start] A --> B{Variable Scope?} B -->|Current Session Only| C[Use `export` command] B -->|User-Specific & Persistent| D[Edit `~/.bashrc` or `~/.profile`] B -->|System-Wide & Persistent| E[Edit `/etc/environment` or `/etc/profile`] C --> F[Variable active until session ends] D --> G[Variable active for user's future sessions] E --> H[Variable active for all users & future sessions] F & G & H --> I[End]
Decision flow for setting environment variables based on scope.
Method 1: User-Specific Permanent Variables
For variables that should only apply to a single user, the best practice is to modify configuration files located in the user's home directory. The most common files are ~/.bashrc
, ~/.profile
, or ~/.bash_profile
.
~/.bashrc
and ~/.profile
(or ~/.bash_profile
) is important. ~/.bashrc
is sourced for interactive non-login shells, while ~/.profile
(or ~/.bash_profile
) is sourced for login shells. For most desktop users, adding variables to ~/.bashrc
is sufficient as it's sourced for new terminal windows.1. Open the configuration file
Open ~/.bashrc
(or ~/.profile
) in your preferred text editor. For example, using nano
:
2. Add the variable
At the end of the file, add your environment variable using the export
command. For example, to set a custom path for a specific application:
3. Save and exit
Save the file and exit the editor. If using nano
, press Ctrl+O
, then Enter
, then Ctrl+X
.
4. Source the file
To apply the changes immediately without logging out and back in, source the file:
5. Verify the variable
Check if the variable is set correctly:
nano ~/.bashrc
Opening .bashrc with nano
export MY_CUSTOM_PATH="/opt/my_app/bin"
export EDITOR="vim"
Adding environment variables to .bashrc
source ~/.bashrc
Sourcing .bashrc to apply changes
echo $MY_CUSTOM_PATH
echo $EDITOR
Verifying the set environment variables
Method 2: System-Wide Permanent Variables
For variables that need to be available to all users and all processes on the system, you should modify system-wide configuration files. The most common locations are /etc/environment
or files within /etc/profile.d/
.
Using /etc/environment
This file is specifically designed for system-wide environment variables. It's read by the pam_env
module at login and is not a script, so it doesn't support shell commands like export
.
1. Open /etc/environment
Open the file with root privileges:
2. Add the variable
Add your variable in the format KEY="value"
on a new line. Do not use export
.
3. Save and reboot
Save the file. A reboot is usually required for changes in /etc/environment
to take full effect system-wide.
sudo nano /etc/environment
Opening /etc/environment with root privileges
MY_SYSTEM_VAR="This is a system-wide variable"
JAVA_HOME="/usr/lib/jvm/java-11-openjdk-amd64"
Adding system-wide variables to /etc/environment
Using /etc/profile.d/
The /etc/profile
script, which is sourced by all login shells, often includes a line to source all scripts within the /etc/profile.d/
directory. This is a clean way to add system-wide variables or execute commands at login without directly modifying /etc/profile
.
1. Create a new script file
Create a new .sh
file in /etc/profile.d/
. The filename should be descriptive, e.g., my_vars.sh
:
2. Add variables to the script
Inside this file, use the export
command to set your variables.
3. Set permissions
Make the script executable:
4. Reboot or re-login
Log out and log back in, or reboot your system, for the changes to take effect.
sudo nano /etc/profile.d/my_vars.sh
Creating a new script for system-wide variables
#!/bin/bash
export APP_CONFIG_DIR="/etc/app_config"
export PATH="$PATH:/usr/local/bin/custom_scripts"
Adding variables to the system-wide script
sudo chmod +x /etc/profile.d/my_vars.sh
Making the script executable
PATH
, always include $PATH
to append your new directory rather than overwriting the existing path. This prevents breaking system commands.