How to add a directory to the PATH?
Solution 1:
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.
Solution 2:
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
Solution 3:
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:
- EnvironmentVariables - Community Help Wiki
Solution 4:
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="YOUR_PATH_WITHOUT_TRAILING_SLASH:$PATH"
-
and give it execute permission
sudo chmod a+x /etc/profile.d/SCRIPT_NAME.sh