Adding a Directory to the PATH Variable on Linux

In this tutorial, we will show you how to add a directory to the PATH variable on a Linux system.

Linux Add Directory to PATH Variable

Most operating systems, including Windows, have an environment variable called “PATH.” This variable gives the operating system a list of directories it should search when you type a command into the terminal.

For example, if you use the ping command from within the terminal, your system will check the path directory and search each folder for an executable called “ping.” This helps save you from having to type in the full path to a program every time you want to run it. It is much easier to type “ping” rather than remembering and typing out the full path like: “/usr/bin/ping.”

If you have set up a location where you have set up certain executables, you may want to add it to your Linux systems PATH variable. Adding additional directories to the path variable on Linux is a relatively straightforward process, even for beginners.

Directories can be temporarily or permanently added to your Linux system’s PATH environment variable. A temporary one will only exist in the current terminal session, while a permanent one will apply to all subsequent sessions.

Checking the PATH Variable on Linux

Before you begin modifying and adding to the PATH environment variable on Linux, you will probably want to know what it already contains.

Most Linux operating systems have a key set of directories already defined within this variable. A common one is the “/usr/bin” directory.

Luckily for us, Linux makes printing out the values of environment variables a relatively simple process. While you are within the terminal, all we need to do is use the echo command, followed by a reference to the PATH variable. The dollar sign symbol ($) we have placed in front of “PATH” tells the terminal to interpret it as a variable.

echo $PATH

After running the above command, you will see that echo outputs the contents of the PATH variable to the terminal. This should give you an idea of the directories already defined in your Linux system’s PATH variable. It also gives you an idea of how the PATH variable is formatted.

Below, you can see an example of what we got after printing out the variable’s contents.The key thing to see here is that the colon symbol (:) is used to separate individual paths.

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

Temporarily Adding a Directory to the PATH Variable on Linux

Sometimes, you may want to temporarily add a directory to your PATH variable on Linux. Luckily, thanks to the way Linux’s terminal systems work, this process is relatively straightforward.

By temporarily adding a directory to the PATH variable, the changes will only affect the current terminal session. If you open a new one or close your current one, the PATH variable will be the default value, not your updated one.

Temporarily updating this value is useful if there is something you only need easy access to during your current session.

Below is a super easy method for adding a directory to the PATH variable. All you need to do is replace “<PATH>” with the directory you want to add.

With this command, you update the PATH environment variable for the current terminal session. You set this value with your new path and then add it to the original.

If you don’t specify the “$PATH” variable within your new value, all the original paths will be dropped.

export PATH="<PATH>:$PATH"

Example of Adding a Temporarily Adding a Directory

Let us walk you through an example by adding the directory “/opt/linux/bin” to the PATH variable on our Linux system.

All we need to do to add this path is to run the following command in the terminal.

export PATH="/opt/linux/bin:$PATH"

You can verify that your new directory has been added to the PATH environment variable by simply using the echo command to output its value.

echo $PATH

Below, you can see that our updated PATH variable has the “/opt/linux/bin” directory at the start of the list. If we try to run a command now, it will check this folder first.

/opt/linux/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/local/games:/usr/games

How to Permanently Add a Directory to the PATH Variable on Linux

Here is where things get a little bit more complicated, mainly because there are a few different ways to do it. Adding a directory permanently to the PATH variable on Linux requires you to modify one of two files.

While there are a couple of other ways to manage changes to the PATH variable on Linux, these are two of the more common files.

The two files that you may want to modify are “/etc/profile” or “~/.profile“. Before we get crazy far ahead, let us explore which file you will want to edit.

  • /etc/profile: Using this file will mean any changes you make to the PATH variable will affect all other users on your system.
  • ~/.profile: If you want your changes to the PATH variable to affect only a specific user, you should modify their “.profile” file within their home directory.

For the example we are walking you through below, we are choosing to use the “/etc/profile” file to add a directory to our Linux systems PATH variable. If you want to only make this change for your current user, then simply use the “~/.profile” file instead; the syntax remains the same.

Quick Walkthrough of Adding a Directory to PATH

1. To begin this section, we will need to begin to edit the “/etc/profile/” file. This file will allow us to add a directory to the PATH variable for all of your users on your Linux system.

To open this file with the nano text editor, use the following command. We like to use nano as it is a relatively easy editor for new users.

sudo nano /etc/profile

2. With the profile file now open on your system, you will want to add the following line to the bottom. This line is how you can add a new directory to Linux’s PATH variable. While typing out this line, ensure that you replace “<PATH>” with your directory.

If you want to add more than one directory to the path variable, you must separate them using the colon symbol (:).

export PATH="<PATH>:$PATH"

For example, if we want to add the “/opt/linux/bin” directory to the PATH variable, we add the following to the bottom of the file.

export PATH="/opt/linux/bin:$PATH"

3. Once you have finished adding your changes, save and quit by pressing CTRL + X, Y, and then ENTER.

4. Changes to the profile don’t usually take effect until you either log out or reboot your Linux system.

If you chose only to edit the profile file for your current user, you can update the variable for your current session using the following command. However, it is typically better to restart your system to ensure the changes affect all programs on your system.

source ~/.profile

5. You can verify that you have successfully permanently added a directory to your Linux system’s PATH variable by running the following command.

echo $PATH

If everything has worked as it should, you should see your newly added directories at the front of the variable.

/opt/linux/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/local/games:/usr/games

Conclusion

At this point in the tutorial, you should know how to add a directory to the PATH environment variable on Linux.

Adding folders to this variable is a super handy thing to know how to do. Luckily Linux makes the process of changing the PATH variable a relatively easy process.

Please feel free to comment below if you have any questions about updating this variable.

If you like this tutorial, we highly recommend you check out our many other Linux tutorials.

Leave a Reply

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