How do I add a directory to the
$PATH in Ubuntu and make the changes permanent?
Using ~/.profile to set $PATH
A path set in
.bash_profile will only be set in a bash login shell (
If you put your path in
.profile it will be available to your complete desktop session. That means even metacity will use it.
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
bash as a login shell doesn't parse
.profile if either
.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
.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.
The recommended place to define permanent, system-wide environment variables applying to all users is in:
(which is where the default
PATH is defined)
This will work in desktop or console, gnome-terminal or TTY, rain or shine ;)
To edit, press Alt+F2 and type:
gksudo gedit /etc/environment
(or open the file using
sudoin your favorite text editor)
To effect changes, run
. /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.
For complete newbies (like I am) who are more comfortable with GUI:
- Open your
- Go to View → Show Hidden Files or press Ctrl + H.
- Right click on
.profileand click on Open With Text Editor.
- Scroll to the bottom and add
- Log out and log back in to apply changes (let Ubuntu actually load
For persistent environment variables available to particular users only. I highly recommend Ubuntu official documentation.
Referring to documentation above, I have setup my Android SDK path-tools by:
~/.pam_environmentfile in home directory.
- the content of which is
- additional custom user path can be added by separating paths with colon (:).
- this requires re-login, which means you need to log-out and log-in back to desktop environment.
Put that line in your
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.
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.
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
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.
The recommended way to edit your
PATH is from
Example output of
For example, to add the new path of
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.
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
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
for appending (instead of
for prepending (instead of
This avoids the spurious leading/trailing colon when
$PATH is initially empty, which can have undesired effects.
protected by heemayl Aug 21 '15 at 22:00
Thank you for your interest in this question.
Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).
Would you like to answer one of these unanswered questions instead?