If you're new to Linux, you may run into a scenario where you have correctly installed a package, tool or application only to wind up with a message like this when you try to run it:
In many cases, this is because even though the program was installed correctly, the operating system still doesn't know how to locate it to run it. Fortunately, this problem is easily fixed by updating the PATH
environment variable with the location of your newly-installed program.
Prerequisites
To understand and follow this article, you should have basic familiarity with either Bash, Z Shell or a similar shell environment, understand how to navigate around the Linux filesystem, and perform simple command line tasks.
Learning outcomes
After reading this article, you will understand:
What the
PATH
environment variable isHow to update
PATH
and persist the update across sessionsWhat the
~/.bashrc
,~/.profile
, and~/.bash_profile
files are and which to updateWhat the
/etc/skel
directory is and how to replace deleted or over-written user session configuration files
What is PATH
?
PATH
is an environment variable that tells the operating system all the different directory locations to look in when attempting to run a program binary. It contains a colon-separated list of directory locations or paths that the operating system will check each time you attempt to run a program outside of the program's directory location. You can view your path with the echo
command. (Note: when referencing any variable in a Linux terminal session, including environment variables, prefix the variable with the $
symbol. However, when setting variables you omit this symbol)
Because PATH
already contains all the most common places a program binary would be located, you likely won't need to modify it very often. However, if you are manually installing an application and placing the binary in a location not specified in your system's path, you will need to update PATH
with the path to your newly installed program's directory location.
Where is PATH
set?
Where the PATH
variable is set and therefore where to modify it is a common point of confusion for new Linux users, because PATH
is set in a couple of different locations on the system. Because Linux is a multi-user operating system, many configurations exist at both the global or system level, as well as the individual user level. This allows individual users to set local configurations that override global configurations. PATH
is usually set globally in either /etc/environment
, /etc/profile,
or is built from one or more scripts in /etc/profile.d
sourced by /etc/profile
. Fortunately, it is usually unnecessary and even unadvisable to change global configurations or variables. Instead, additional binary locations can be appended to the globally-set PATH
right in the user's home directory. The simplest way to update PATH
for most users is to run the following commands:
cd ~
echo "PATH=$PATH:/path/to/binary" >> .bashrc
source .bashrc
The first command ensures that you are in your home directory, where the .bashrc
script is located. The second command over-writes the existing PATH
variable with itself appended with your newly installed program binary location, and adds this to the end of the .bashrc
file. Of course, make sure to substitute your program's directory location. It is important to not simply over-write PATH
with your new directory location, but to over-write PATH
with itself plus the new directory location. Otherwise, you would lose all the other locations specified in PATH
. This would mean you would lose the ability to run pretty much all programs outside of the directories where their binaries are located. The final command executes the .bashrc
script, allowing your new PATH
to take effect immediately. This step is optional, as you could achieve the same effect by closing and re-opening your terminal emulator.
The process of updating your PATH
described above is slightly different if you are working on a Linux machine where you only have console access, either at a monitor or over the network through SSH. The ~/.bashrc
script is executed when new terminals are opened up in a graphical environment, but if you're working at a text console then either the ~/.bash_profile
or ~/.profile
script will run when your session begins. In this case, the steps to update your PATH
are almost identical to those previously outlined, except that you would substitute .bashrc
for either .profile
or .bash_profile
. To determine which one your system uses, you can run:
cd ~
ls -lA
What if I over-write or delete a file?
If you're new to Linux or new to working in a text-based terminal, there's a good chance that at some point you will accidentally over-write or even delete one of the files that we have been discussing. This is one reason why it's advisable to stick to modifying configurations in your home directory as much as possible. Accidentally deleting or over-writing a file that contains globally-defined variables or configurations could do serious damage. Fortunately, if you accidentally delete or over-write either ~/.bashrc
, ~/.profile
or ~/.bash_profile
, you can restore a default copy from the /etc/skel
directory. /etc/skel
contains default copies of user session config files that can be easily copied to your home directory so that you can start over if things go wrong. For instance, if you over-write your .bashrc
file, you can get a fresh default by running the following command:
sudo cp /etc/skel/.bashrc ~
Once you copy one of these files to your home directory, you will want to change the user and group ownership of the file to your user and your user's default group. This can be accomplished with the following command:
sudo chown username:groupname .bashrc
Of course, make sure to substitute your username and group in the above command. If you are unsure, your default group will be the same as your username.
Alternatively, you can make quick backups of any configuration file you are about to modify by making a copy of the file and adding an extension that will remind you that the file is a backup. This allows you to save a copy of the file in a known good state. For instance, to make a backup of your .bashrc
file you could run the following command:
# .bak is common but not mandatory. any naming convention will work
cp .bashrc .bashrc.bak
Then, if you accidentally delete the file or things go terribly wrong, you can quickly return to a known good state by restoring the backup:
# if the file still exists but contains too many mistakes, delete it
rm .bashrc
# restore the backup
mv .bashrc.bak .bashrc
Conclusion
And there you have it! You should now understand what the PATH
environment variable is, how to modify it in your home directory, and what to do if you accidentally delete any of the files we have been discussing. I plan to write another tutorial article running through a real-life scenario where you would need to manually install a program binary and update your PATH
, so stay tuned. Thanks for dropping by!