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

link|improve this question

75% accept rate
feedback

migrated from serverfault.com Sep 6 '11 at 7:35

This question came from our site for system administrators and desktop support professionals.

9 Answers

up vote 38 down vote accepted

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)

link|improve this answer
2  
Cool, that worked. I saw where it will auto add the bin dir if I make it so I just used that instead of scripts. TY. – justingrif Jul 22 '09 at 22:13
feedback

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

when in the home directory.

link|improve this answer
1  
How do you "source your .bashrc"? How do you "restart the terminal"? – Lao Tzu Sep 10 '11 at 1:16
1  
In bash it is simply '. .bashrc' – Ophidian Sep 12 '11 at 2:54
1  
$ .bashrc .bashrc: command not found – Lao Tzu Sep 12 '11 at 5:19
I was making the assumption that you were in your home directory. since that's where the .bashrc you want to edit is. – Ophidian Feb 16 at 14:23
.bashrc is not the right place for setting environment variables. They should go in .profile or .pam_environment. See mywiki.wooledge.org/DotFiles – geirha Mar 2 at 12:21
feedback

I think the better way in idealogy of Ubuntu is create new file under /etc/profile.d/

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

add there

export PATH=YOUR_PATH_WITHOUT_TRAILING_SLASH:$PATH

and give right on executable

sudo chmod a+x /etc/profile.d/SCRIPT_NAME.sh

sorry for Bad English.

link|improve this answer
1  
This works great, thanks – qui Apr 26 '11 at 15:57
This is an excellent way of achieving this task. – Boinst Apr 19 at 2:46
feedback

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

  1. Open your HOME folder
  2. Go to View > Show 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)
link|improve this answer
feedback

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.

link|improve this answer
feedback

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.

link|improve this answer
I don't have a .bash_profile, Should I create it? – justingrif Jul 22 '09 at 21:39
4  
If you have .bashrc, stick it in .bashrc instead. GUI terminals in Ubuntu are not login shells, so .bash_profile will not be run. – koenigdmj Jul 22 '09 at 21:58
I am not running a gui shell. But from the thread above it looks like the .bashrc will work just fine. – justingrif Jul 22 '09 at 22:05
2  
Both will work if your shell is a login shell. But I just tried the .bash_profile approach on one of my Ubuntu machines and even after restarting my gnome session it didn't source my .bash_profile. So I would say that putting this in .bashrc is probably the way to go with Ubuntu. – 3dinfluence Jul 23 '09 at 2:30
1  
@justingrif No, you don't need .bash_profile. If bash doesn't find a .bash_profile (when you log in interactively), it will look for .profile and use that instead. By default, you'll have a .profile and .bashrc in Ubuntu. And .profile is the correct place to set environment variables if we disregard pam_env. – geirha Mar 2 at 12:19
feedback
sudo vi /etc/profile.d/SCRIPT_NAME.sh

add there

export PATH=YOUR_PATH_WITHOUT_TRAILING_SLASH:$PATH
link|improve this answer
sudo nano /etc/profile.d/SCRIPT_NAME.sh is easier for beginners. – Lao Tzu Sep 10 '11 at 1:22
feedback

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

link|improve this answer
feedback

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

. ~/.bash_profile

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

link|improve this answer
Downvoted because you didn't explain how to "edit your .bash_profile as you need". What exactly do I need to do to the .bash_profile? – Lao Tzu Sep 10 '11 at 1:17
This is the wrong way. .profile or .bash_profile should source .bashrc. Not the other way around. – geirha Mar 2 at 12:15
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.