Slider

How to Set Environment Variables in Linux?

How to Set Environment Variables in Linux?
Environment variables play a crucial part in your operating system and the installed software. All operating systems come with some predefined variables to control the system. Users also can define custom environment variables to work with additional software.

Let's take the famous environment variable: PATH. This variable tells the system where to look for executable binaries in the file system. For example, Ubuntu has the following PATH variable definition by default:
/home/linuxedo/.local/bin:/home/linuxedo/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin
In Linux, directories included in the PATH variable must be separated by a colon (:). This PATH variable tells Ubuntu to look for executable binaries in any of the folders included in the variable definition.

The installation instructions of most of the compilers and command-line tools may ask you to add their "bin" folder to the PATH variable so that their commands can be accessed anywhere from the terminal. There are several other environment variables. Entering the command printenv will print all the environment variables along with their values in the terminal.
 
To check the value of a particular environment variable, use the echo command as shown below.
echo $PATH

Setting Environment Variables

There are three different use cases with environment variable settings.
  1. Temporary environment variables
  2. Environment variables for the current user
  3. Environment variables for all users


1. Temporary Variables

Temporary environment variables are used to change the behavior of a program or feed user input to a program by changing the environment variable in the current session.

Step 1:
To make those changes, use the export command as shown below.

export JAVA_HOME=/usr/lib/jvm/jdk1.8.0_301/
If you are appending a new directory to the PATH variable, always include the original PATH in the value as shown below. Otherwise, the new path will overwrite the original PATH instead of appending to it.
export PATH=$PATH:/usr/lib/jvm/jdk1.8.0_301/bin


Step 2:
After setting the variable, use the echo command to check if it has been correctly applied.

echo $JAVA_HOME
echo $PATH

 

Step 3 [Optional]:
To remove a variable, use the unset command as shown below. It will also get lost if you close the terminal and open a new one.

unset JAVA_HOME

 

 

2. Environment Variables for the Current User

To set an environment variable for the current user, add the export command to the ~/.bash_profile file. If the ~/.bash_profile file does not already exist, simply create a new file.

Step 1:
Open the ~/.bash_profile file in your favorite editor.

nano ~/.bash_profile


Step 2:
Append the export command to the end of the file.

export PATH=$PATH:/usr/lib/jvm/jdk1.8.0_301/bin


Step 3:
Save the changes and close the editor

Ctrl+O and Ctrl+X

Step 4:
After changing the ~/.bash_profile file, you need to restart the computer to apply the changes. However, the following command will load the changes into the terminal executing this command.

source ~/.bash_profile
echo $PATH



3. Environment Variables for All Users

Step 1:
To apply the environment variable for all users, open the /etc/environment variable as root user in your favorite editor.

sudo nano /etc/environment


Step 2:
If the file already has PATH variable, append your new path to that variable by separating the directories using a colon.
PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:/usr/lib/jvm/jdk1.8.0_301/bin"
JAVA_HOME="/usr/lib/jvm/jdk1.8.0_301"

In some distributions, the /etc/environment file may be empty. In such scenarios, use the following PATH variable definition to avoid breaking the system.
PATH="$PATH:/usr/lib/jvm/jdk1.8.0_301/bin"
JAVA_HOME="/usr/lib/jvm/jdk1.8.0_301"
The $PATH is equivalent to the PATH variable value before executing this line. Therefore, the new path will be appended to the PATH variable and will not break the system. Suppose you have deleted a folder from the system PATH variable, Linux may not be able to find the necessary commands next time when you login. If you are adding a new variable, add it to the next line as how the JAVA_HOME is added in the above code snippet.

Corrupting the system-level PATH variable may break your system and prevent you from logging into the system next time.


Step 3:
After making all the changes, save and close the editor.

Ctrl+O and Ctrl+X

Step 4:
Similar to the ~/.bash_profile, you need to restart the system to apply these changes. However, you can use the source command to load the changes into the active terminal as shown below.

source /etc/environment

Check if the changes have been applied successfully.

echo $PATH
echo $JAVA_HOME

Similar to JAVA_HOME, other applications may require setting their own environment variables. For example, Apache Maven requires a variable named M2_HOME and Golang requires a variable named GOPATH. Regardless of the variable name and the value, setting them in your system is always the same process. If there are any questions or issues, please comment below. I will try my best to answer your questions.
0

No comments

Post a Comment

disqus,linuxedo

DON'T MISS

News,Linuxedo
© all rights reserved