How to Add PATH Variables in Ubuntu 24.04

Last updated: November 4, 2024 — Leave a comment

This tutorial shows how to configure PATH environment variables. Though title said for Ubuntu 24.04, it works in most Linux systems.

PATH specifies the directories in which the executable files of programs are located on the system. Without knowing or typing the full path to the file, just type the app name in the command line, then your system will look into the path directories and run the first that matches.

For example, type firefox in command line will run /usr/bin/firefox to launch Firefox web browser in Ubuntu, Fedora, and other Linux that use the browser as default.

Common Linux packages (e.g., .deb, .rpm) will mostly install their executable files into the PATH automatically. For portable Linux package and others, user can manually add the app folders, that contain the executable files, into PATH.

Check current PATH Variables

Most Linux by default includes /usr/bin, /usr/local/bin, /usr/sbin, /usr/local/sbin, /usr/games, /usr/local/games directories into PATH. While, third-party apps may add more.

To tell the current PATH variables, open terminal (Ctrl+Alt+T) and run command:

echo $PATH

It will print all the PATH directories separated with colon (:).

Option 1: Add a folder/directory into PATH for temporary use.

For developing or other purpose, you may add a custom folder or directory into PATH that works only for current terminal window or command console.

To add custom folder into PATH, for current terminal only, use either command below:

export PATH="/PATH/TO/CUSTOM/FOLDER:$PATH"

or:

export PATH="$PATH:/PATH/TO/CUSTOM/FOLDER"

For example, I downloaded SweetHome3D tarball and extracted into “MyApps” sub-folder into user home. To add it into PATH, for current terminal only, use command:

export PATH="/home/ji/MyApps/SweetHome3D-7.5:$PATH"

It added that folder into the beginning of PATH.

For choice, the command below will add it to the end.

export PATH="$PATH:/home/ji/MyApps/SweetHome3D-7.5

NOTE: When running a command, it looks into path directories from beginning to end. The first one that matches will be run.

Option 2: Add PATH Variables for Current Users Only

For current user only, just edit one of the below files for adding PATH variables:

  • .bash_profile – usually in use in Fedora, Manjaro Linux.
  • .bash_login
  • .profile – usually in use in Debian, Ubuntu Linux.

In case your system have more than one of them, then edit .bash_profile if exist, or .bash_login, and only use .profile when the previous 2 don’t exist. All the 3 usually tells to run .bashrc, so editing .bashrc also works.

In Debian/Ubuntu, just open home folder, press Ctrl+H to show/hide hidden files, and click edit the .profile file.

When file opens, you’ll see something like this:

if [ -d "$HOME/bin" ] ; then
    PATH="$HOME/bin:$PATH"
fi

It tells to add the bin folder if exist in user home to PATH. There’s also a section to add .local/bin to PATH if exists.

You can add custom folder into user’s PATH, by adding similar lines. For example, use lines below to add SweetHome3D v7.5 under MyApps sub-folder:

if [ -d "$HOME/MyApps/SweetHome3D-7.5" ] ; then
    PATH="$HOME/MyApps/SweetHome3D-7.5:$PATH"
fi

Or, add the line below instead to add that folder to PATH whenever no matter if it exists or not.

export PATH="$HOME/MyApps/SweetHome3D-7.5:$PATH"

To apply the change, you need to log out and back in. However, it does NOT work for non-login shell, e.g., su user_name command.

Option 2: Add PATH Variables system wide for all users

Debian and Ubuntu based systems use /etc/environment file to specify the original system-wide PATH variables. However, it’s NOT recommended to edit the file directly.

Instead, /etc/profile or .sh files under /etc/profile.d/ directory are the correct places for adding system wide PATH variables.

For example, edit the /etc/profile by running command:

sudo gnome-text-editor /etc/profile

Replace gnome-text-editor according to your desktop environment or use nano that works in most Linux. When file open, add new line in end:

export PATH="/usr/lib/myapps/bin:$PATH"

After saving the file (For nano, press Ctrl+S to save then Ctrl+X to exit), /usr/lib/myapps/bin in the case will be added to PATH at next login.

To keep /etc/profile clean, it better to create custom .sh file under /etc/profile.d instead.

For example, run command below to create a custom_path_global.sh file and edit it:

sudo gnome-text-editor /etc/profile.d/custom_path_global.sh

Then, add the lines below will do the same job if the directory exists. And, it will be applied in next login:

# Add custom PATH variables work for all users
if [ -d "/usr/lib/myapps/bin" ] ; then
    PATH="/usr/lib/myapps/bin:$PATH"
fi

Summary

In the tutorial, I’ve taught how to add custom folder or directory into PATH variables in Ubuntu Linux.

In short, user may run export PATH="/PATH/TO/CUSTOM/FOLDER:$PATH" to add custom folder to PATH that works only in current terminal.

Or, edit .profile for Debian/Ubuntu or .bash_profile for Fedora, and add similar line that works at next login for current user only. While, editing /etc/profile or creating .sh file under /etc/profile.d can do the job system wide for all users.

Twitter

I'm a freelance blogger who started using Ubuntu in 2007 and wishes to share my experiences and some useful tips with Ubuntu beginners and lovers. Please comment to let me know if the tutorial is outdated! And, notify me if you find any typo/grammar/language mistakes. English is not my native language. Contact me via ubuntuhandbook1@gmail.com Buy me a coffee: https://ko-fi.com/ubuntuhandbook1 |

No Comments

Be the first to start the conversation.

Leave a Reply

Text formatting is available via select HTML.

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> 

*