Home Linux Tips & Tricks How To Add A Directory To PATH In Linux

How To Add A Directory To PATH In Linux

By sk
2135 Views

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
Add cargo bin directory to PATH
Add cargo bin directory to PATH

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 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
List Environment variables in PATH in Linux
List Environment variables in PATH in Linux

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
Add a directory to PATH in Linux
Add a directory to PATH in Linux

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
Add the Cargo's bin directory to your profile file
Add the Cargo's bin directory to your profile file

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.

Hope this helps.

You May Also Like

Leave a Comment

* By using this form you agree with the storage and handling of your data by this website.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More