Add / Remove a Folder to $PATH variable in Ubuntu 22.04

Want to configure your $PATH variable? Here’s the beginner’s guide shows how to do the trick in Ubuntu, though it works in most Linux systems.

Why adding custom folders to $PATH

PATH is an environment variable. It tells which folders to look for when trying to run executable file from command line.

Say you downloaded an executable script from the web, and saved it in ‘Downloads‘ folder. Before adding to $PATH, you need to either navigate to the folder that contains the file, and run it in terminal via:

cd ~/Downloads && ./executable-file

Or type the full path to file every time to start the script:

~/Downloads/executable-file

After moving the script into one of $PATH folders, such as /usr/bin, /usr/local/bin, ~/.loca/bin/, you do NOT have to type so much anymore, just input the filename in terminal, and hit Enter. Linux will find it from $PATH folders, and run it if exist, or output command not found if can’t find it.

executable-file

However, some apps have executable file as well as required run-time libraries in same folder. Moving the whole folder into one of $PATH folders will NOT work, because Linux does not find executable from sub-folders in $PATH.

Or, your project is still in developing stage. You want to try it out easily without installing into a PATH folder. So, add the working directory as $PATH variable is helpful.

Which file to edit: /etc/environment, /etc/profile, .bash_profile, .bash_login, .profile, or .bashrc

There are quite a few places to specify $PATH variable. They include /etc/environment, /etc/profile, scripts under /etc/profile.d, /etc/bash.bashrc, .bash_profile, .bash_login, .profile, and .bashrc.

Here I’m going to show you which file you should use for configuring $PATH variable.

/etc/environment

Ubuntu and its based Linux systems has a line in “/etc/environment” file that specify system wide $PATH variable. It’s:

PATH=”/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin”

Meaning the directories separated via “:” are in path for all users for all the cases.

However, it’s NOT recommended for editing this file to set $PATH variable. Any incorrect setup MAY cause login issues!!

NOT Recommend to Edit

/etc/profile

To add a folder to $PATH variable for all users, “/etc/profile” is the right place for login shell (common login via user password, SSH login, or via su - username).

Instead of manually editing the /etc/profile file, many applications automatically create scripts under “/etc/profile.d” to do the same job.

It however does not work for non-login shell. For example, you log into Linux via user-A, then open terminal and use su user-B to switch to user B. In the case, $PATH settings in ‘/etc/profile’ works for user-A, but NOT for user-B in this terminal. As a workaround, use su – user-B instead.

.bash_profile, .bash_login, .profile

For current user only, there are 3 files to specify $PATH variable for login shell. They are hidden files in user home directory: ‘.bash_profile‘, ‘.bash_login‘, ‘.profile‘.

On user login, it first reads “/etc/profile” (& scripts under /etc/profile.d). Then, tries to:

  • run ‘.bash_profile‘ if exist and skip 2 others.
  • if ‘.bash_profile‘ not exist, use .bash_login if available and skip .profile.
  • take use .profile only when the previous 2 are not exist.
For Ubuntu & its based systems, only .profile exist out-of-box.
Linux environment setting files

/etc/bash.bashrc & .bashrc

The 2 files are used for configuring alias and other bash functions for non-login shell, such as terminal access via su username. While /etc/bash.bashrc is for system wide, .bashrc works for current user only.

However, /etc/profile in most Linux includes rules to run /etc/bash.bashrc automatically. And, .bash_profile, .bash_login, or .profile also tells to run .bashrc automatically.  As a result, the 2 files work in both login and non-login shell.

Add/Remove a folder to $PATH variable

After reading the content above, you should know which file to edit for setting your $PATH.

In short, for login shell:

  • For system wide, edit /etc/profile file or create script under /etc/profile.d.
  • For current user only, edit ~/.bash_profile (if exist), or ~/.bash_login (if former one not exist), or ~/.profile.

For non-login shell (and login shell, because previous files will also run the 2 files below):

  • For system wide, edit /etc/bash.bashrc.
  • For current user only, edit ~/.bashrc.

Edit file and add a folder to $PATH

Now, open a terminal window (press Ctrl+Alt+T on Ubuntu) and run command to edit a config file.

nano ~/.bashrc
Replace ~/.bashrc with one of previous files you want to edit. Also, replace nano with your favorite text editor. And, add sudo for editing files under /etc/

When file opens, scroll to end and add new line or lines:

  • For example, add the “work” folder in user home to $PATH variable, no matter if the folder exists or not:
    export PATH=$HOME/work:$PATH
  • Add “work” folder in user home to $PATH only when it exists.
    if [ -d "$HOME/work" ] ; then
    PATH="$HOME/work:$PATH"
    fi

Verify your $PATH variable

After log out and back in, you may run the command below in terminal to output current $PATH variable:

$PATH

There may be “No such file or directory” that occurs when a folder added to $PATH, but later removed. It does not matter, but can be avoided by using “if” statement when adding to $PATH.

See How to Add folder to PATH via YouTube Video:

Summary

In general, user can edit ‘/etc/profile‘ or add scripts under ‘/etc/profile.d‘ to configure $PATH for system wide.

For current user only, edit ‘~/.bash_profile‘ if exist, or ‘~/.bash_login‘. If non of the 2 exists, edit ‘~/.profile‘.

In case you want to make it work for non-login shell, such as switch to user in terminal via su username, edit ‘/etc/bash.bashrc‘ or ‘~/.bashrc‘ instead.

Hi, I'm Merilyn Ne, a computer geek working on Ubuntu Linux for many years and would like to write useful tips for beginners. Forgive me for language mistakes. I'm not a native speaker of English.