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.
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. So 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.
$ 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 our
$ echo $PATH
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:
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 $PATH
~/.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
$ nano ~/.bashrc
Add the following line at the end:
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
Hope this helps.