How do I add a directory to the $PATH in Ubuntu and make the changes permanent?
-
5help.ubuntu.com/community/EnvironmentVariables There is all you need to know. I found out that a lot of the input here was incorrect or at least the method was not suggested. This is a great piece of information that will let you figure out where to modify your environment variable based on the reason you are doing it and exactly how to do it without screwing everything up (like I did following some of the aforementioned bad advice). So long, and thanks for all the fish!Bus42– Bus422016-06-12 20:30:05 +00:00Commented Jun 12, 2016 at 20:30
17 Answers
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.
-
5Cool, 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.0xnuminous– 0xnuminous2009-07-22 22:13:00 +00:00Commented Jul 22, 2009 at 22:13
-
8On Xbunutu .profile isn't be executed so I put it in .bashrc and it works.tekumara– tekumara2012-08-25 22:21:11 +00:00Commented Aug 25, 2012 at 22:21
-
16This piece of documentation is very well done: Official documentation about environment variable. Consider reading it (not to say that is updated to the last version of the rules to add values to environment variable).Michele– Michele2013-05-23 13:38:01 +00:00Commented May 23, 2013 at 13:38
-
4I've still got no idea where to add my extra path part to. I need to add the android SDK to my path...
PATH="$HOME/bin:$PATH"So I add it to it?Jamie Hutber– Jamie Hutber2016-04-25 14:37:30 +00:00Commented Apr 25, 2016 at 14:37 -
4Keep in mind .profile is used on login and thus you have to logout-login again for it to be used (closing and reopening the terminal is not enough). @JamieHutber the path is
$WHERE_YOU_INSTALLED_THE_SDK/platform-toolsand$WHERE_YOU_INSTALLED_THE_SDK/toolsDaniele Segato– Daniele Segato2016-09-01 08:36:42 +00:00Commented Sep 1, 2016 at 8:36
To only affect interactive Bash shells for your user
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
-
4How do you "source your
.bashrc"? How do you "restart the terminal"?isomorphismes– isomorphismes2011-09-10 01:16:13 +00:00Commented Sep 10, 2011 at 1:16 -
6In bash it is simply '. .bashrc'Ophidian– Ophidian2011-09-12 02:54:12 +00:00Commented Sep 12, 2011 at 2:54
-
1I was making the assumption that you were in your home directory. since that's where the .bashrc you want to edit is.Ophidian– Ophidian2012-02-16 14:23:45 +00:00Commented Feb 16, 2012 at 14:23
-
33
.bashrcis not the right place for setting environment variables. They should go in.profileor.pam_environment. See mywiki.wooledge.org/DotFilesgeirha– geirha2012-03-02 12:21:08 +00:00Commented Mar 2, 2012 at 12:21 -
4@LaoTzu
. .bashrcnot.bashrc:) orsource .bashrcfor that matterMarkus Hedlund– Markus Hedlund2012-08-21 08:26:38 +00:00Commented Aug 21, 2012 at 8:26
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
sudoin 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:
-
12and then you need to reboot for changes to take effect...Lee– Lee2013-11-17 09:27:12 +00:00Commented Nov 17, 2013 at 9:27
-
3This is exactly what I needed. Provisioning a throw-away vm image via vagrant and needed to add node and npm to the path.Austin Pray– Austin Pray2014-06-30 03:07:16 +00:00Commented Jun 30, 2014 at 3:07
-
12To take changes in effect run . /etc/environement (yes, dot, a space and /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.WindRider– WindRider2015-05-20 13:27:29 +00:00Commented May 20, 2015 at 13:27
-
3I needed to run
source /etc/environmentto reload the changesJohnnyAW– JohnnyAW2018-05-16 08:54:44 +00:00Commented May 16, 2018 at 8:54 -
2@JohnnyAW: source is equivalent to the initial dot, see for example en.wikipedia.org/wiki/Source_(command).Roland Sarrazin– Roland Sarrazin2019-01-11 15:13:26 +00:00Commented Jan 11, 2019 at 15:13
I think the canonical way in Ubuntu is:
create a new file under
/etc/profile.d/sudo vi /etc/profile.d/SCRIPT_NAME.shadd there:
export PATH="$PATH:YOUR_PATH_WITHOUT_TRAILING_SLASH"and give it execute permission
sudo chmod a+x /etc/profile.d/SCRIPT_NAME.sh
-
31It is usually safer to add your custom path to the end of PATH instead of the beginning. This avoids accidentally replacing system commands with your programs (or someone else's malicious programs). This also avoids a lot of confusion when someone else works on your system (or gives you advice) and they get unexpected results from commands you have "replaced".Joe– Joe2013-02-07 16:37:06 +00:00Commented Feb 7, 2013 at 16:37
-
It works in my case! I
pip install cmakebut I get "WARNING: The scripts cmake, cpack and ctest are installed in '/home/q/.local/bin' which is not on PATH." So I do your script withexport PATH="$PATH:/home/q/.local/bin"After reboot it works ok.Andrew Ternity– Andrew Ternity2023-12-01 02:44:37 +00:00Commented Dec 1, 2023 at 2:44
For complete newbies (like I am) who are more comfortable with GUI:
- Open your
$HOMEfolder. - 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
PATH="$PATH:/my/path/foo". - Save.
- Log out and log back in to apply changes (let Ubuntu actually load
.profile).
-
5Editing the .profile file is not recommended anymore.You can still use this method to edit the file .pam_environment see: help.ubuntu.com/community/EnvironmentVariablesPulsarBlow– PulsarBlow2013-05-19 04:20:58 +00:00Commented May 19, 2013 at 4:20
-
Thank @PulsarBlow! I'm not really sure what's exactly the difference and the benefit though... This is the direct URL to the relevant section: help.ubuntu.com/community/…dain– dain2013-05-20 12:22:00 +00:00Commented May 20, 2013 at 12:22
-
3This answer caused my system to stop logging in due to all paths being overridden. Using Ubuntu 16.04.Frisbetarian-Support Palestine– Frisbetarian-Support Palestine2017-03-02 11:27:00 +00:00Commented Mar 2, 2017 at 11:27
-
4@Frisbetarian you have to make sure to add the
$PATH:bit which includes the existing PATH definitiondain– dain2017-03-10 05:07:48 +00:00Commented Mar 10, 2017 at 5:07 -
2home folder means not the one named home, but the one you go into when you type "cd ~" in terminalAseem– Aseem2019-03-26 07:13:12 +00:00Commented Mar 26, 2019 at 7:13
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:
- creating
~/.pam_environmentfile in home directory. - the content of which is
PATH DEFAULT=${PATH}:~/android-sdk-linux/tools. - 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 ~/.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.
-
2Actually, I thought you set the path in either
$HOME/.profilefor personal settings, or/etc/profilefor all users. But if it's only needed for bash, I suppose either will work.Marty Fried– Marty Fried2012-07-31 01:37:44 +00:00Commented Jul 31, 2012 at 1:37 -
2If you set it in
~/.bashrc, it'll only be available in the terminals you open. E.g. if you hit Alt+F2 and try to run a command from that dir, it won't find it. If you set it in~/.profileor~/.pam_environment, the gnome session (or whichever DE you use) will inherit it. Appending PATH in~/.bashrcalso has the drawback that if you open/exec bash interactively from another interactive bash shell, it'll be appended multiple times.geirha– geirha2012-07-31 04:58:52 +00:00Commented Jul 31, 2012 at 4:58 -
3I haven't really looked into this for a while, so I did a search, and it seems that there are at least 95 different ways to set the path, most of which are discussed here. I never figured out which one is best. I think
~/.profileis correct for personal paths, though; that's where Ubuntu adds the~/bindirectory. And I confess that I exaggerated a slight bit on the number of ways - just a little.Marty Fried– Marty Fried2012-07-31 05:02:48 +00:00Commented Jul 31, 2012 at 5:02 -
2@MartyFried, yes, to quote the bot in #bash on freenode: «The overwhelming majority of bash scripts, code, tutorials, and guides on the Internet are crap. Sturgeon was an optimist.» Using google for bash problem, you'll often find a lot of half-working solutions before you find a good one. Oh and I'd go with
~/.profilein this case too.geirha– geirha2012-07-31 05:14:51 +00:00Commented Jul 31, 2012 at 5:14 -
3@geirha - I agree that most guides on the internet in general are probably crap, especially anything linux since different distros, or even different versions of the same one, do things differently. It usually boils down to what works, but most people don't realize that what works is simply what works, not necessarily what's right or even what will always work. I try to figure out which of the many ways is actually correct, because I hate doing things more than once - but it's not always easy. :)Marty Fried– Marty Fried2012-07-31 18:50:58 +00:00Commented Jul 31, 2012 at 18:50
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.
-
1I don't have a .bash_profile, Should I create it?0xnuminous– 0xnuminous2009-07-22 21:39:26 +00:00Commented Jul 22, 2009 at 21:39
-
7If you have
.bashrc, stick it in.bashrcinstead. GUI terminals in Ubuntu are not login shells, so.bash_profilewill not be run.koenigdmj– koenigdmj2009-07-22 21:58:26 +00:00Commented Jul 22, 2009 at 21:58 -
1I am not running a gui shell. But from the thread above it looks like the .bashrc will work just fine.0xnuminous– 0xnuminous2009-07-22 22:05:47 +00:00Commented Jul 22, 2009 at 22:05
-
2Both 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– 3dinfluence2009-07-23 02:30:08 +00:00Commented Jul 23, 2009 at 2:30
-
3@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.profileand use that instead. By default, you'll have a.profileand.bashrcin Ubuntu. And.profileis the correct place to set environment variables if we disregard pam_env.geirha– geirha2012-03-02 12:19:12 +00:00Commented Mar 2, 2012 at 12:19
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.
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.
sudo vi /etc/profile.d/SCRIPT_NAME.sh
add there
export PATH=YOUR_PATH_WITHOUT_TRAILING_SLASH:$PATH
-
2
sudo nano /etc/profile.d/SCRIPT_NAME.shis easier for beginners.isomorphismes– isomorphismes2011-09-10 01:22:03 +00:00Commented Sep 10, 2011 at 1:22 -
1For beginners,
gksu gedit /etc/profile.d/SCRIPT_NAME.shis even easier.fouric– fouric2013-03-26 00:04:48 +00:00Commented Mar 26, 2013 at 0:04
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
-
3The lowest answer yet the most correct. This file is usually auto-populated bin Ubuntu with the path.NotoriousPyro– NotoriousPyro2017-10-12 13:33:22 +00:00Commented Oct 12, 2017 at 13:33
-
Indeed this is THE correct answer, not the othersuser1004366– user10043662021-09-11 00:51:33 +00:00Commented Sep 11, 2021 at 0:51
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
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.
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.
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).
For Ubuntu edit the ~/.bashrc and add the following line.
. ~/.bash_profile
Then edit your .bash_profile as you need.....
-
1Downvoted because you didn't explain how to "edit your
.bash_profileas you need". What exactly do I need to do to the.bash_profile?isomorphismes– isomorphismes2011-09-10 01:17:14 +00:00Commented Sep 10, 2011 at 1:17 -
4This is the wrong way.
.profileor.bash_profileshould source.bashrc. Not the other way around.geirha– geirha2012-03-02 12:15:39 +00:00Commented Mar 2, 2012 at 12:15