How to Change PATH Permanently on Ubuntu?



On all Linux-based systems, PATH is the name of a crucial environment variable which is used by shell to look for executable files before running any command.

The PATH variable contains a list of directories where different system and user-based programs are available. This variable sometimes needs to be updated to include additional custom directories, to allow running executables from new location without specifying the absolute path of the executable (or first changing to its parent directory).

For example, instead of specifying the absolute path for Python like /usr/bin/python3, you can simply write python3 because /usr/bin is part of the PATH variable. You can check the current value of the PATH variable using echo command.

$ echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin
$

As you can see, there are multiple directories listed in the output and each directory is separated by a ':' (colon) character. When executing any command, these directories are checked by the system from left to right.

In this article, we'll cover how you can modify the PATH variable on an Ubuntu machine though most of this should work for all Linux distributions and even UNIX as well, with some minor changes.

Temporarily Modifying PATH Variable

To add a directory at the beginning of the PATH variable, you can use export command as ?

$ export PATH=/path/to/dir:$PATH

For adding the directory to the end of the PATH variable, you can use export command like:

$ export PATH=$PATH:/path/to/dir

Multiple directories can also be specified together, if needed, ensuring each directory is separated by a ':' (colon) character as shown below:

$ export PATH=/path/to/dir1:$PATH:/path/to/dir2:/path/to/dir3

You can also remove one or more directories from the PATH variable. Make sure you follow the syntax and do not remove default or system directories.

Verify the changes using echo command. It should show the modified value of the PATH variable in output.

$ echo $PATH
/path/to/dir1:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:/path/to/dir2:/path/to/dir3
$


This method works but is only able to persist these changes for one session. As soon as you close your session (by closing the terminal or logging-out/rebooting the system), all the changes done using export command will be lost.

In your next session, the PATH variable will be reset to its original value. This is because it is initialized, along with other environment variables, from one of your shell's local configuration files at the start of your shell session.

Next, we'll take a look at how the PATH variable can be modified permanently.

Permanently Modifying PATH Variable

Depending on the shell you're using on your system, you would need to update the PATH variable in the respective configuration file as shown below:

Shell Configuration File
bash ~/.bashrc
zsh ~/.zshrc
ksh ~/.kshrc
csh ~/.cshrc

We'll show you how this can be done for bash shell. Open the ~/.bashrc file for editing using vi or nano tool and add/modify the PATH variable with new directory path, following the syntax (use a ':' colon character to separate each directory).

$ vi ~/.bashrc

Your shell configuration file should have the modified PATH variable as shown below:


Restart your session by closing your terminal and re-opening it again. You should be able to see the modified PATH variable's value using echo command.

$ echo $PATH
/path/to/dir1:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:/path/to/dir2:/path/to/dir3
$

This change is persistent for the user whose configuration file is modified. To modify the PATH variable for multiple users, you would need to update this configuration for all such users. Or you can modify it at the system level using the steps shown in the next section.

Modifying PATH Variable for Whole System

To modify the PATH variable for the whole system affecting every user, you can do it via /etc/environment configuration file. This change affects the whole system. As such, ensure to verify the syntax and changes before saving it.

Ensure to create a backup copy of this file, just in case you need to roll back the changes.

$ sudo cp /etc/environment /etc/bkp-environment

This file needs to be edited as superuser (root), so use vi or nano with sudo command to edit it.

$ sudo vi /etc/environment

Update the PATH variable with the new directory and save the configuration file.


Now, start a new session (or restart existing one) for any user and then use echo command to verify the value of modified PATH variable.

$ echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:/path/to/executable
$

Conclusion

In Linux systems, the PATH variable plays an important role, especially in command-line use. Instead of typing complete paths of every command to be executed, it makes the life easy by allowing to run common system and user programs from anywhere.

Most programs modify the PATH variables during the installation process themselves. But in cases where it is not modified or you want to add your custom file location, you now know the process to do it.

Updated on: 2025-01-02T10:19:49+05:30

149 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements