| by Arround The Web | No comments

How to Add a Path Permanently in Linux

This Linux tutorial explains how to add a path permanently in Linux. It is optimized for both users who are looking for a fast practical answer and for users who are looking for understanding both global and user environment variables.

The tips provided in this article include two methods to add the persistent PATH for both specific and all users, being useful for every Linux distribution.

The first two sections of the content go straight to the point, describing the steps to add a path permanently. After which, you can find a short explanation on environment variables.

All instructions explained in this article contain screenshots, making it easy to understand and execute the examples.

How to Add a PATH Permanently to a Specific User in Linux

The first step before we start is to check our current PATH environment variable.

You can do it in a simple way by executing the echo command followed by a dollar sign ($) and the environment variable whose value you want to see which, in this case is the PATH, as shown in the following screenshot:

echo $PATH

As you can see, there are 6 paths separated by colon. All paths must be separated by colon.

Another way to check all your environment variables including PATH is by running the env command as shown in the following example:

NOTE: While writing this tutorial, I edited my paths several times. That’s why you will see different values in the screenshots.

env

As you can see in the given figure, all environment variables are listed including the user paths.

To add a permanent path, one of the methods is to edit the hidden file .bashrc. You can print the hidden files by running the ls command followed by the –a (All) flag in the home directory.

ls -a ~/

To edit the .bashrc file, add a permanent path. Use the text editor of your preference. In my case, I used nano, as shown in the following:

nano .bashrc

In this first example, I added the fictitious path /home/linuxhint/something/default/bin where linuxhint is the user home.

The syntax is the following, where <PATH> must be replaced with the actual path that you want to add:

export PATH="$PATH:<PATH>"

In this case, I added the following line:

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

Alternatively, you can use the echo command to append the line using the following syntax where ‘Line Content’ must be replaced with the full command. And File must be replaced with the file you are editing (.bashrc).

echo 'Line Content' >> File

The practical example is the following, as stated previously, in case I want to add the path $HOME/something/default/bin or /home/linuxhint/something/default/bin (which are the same):

echo 'export PATH="$PATH:$HOME/something/default/bin"' >> .bashrc

Update your environment variables by running the following command:

source .bashrc

To print all environment variables including updated paths, you can use the env command.

env

To show only the PATH environment variable, run the command shown in the following figure:

echo "$PATH"

Another way to add a path to the user environment variable is by editing the “.profile” file located in the home directory.

This time, let’s use the ls command followed by the –l flag to show all files including the hidden files.

ls -ld .?*

As you can see, there is a file named “.profile”. Edit it using the text editor of your choice. In my case, I used nano.

nano .profile

Find the line similar to the one pointed by the white arrow in the following figure:

In this example, I added the /home/linuxhint/something2/default/bin path.

Below the found line, add a line as shown at the end of the following image, replacing the /home/linuxhint/something2/default/bin with the actual path that you want to add:

Update your environment variable by executing the command shown in the following image:

source .profile

Check the updated path with the following command:

echo $PATH

Or print all the environment variables including PATH using the env command:

env

You can find the instructions to add a permanent path for all users in the following discussion.

How to Globally Add a Path Permanently to All Users in Linux

This section shows how to add a global persistent path environment variable for all users.

This can be done by editing two files, /etc/profile and /etc/bash.bashrc.

In the first example, I will show you how to edit the /etc/profile file.

Use the text editor that you used to modify the /etc/profile.

sudo nano /etc/profile

At the end of the file, add the following line where /opt/something10/bin must be replaced with the path that you want to add.

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

Update the environment variable PATH using the source command as done in the previous section of this article.

source /etc/profile

Check if the path was properly added using the echo command as shown in the following image:

echo $PATH

Another way to permanently add a path globally is by editing the /etc/bash.bashrc file using the text editor to open it.

sudo nano /etc/bash.bashrc

In the following example, I added the new path /opt/something20/bin.

Append a similar line as the following, replacing the /opt/something20/bin with the actual path that you want to add.

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

Update the PATH environment variable using the source command. Then, check it by running the echo command as shown in the following example:

source /etc/bash.bashrc
echo $PATH

As you can see, both methods worked successfully and the paths were added.

User vs System Wide Environment Variables

The difference between the user specific and the global environment variables is the following:

  • User Environment Variables: User environment variables like PATH are defined in the user home configuration files. They are loaded from the home directory when the user starts a session.
  • System Wide Variables: This type of variables don’t belong to a specific user, but to the whole system affecting all users.

This tutorial deeply explained how to add a PATH variable. To edit the user environment variables, you need to edit the .bashrc  or .profile files located in the home directory. The variables exported to this file will load every time the user starts a session.

The system variables are stored in the /etc/bash.bashrc or /etc/profile files.

Conclusion

As you can see, adding the PATH variables both temporarily and permanently is pretty easy and can be done by any Linux user independently of the knowledge level. Every Linux user must understand the PATH variable function and how to manage it.  Other variables are also deeply explained at Linux Hint. The content previously explained is valid for almost every Linux distribution.

Thank you for reading this tutorial showing how to permanently add the PATH variable. Keep following us for more professional content.

Share Button

Source: linuxhint.com

Leave a Reply