How to Add Environment Variable in Linux Permanently
|

How to Add Environment Variable in Linux Permanently

If you want to know how to add environment variable in linux permanently, the short answer is to place the variable in the correct startup file for your shell, such as ~/.bashrc, ~/.zshrc, or ~/.profile, or set it system-wide in /etc/environment. The right file depends on whether you want the variable for just one user or for every user on the machine.

What it means to set a Linux variable permanently

What it means to set a Linux variable permanently

An environment variable is a value that programs can read while they run. Common examples include PATH, JAVA_HOME, EDITOR, and proxy settings. If you set a variable only in the current terminal session, it disappears when you close that terminal. A permanent setup stores the value in a shell configuration file or a system-wide configuration file so it is loaded again in future sessions.

There are two main ways to make a variable permanent:

  • User-level, which applies only to one account.
  • System-wide, which applies to all users on the Linux machine.

User-level vs system-wide setup

Choose the method based on who should see the variable and when it should load.

Use caseBest place to set itNotes
Variable for one user only~/.bashrc, ~/.zshrc, or ~/.profileDepends on the shell and whether it is a login shell
Variable for all users/etc/environment or shell profile files under /etcRequires admin access
PATH changes for interactive shells~/.bashrc or ~/.zshrcCommon for terminal sessions
Settings needed in login sessions~/.profile or ~/.bash_profileUseful for SSH logins and desktop sessions

How to add an environment variable permanently in Linux

How to add an environment variable permanently in Linux

The exact steps depend on whether you want the change for one user or for the whole system. Start with the user-level method unless you specifically need a system-wide setting.

1. Add the variable for your own user account

Add the variable for your own user account

For Bash, the most common file is ~/.bashrc. For Zsh, use ~/.zshrc. If you want the variable available in login sessions too, ~/.profile is often a better choice.

Open the file with a text editor, such as:

nano ~/.bashrc

Add a line like this:

export MY_APP_HOME="/opt/myapp"

Then save the file. The export part is important, because it makes the variable available to programs started from that shell.

2. Reload the file without restarting Linux

After editing the file, load the new setting into your current terminal session with:

source ~/.bashrc

If you used ~/.zshrc, run:

source ~/.zshrc

For ~/.profile, use:

source ~/.profile

This step is not always required for future terminals, but it lets you test the change immediately.

3. Verify the variable is available

Check the value with:

echo $MY_APP_HOME

If the variable is set correctly, the terminal should print the path or value you defined.

How to set an environment variable system-wide

If every user on the system should get the variable, you can set it in /etc/environment. This file is commonly used for simple system-wide variables that do not require shell-specific logic.

Open the file with administrative privileges:

sudo nano /etc/environment

Add the variable in this format:

MY_APP_HOME="/opt/myapp"

Save the file, then log out and log back in. In many cases, a full logout is enough, though some desktop environments or remote sessions may require reopening the session.

Important: /etc/environment does not use the normal shell syntax in the same way as .bashrc. It is meant for simple assignments, not shell commands. Do not add export there.

Which file should you use?

This is where many Linux users get stuck. The right file depends on how you access the shell and what you need the variable for.

  • Use ~/.bashrc for interactive Bash terminal sessions.
  • Use ~/.zshrc for interactive Zsh sessions.
  • Use ~/.profile for login shells and desktop login environments.
  • Use /etc/environment for simple system-wide variables.

If you are unsure, a practical rule is this: use ~/.bashrc for terminal-only settings, and ~/.profile for values you want loaded when you sign in. This matters when you use SSH, a graphical desktop terminal, or a remote Linux server, because each session type can read different startup files.

How to set PATH permanently in Linux

Setting PATH is one of the most common reasons people search for how to add environment variable in linux permanently. Instead of replacing the whole PATH, usually you append or prepend a directory.

Example for Bash or Zsh:

export PATH="$HOME/bin:$PATH"

This puts $HOME/bin at the front of the search path, so commands there are found before system commands.

If you want to add a specific tool directory, such as a custom binary folder, use the full path:

export PATH="/opt/mytool/bin:$PATH"

For a system-wide PATH change, be careful. Editing global startup files can affect every user and may create conflicts if a directory is missing or invalid. In many cases, user-level PATH changes are safer.

Examples of common permanent environment variables

Here are a few practical examples you can place in the right file.

export JAVA_HOME="/usr/lib/jvm/java-17-openjdk-amd64"
export EDITOR="nano"
export MY_API_KEY="example-value"

If you need these values for every terminal session, put them in a shell startup file such as ~/.bashrc. If you need them before the shell starts, use a system-wide file such as /etc/environment when appropriate.

How to reload and verify the variable

After editing the file, use one of these approaches:

  1. Reload the file with source ~/.bashrc, source ~/.zshrc, or source ~/.profile.
  2. Open a new terminal to test the permanent setting.
  3. Check the value with echo $VARIABLE_NAME.
  4. Confirm the shell sees it with printenv VARIABLE_NAME.

For example:

printenv MY_APP_HOME

If the command prints nothing, the variable is not loaded in the current session or it was added to the wrong file.

Common problems and fixes

The variable works only in the current terminal

The variable works only in the current terminal

This usually means you ran export MY_VAR=value directly in the terminal, but did not save it in a startup file. Add it to the correct file and reload the shell.

You edited the wrong file

Many systems use different files for different shell types. Bash often reads ~/.bashrc for interactive terminals, while login shells may read ~/.profile or ~/.bash_profile. If the variable does not appear, check which shell you are using with:

echo $SHELL

Then make sure you edited the file that shell actually loads.

You forgot to use export

If you write MY_VAR=value in .bashrc without export, the shell may know the value, but child programs may not inherit it. In most cases, use:

export MY_VAR="value"

Your GUI terminal and SSH session behave differently

Desktop terminals, SSH logins, and non-login shells do not always read the same startup files. That is why a variable may appear after one kind of login but not another. If you need the variable broadly available, consider placing it in ~/.profile or adjusting your shell startup logic carefully.

Your value contains spaces

Wrap the value in quotes if it has spaces:

export MY_VAR="some folder name"

Without quotes, the shell may treat the value as multiple words and break the assignment.

Quick decision guide

If you want a fast way to choose the right approach, use this:

  • One user, terminal only: edit ~/.bashrc or ~/.zshrc.
  • One user, login sessions too: edit ~/.profile.
  • All users: edit /etc/environment if the variable is simple.
  • Changing PATH: add the directory with export PATH="new/path:$PATH".

If you are setting something like PATH, JAVA_HOME, or an app-specific directory, the safest approach is usually to place it in the user’s shell startup file first, verify it works, and only then move to a system-wide configuration if you truly need it.

Frequently Asked Questions About how to add environment variable in linux permanently

Where do I put environment variables in Linux?

For one user, put them in ~/.bashrc, ~/.zshrc, or ~/.profile. For all users, use /etc/environment or another system-wide configuration file depending on the shell and distribution.

What is the difference between .bashrc and .profile?

.bashrc is commonly used for interactive Bash sessions, while .profile is used for login shells. If a variable must load when you sign in, .profile is often the better choice.

How do I set PATH permanently in Linux?

Add an export PATH=... line to your shell startup file, usually ~/.bashrc or ~/.zshrc. Be sure to include the existing $PATH so you do not remove system commands.

How do I check whether an environment variable was saved permanently?

Close the terminal, open a new one, and run echo $VARIABLE_NAME or printenv VARIABLE_NAME. If it still appears, the variable is likely stored in the correct startup file.

Why does my environment variable disappear after I close the terminal?

Because it was set only for the current session. To make it permanent, add it to a shell configuration file or a system-wide file instead of typing it directly into the terminal.

How do I set a system-wide environment variable in Linux?

Use sudo to edit /etc/environment, add the variable in assignment format, then log out and back in. This makes the value available to users who sign in to the system.

Conclusion

To make an environment variable permanent in Linux, save it in the right startup file instead of typing it only into the terminal. Use ~/.bashrc or ~/.zshrc for your own interactive sessions, ~/.profile for login sessions, and /etc/environment for simple system-wide values. Once you reload the file and verify the result, the variable should persist across new sessions.

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *