How to Permanently Add a Path to PATH in Ubuntu?

In Ubuntu, the “PATH” is an environment variable that specifies a set of directories where the system should search for executables. Adding a permanent path to the “PATH” variable provides easier access to the programs and scripts in that directory. 

When a program is added to PATH, users can execute it from any system directory without specifying its full path. This can save time and make it easier to use and manage programs, especially if you use the same programs frequently.

This guide will illustrate different methods to add a path to PATH in Ubuntu permanently.

Method 1: Modifying .bashrc File

The “.bashrc” file is a configuration file to the bash shell. The file is executed every time a new terminal session is started. The $PATH variable in the .bashrc file is utilized to define the directories that the shell explores for executable files. To add a permanent path to the “PATH” variable, open the “.bashrc” file in a text editor with the “sudo” privileges:

$ sudo nano .bashrc

Add your path as follows and paste the line in the “.bashrc” file:

$ export PATH="$PATH:/home/itslinuxfoss/Folder"

Then, save the file and run the following command to make the changes effective:

$ source ~/.bashrc

In this way, the new directory path “/home/itslinuxfoss/Folder” has been permanently added to the PATH in the .bashrc file.

Method 2: Modifying .profile File

You can add a new directory path to “PATH” in the “.profile” file, executed at login for GUI sessions or non-login shells. The “.bashrc file” is specific to interactive shell sessions, while the “.profile” file is executed for login shells. For this, open the “.profile” file in the text editor with the “sudo” privilege as below: 

$ sudo nano .profile

To add a path to PATH, open the “.profile” file in a text editor and add the following line at the end:

$ export PATH="$PATH:/home/itslinuxfoss/Folder"

Then, save the file and log out and log back in to make the changes effective.

Method 3: Modifying /etc/environment File

Another method is to add a permanent new directory path to “PATH” in the “/etc/environment” file, which sets system-wide environment variables. 

Open the “environment” file in a nano editor by executing the below script:

$ sudo nano /etc/environment

To add a path to PATH, write the following line at the end of the file:

$ PATH="PATH/home/itslinuxfoss/Folder"

Then, save the file and log out to make the changes effective.

Difference Between the “.bashrc”,“.profile”, and “/etc/environment” Files 

The difference between the above three methods is described as: 

  • The “.bashrc” file is for setting shell-specific environment variables for the current user in interactive shells. 
  • While the “.profile” file is for setting environment variables for the current user in both interactive and login shells. 
  • The “/etc/environment” file sets environment variables for all users system-wide.

Conclusion

Ubuntu offers the “.bashrc”,“.profile”, and “/etc/environment” files to add a path to the PATH environment variable permanently. For the “.bashrc” and “.profile” files, you need to use the “export” command to add the Path. While the user needs to just add the Path directly without any command. This post briefly explained possible ways to add a Path to PATH in Ubuntu permanently.