Generally, the location of the executable file to launch an installed program will be added to the $PATH
in Linux. Hence, you can run the program from anywhere in the shell, without typing the full path of the executable file. However, in some cases, you need to manually add a program's installation location to the $PATH
. In this brief tutorial, we will see how to add a directory to PATH in Linux operating systems.
Table of Contents
Why Should We Add a Directory to the $PATH?
The other day I was testing a program named Macchina, which is written in Rust programming language. I installed Rust using conda
package manager in my Ubuntu system, and then installed Macchina using Rust's cargo
package manager.
When I tried to launch the Macchina program, the output said that the program is not installed. Again, I tried to install it and got the following error message:
Updating crates.io index Ignored package `macchina v0.5.9` is already installed, use --force to override warning: be sure to add `/home/sk/.cargo/bin` to your PATH to be able to run the installed binaries
As you might already know, when we install a program that is written in Rust, the executable binary files will be saved under Cargo's bin directory (i.e. ~/.cargo/bin
).
ls ~/.cargo/bin/
macchina
As you can see, the Macchina binary file is kept in the cargo bin directory. After installing Rust, I should have added this directory to my $PATH, but I forgot. Hence the above problem!
If I installed Rust using the rustup
installer script, I wouldn't have encountered this issue. Because the rustup script will automatically add Cargo's bin directory to the $PATH
environment variable by modifying the profile file located at ~/.profile.
But in this case, I installed Rust inside a conda
environment, and the cargo bin directory is not added to the PATH.
List Environment Variables in $PATH
Let us list all Environment variables in our $PATH
using echo
command.
echo $PATH
Sample output:
/home/sk/anaconda3/envs/rustenv/bin:/home/sk/anaconda3/condabin:/home/sk/.nvm/versions/node/v15.0.1/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin
As you can see in the above output, the directory ~/.cargo/bin/
is not available in the $PATH, hence the program Macchina can not be launched using its name. I can, however, launch the program by typing its full path like below:
~/.cargo/bin/macchina
In order to run a program using its name from any location, we need to add it to PATH as shown in the following section.
Add a Directory to PATH in Linux
To add a directory, for example /home/sk/.cargo/bin/
, in the $PATH, run:
export PATH=/home/sk/.cargo/bin:$PATH
Please mind the colon (:) at the end of the directory's path.
Now list again the environment variables using echo
command:
echo $PATH
Sample output:
/home/sk/.cargo/bin:/home/sk/anaconda3/envs/rustenv/bin:/home/sk/anaconda3/condabin:/home/sk/.nvm/versions/node/v15.0.1/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin
See? The ~/.cargo/bin
directory has been added to the $PATH
. From now on, I can launch any program installed in this directory by simply calling program's name. No need to mention full path!
Please note that this is temporary. Once you exit from the current session, the environment variable will be gone.
To make the changes permanent, edit ~/.bashrc
file:
nano ~/.bashrc
Add the following line at the end:
export PATH=/home/sk/.cargo/bin:$PATH
Press CTRL+O
followed by CTRL+X
to save the file and exit.
Run the following command to take the changes into effect immediately:
source ~/.bashrc
If you want to do it system-wide, add the same line to /etc/profile
file.
Hope this helps.
3 comments
Thx very much. It was a good reminder.
this was very elaborate. thank you
Really clearcut and helpful. Thank you!