996

How do I add a directory to the $PATH in Ubuntu and make the changes permanent?

αғsнιη
  • 36,350
0xnuminous
  • 11,280

17 Answers17

614

Using ~/.profile to set $PATH

A path set in .bash_profile will only be set in a bash login shell (bash -l). If you put your path in .profile it will be available to your complete desktop session. That means even metacity will use it.

For example ~/.profile:

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

Btw, you can check the PATH variable of a process by looking at its environment in /proc/[pid]/environ (replace [pid] with the number from ps axf). E.g. use grep -z "^PATH" /proc/[pid]/environ

Note:

bash as a login shell doesn't parse .profile if either .bash_profile or .bash_login exists. From man bash :

it looks for ~/.bash_profile, ~/.bash_login, and ~/.profile, in that order, and reads and executes commands from the first one that exists and is readable.

See the answers below for information about .pam_environment, or .bashrc for interactive non-login shells, or set the value globally for all users by putting a script into /etc/profile.d/ or use /etc/X11/Xsession.d/ to affect the display managers session.

wjandrea
  • 14,504
ko-dos
  • 6,288
  • 1
  • 14
  • 3
494

Edit .bashrc in your home directory and add the following line:

export PATH="/path/to/dir:$PATH"

You will need to source your .bashrc or logout/login (or restart the terminal) for the changes to take effect. To source your .bashrc, simply type

$ source ~/.bashrc
wjandrea
  • 14,504
Ophidian
  • 5,073
197

The recommended place to define permanent, system-wide environment variables applying to all users is in:

/etc/environment

(which is where the default PATH is defined)

This will work in desktop or console, gnome-terminal or TTY, rain or shine ;)

  • To edit, open the terminal and type:

    sudoedit /etc/environment
    

    (or open the file using sudo in your favorite text editor)

To make it work without rebooting, run . /etc/environment or source /etc/environment. Since this file is just a simple script it will run and assign the new path to the PATH environment variable. To check run env and see the PATH value in the listing.

Related:

Kulfy
  • 18,154
ish
  • 141,990
69

I think the canonical way in Ubuntu is:

  • create a new file under /etc/profile.d/

     sudo vi /etc/profile.d/SCRIPT_NAME.sh
    
  • add there:

     export PATH="$PATH:YOUR_PATH_WITHOUT_TRAILING_SLASH"
    
  • and give it execute permission

     sudo chmod a+x /etc/profile.d/SCRIPT_NAME.sh
    
Dave Jarvis
  • 991
  • 2
  • 12
  • 26
woto
  • 817
62

For complete newbies (like I am) who are more comfortable with GUI:

  1. Open your $HOME folder.
  2. Go to ViewShow Hidden Files or press Ctrl + H.
  3. Right click on .profile and click on Open With Text Editor.
  4. Scroll to the bottom and add PATH="$PATH:/my/path/foo".
  5. Save.
  6. Log out and log back in to apply changes (let Ubuntu actually load .profile).
Aditya
  • 13,616
dain
  • 737
48

For persistent environment variables available to particular users only. I highly recommend Ubuntu official documentation.

https://help.ubuntu.com/community/EnvironmentVariables

Referring to documentation above, I have setup my Android SDK path-tools by:

  1. creating ~/.pam_environment file in home directory.
  2. the content of which is PATH DEFAULT=${PATH}:~/android-sdk-linux/tools.
  3. additional custom user path can be added by separating paths with colon (:).
  4. this requires re-login, which means you need to log-out and log-in back to desktop environment.
27

Put that line in your ~/.bashrc file.

It gets sourced whenever you open a terminal

EDIT: Based on the comments below, for a more general setting that will apply to all shells (including when you hit Alt-F2 in Unity), add the line to your ~/.profile file. Probably shouldn't do both however, as the path will be added twice to your PATH environment if you open a terminal.

Severus Tux
  • 10,126
Ian B.
  • 3,533
  • 22
  • 25
18

Adding it to .bashrc will work but I think the more traditional way of setting up your path variables is in .bash_profile by adding the following lines.

PATH=$PATH:/my/path/foo
export PATH

According to this thread it appears as though Ubuntu's behavior is slightly different than RedHat and clones.

17

To set it system wide, append the line export PATH=/path/you're/adding:$PATH to the end of /etc/profile.

To add the directory for only the logged-in user, append the same line to ~/.bash_profile.

12

In terminal, cd to the_directory_you_want_to_add_in_the_path

echo "export PATH=$(pwd):\${PATH}" >> ~/.bashrc

This wasn't my idea. I found this way to export path at this blog here.

9
sudo vi /etc/profile.d/SCRIPT_NAME.sh

add there

export PATH=YOUR_PATH_WITHOUT_TRAILING_SLASH:$PATH
Jorge Castro
  • 73,717
8

The recommended way to edit your PATH is from /etc/environment file

Example output of /etc/environment:

PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games"

For example, to add the new path of /home/username/mydir

PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/home/username/mydir"

Then, reboot your PC.


System-wide environment variables

A suitable file for environment variable settings that affect the system as a whole (rather than just a particular user) is /etc/environment. An alternative is to create a file for the purpose in the /etc/profile.d directory.

/etc/environment

This file is specifically meant for system-wide environment variable settings. It is not a script file, but rather consists of assignment expressions, one per line.

Note: Variable expansion does not work in /etc/environment.

More info can be found here: EnvironmentVariables

Benny
  • 5,100
7

Whenever I "install" my folder of BASH scripts, I follow the pattern of the test for a $HOME/bin folder that's in most .profile files in recent versions of Ubuntu. I set a test that looks like

if [ -d "/usr/scripts" ]; then
   PATH="/usr/scripts:$PATH"
fi

It works just about 100% of the time, and leaves me free to change it in a GUI text editor with a quick "Replace all" should I ever decide to move /scripts somewhere closer to my $HOME folder. I haven't done so in 6 Ubuntu installs, but there's "always tomorrow." S

BZT

Shashanth
  • 463
6

Open your terminal, type gedit .profile and insert the following:

# set PATH so it includes user's private bin if it exists
if [ -d "$HOME/bin" ] ; then
    PATH="$PATH:$HOME/bin"
fi

 #the following line add Bin where you dont have a Bin folder on your $HOME
    PATH="$PATH:/home/mongo/Documents/mongodb-linux-i686-2.2.2/bin"

Close and open terminal, it should be working.

Eric Carvalho
  • 55,453
djavier
  • 61
4

Even if system scripts do not use this, in any of the cases that one wants to add a path (e.g., $HOME/bin) to the PATH environment variable, one should use

PATH="${PATH:+${PATH}:}$HOME/bin"

for appending (instead of PATH="$PATH:$HOME/bin"), and

PATH="$HOME/bin${PATH:+:${PATH}}"

for prepending (instead of PATH="$HOME/bin:$PATH").

This avoids the spurious leading/trailing colon when $PATH is initially empty, which can have undesired effects.

See e.g. https://unix.stackexchange.com/questions/162891/append-to-path-like-variable-without-creating-leading-colon-if-unset

3

Put it to your ~/.bashrc or whatevershell you use rc (or to beforementioned ~/.profile) AND ~/.xsessionrc so it will also work in X (outside shell).

heemayl
  • 93,925
Aminda
  • 337
0

For Ubuntu edit the ~/.bashrc and add the following line.

. ~/.bash_profile

Then edit your .bash_profile as you need.....

Jorge Castro
  • 73,717