Home Ubuntu How to Permanently Set $PATH in Linux

How to Permanently Set $PATH in Linux

Unlocking the full potential of your Linux system involves understanding the importance of the $PATH variable, which serves as a roadmap for your operating system to locate and execute commands swiftly and effortlessly. However, if you find yourself repeatedly setting the $PATH every time you log in or reboot, it is time to learn how to permanently configure it.

In this comprehensive guide, we will walk you through the step-by-step process of setting the $PATH variable in Linux, ensuring that your customizations persist across sessions.

What is a $PATH Variable in Linux

The $PATH variable is a fundamental part of the Linux operating system, acting as a reference to locate executable files. It consists of a list of directories separated by colons (:), with the system searching for commands in these directories when they are executed. Understanding the importance of the $PATH variable is crucial to effectively manage your command-line experience.

There are multiple approaches to permanently setting the $PATH variable in Linux and the two most common methods involve:

  1. Editing Shell Configuration Files
  2. Utilizing Environment-Specific Files

Each method has its advantages and disadvantages, so choose the one that best suits your needs. The PATH variable can also be set on a per-user basis or system-wide for all user accounts.

Check $PATH Variables in Linux

You can see your $PATH variables by running the following echo command:

$ echo $PATH
Check $PATH Variable in Linux
Check $PATH Variable in Linux

Add a Directory to PATH Variable Linux

To temporarily add a directory, for example, /opt/sysadmin/scripts to your path, you can run the following command:

$ PATH=$PATH:/opt/sysadmin/scripts
$ echo $PATH
Add Directory to PATH in Linux
Add Directory to PATH in Linux

One way to permanently set the $PATH variable is by modifying shell configuration files, such as .bashrc or .bash_profile. These files are executed each time you start a new shell session.

$ nano ~/.bashrc
OR
$ vim ~/.bashrc

Locate the line that begins with "export PATH=" and modify it to include the directories you want to add to the $PATH variable. Separate each directory with a colon (:).

The .bashrc File in Linux
The .bashrc File in Linux

If you are unable to find this line, you can add it manually inside the file at the bottom of the page. As an example, I am adding the /opt/sysadmin/scripts directory path to my PATH using the command given below:

export PATH="$PATH: /opt/sysadmin/scripts"
Permanently Add Directory to PATH in Linux
Permanently Add Directory to PATH in Linux

To apply the changes, either open a new terminal or run the following command:

$ source ~/.bashrc

Note: The above method only works for user accounts.

Permanently Set $PATH for All Users

To permanently set system PATH for all users on the system, append the following lines at the end of the /etc/profile file. On some Linux distros, you can also use the /etc/bash.bashrc file (if it exists) but it should be sourced in /etc/profile for changes in it to work.

PATH="$PATH:/opt/sysadmin/scripts"
export  $PATH
OR
$export  PATH="$PATH:/opt/sysadmin/scripts"

Alternatively, instead of making changes directly in the /etc/profile (which is not recommended), you can create a script (ending with the .sh extension) under the directory /etc/profile.d (if it exists) and make the changes in that script.

$ sudo vim /etc/profile.d/set_system_path.sh

Append the following line in it:

export PATH="$PATH:/opt/sysadmin/scripts"

Next, source the ~/.bashrc or /etc/profile or /etc/bash.bashrc (depending on the one you used), for the changes to take effect.

$ source ~/.bashrc
OR
$ source /etc/profile
OR
$ source /etc/bash.bashrc

Then confirm that your PATH has been set correctly:

$ echo $PATH

/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/opt/sysadmin/scripts:/opt/sysadmin/scripts

Set PATH Variable in /etc/environment File

Another approach to permanently setting the $PATH a variable is by using environment-specific files. Two common files used for this purpose are /etc/environment and /etc/profile.d/.

$ sudo nano /etc/environment

Next, locate the line that begins with "PATH=" and add the directories you want to include, separated by colons (:).

PATH="/opt/sysadmin/scripts:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/>

To apply the changes, reboot your system or run the following command:

$ source /etc/environment

After making modifications to the $PATH variable, it’s crucial to verify that the changes have taken effect.

$ echo $PATH
Check PATH in Linux
Check PATH in Linux

This will display the updated $PATH variable, including the directories you added. Ensure that the desired directories are now part of the $PATH variable.

Conclusion

To make the most of your Linux system, it’s a must for you to understand the $PATH variable that acts like a map and helps your computer find and run commands quickly.

If you are tired of setting  $PATH every time you log in or restart your computer, better to set it permanently using the step-by-step guidelines presented in the above article.

If you have managed to do this process, you will have a better command-line experience on your Linux system.

Ravi Saive
I am an Experienced GNU/Linux expert and a full-stack software developer with over a decade in the field of Linux and Open Source technologies. Founder of TecMint.com, LinuxShellTips.com, and Fossmint.com. Over 150+ million people visited my websites.

Each tutorial at UbuntuMint is created by a team of experienced writers so that it meets our high-quality standards.

Was this article helpful? Please add a comment to show your appreciation and support.

Got something to say? Join the discussion.

Thanks for choosing to leave a comment. Please keep in mind that all comments are moderated according to our comment policy, and your email address will NOT be published or shared. Please Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.