PATH variable contains a set of folder locations that Linux uses to locate applications and packages. Sometimes you may need to add a new directory to PATH variable in Linux. In this article, we will look at how to add directory to PATH in Linux.
How to Add Directory to PATH in Linux
Open terminal and run the following command to view the present value of PATH variable in Linux.
# echo $PATH
The output will look something like
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
Whenever you run a command or utility in Linux, it will look into these folders to find their installation binaries, which are then used to run your command.
Typically, when you install a software, Linux will install it in one of the above directories, or automatically add the installation path of your new software to PATH variable.
In case this does not happen, you may need to manually add this directory to PATH. For example, if Linux shows “command not found” when you run a command after installation, it most likely means that Linux is unable to locate it, since its installation directory is not preset in PATH.
Let us say you have installed a new software at /home/software and want to add this directory to PATH. In such cases, simply run the following command in terminal.
# export PATH = "/home/software:$PATH"
The above command concatenates your new directory to the PATH variable’s value and stores it again in PATH variable. Now if you run the following command to view PATH variable.
# echo $PATH
The output will look something like
/home/software:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
Now you can easily run commands without entering full path to command’s executable file. However, please note, this is only temporary and only valid till the current session. If you start a new session, these changes will not be available.
Generally, Linux looks at /etc/environment and /etc/profile files for PATH variables. If you update PATH variables in any of these files, then it will be automatically updated for all users in your system, permanently.
If you only want to update PATH variable for a particular shell for a specific user, then add your new PATH variable to that shell’s configuration file. For example, we will set PATH variable in ~/.bashrc.
Open file in new terminal.
$ sudo vi ~/.bashrc
Add the following line to it.
export PATH = "/home/software:$PATH"
Save and close the file. Run the following command to apply changes.
$ source ~/.bashrc
That’s it. Now your directory will be permanently added to PATH variable, for bash shell, for current user.
Also read:
How to Update Ubuntu Linux Kernel Version
How to Fix SSH Connection Refused Error
How to Reset Password in Ubuntu
How to Resolve Unmet Dependencies in Ubuntu
How to Execute Shell Command from Python
Sreeram has more than 10 years of experience in web development, Python, Linux, SQL and database programming.