How to Add a Directory to PATH in Ubuntu

This guide will show you how to add directories to the PATH environment variable in Ubuntu.

Ubuntu add to path

On Linux-based systems such as Ubuntu, the PATH environment variable is used by the system to search for commands.

For example, when you type in a command like “nano”, Ubuntu will search any directory listed in the PATH variable for that program.

If the PATH environment variable didn’t exist, you would have to type in the full path to every file you want to run.

It is possible to add directories temporarily or permanently to Ubuntu’s PATH variable. By adding a new directory, Ubuntu will check it for any new binary files the next time you run a command.

For the following steps, you will need to be using the terminal on Ubuntu. If you are using a desktop variant of Ubuntu, you can open the terminal by pressing CTRL + ALT + T.

Viewing the contents of the PATH Variable

Before we show you how to add a directory to the PATH variable on Ubuntu, let us start by exploring what exactly is stored in this variable.

Seeing what directories have already been added to the PATH Variable is a straightforward process.

All we need to do is use the echo command, followed by a reference to the PATH variable. We reference this variable by including the dollar sign symbol ($) in front of it.

echo $PATH

By using echo, we can print the contents of the PATH variable without the terminal trying to execute the values stored within it.

Running this on our Ubuntu system, you can see that the following directories were configured. Each directory is separated by the use of the colon symbol (:).

/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin

Adding a Directory to PATH on Ubuntu Temporarily

The process is straightforward if you only need a directory to be added to the PATH variable on Ubuntu temporarily.

Please note that following these steps, your changes to PATH will only affect the current terminal session. Any new sessions will have the variable reset back to its default value.

To temporarily add a new directory to the PATH variable, you will want to use the following syntax. First, make sure you replace “NEWPATH” with the path you want to be added.

export PATH="NEWPATH:$PATH"

You will notice that we reference “$PATH” when we set the value of the PATH variable again. We do this to ensure that the existing values are copied into the new variable.

Example of Temporarily Adding a Directory to PATH

We will temporarily add the directory “/example/bin” to Ubuntu’s PATH variable to showcase how this works in action.

export PATH="/example/bin:$PATH"

After running the above command, we can then use the echo to see the updated value of the PATH environment variable.

echo $PATH

With this updated value, you can see that it now starts our newly added directory. So next time you run a command, Ubuntu will check this path.

/example/bin/:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin

Permanently Adding a Directory to PATH on Ubuntu

This section will show you how to add a directory to the PATH variable on Ubuntu permanently.

To add a value permanently to PATH on Ubuntu, you will need to modify either the “/etc/profile” or “~/.profile” files.

  • /etc/profile – The profile file within the “/etc/” directory allows you to modify system-wide environment variables for logged-in users.
  • ~/.profile – The “.profile” file allows you to change the PATH variable for a specific user.

Choose whichever file best suits your needs. For our example, we will be modifying the “/etc/profile” file to add a directory to Ubuntu’s PATH list.

1. Let us start by modifying the “/etc/profile” file so that we can make changes to the PATH variable on Ubuntu for all users.

You can modify this file by using the following command within the terminal.

sudo nano /etc/profile

We are using the nano text editor as we find it to be one of the simplest to use within

2. To the bottom of this file, you will want to add the following line. Make sure you replace “MYPATH” with the directory you want to be added to the PATH variable.

You can add multiple additional paths if you want. Just make sure you separate them using a colon (:).

export PATH="MYPATH:$PATH"

For example, a line that added the path “/example/bin” to the PATH variable would look like the following.

export PATH="/example/bin:$PATH"

3. Once you have added your changes to the bottom of the file, you can save and quit by pressing CTRL + X, followed by Y, and then ENTER.

4. Changes to the “/etc/profile” and “~/.profile” files will not take effect immediately.

You will need to either log out or reboot Ubuntu for these changes to be applied to all users.

5. You can verify that your changes to Ubuntu’s PATH variable have worked using the command below.

echo $PATH

You should see the directory you added listed at the front if everything has worked correctly.

Conclusion

By now, you should have a good idea of how to add to the PATH variable on Ubuntu.

The PATH variable is a crucial part of systems like Ubuntu as it helps the system easily find applications.

Please comment below if you have any questions about adding to the PATH environment variable.

Be sure to check out our many other Ubuntu guides to learn more about the system. We also have a wealth of guides that cover the many Linux commands.

Leave a Reply

Your email address will not be published. Required fields are marked *