What is the proper way to modify environment variables like PATH in OS X?
I\'ve looked on Google a little bit and found three different files to edit:
One thing to note in addition to the approaches suggested is that, in OS X 10.5 (Leopard) at least, the variables set in launchd.conf
will be merged with the settings made in .profile
. I suppose this is likely to be valid for the settings in ~/.MacOSX/environment.plist
too, but I haven't verified.
Up to and including OS X v10.7 (Lion) you can set them in:
~/.MacOSX/environment.plist
See:
For PATH in the Terminal, you should be able to set in .bash_profile
or .profile
(you'll probably have to create it though)
For OS X v10.8 (Mountain Lion) and beyond you need to use launchd and launchctl.
Here is a very simple way to do what you want. In my case, it was getting Gradle to work (for Android Studio).
Run the following command:
sudo nano /etc/paths
or sudo vim /etc/paths
Enter your password, when prompted.
Open a new terminal window then type:
echo $PATH
You should see the new path appended to the end of the PATH.
I got these details from this post:
Add to the PATH on Mac OS X 10.8 Mountain Lion and up
Any of the Bash startup files -- ~/.bashrc
, ~/.bash_profile
, ~/.profile
. There's also some sort of weird file named ~/.MacOSX/environment.plist
for environment variables in GUI applications.
Another, free, opensource, Mac OS X v10.8 (Mountain Lion) Preference pane/environment.plist solution is EnvPane.
EnvPane's source code available on GitHub. EnvPane looks like it has comparable features to RCEnvironment, however, it seems it can update its stored variables instantly, i.e. without the need for a restart or login, which is welcome.
As stated by the developer:
EnvPane is a preference pane for Mac OS X 10.8 (Mountain Lion) that lets you set environment variables for all programs in both graphical and terminal sessions. Not only does it restore support for ~/.MacOSX/environment.plist in Mountain Lion, it also publishes your changes to the environment immediately, without the need to log out and back in. <SNIP> EnvPane includes (and automatically installs) a launchd agent that runs 1) early after login and 2) whenever the ~/.MacOSX/environment.plist changes. The agent reads ~/.MacOSX/environment.plist and exports the environment variables from that file to the current user's launchd instance via the same API that is used by launchctl setenv and launchctl unsetenv.
Disclaimer: I am in no way related to the developer or his/her project.
P.S. I like the name (sounds like 'Ends Pain').
Do:
vim ~/.bash_profile
The file may not exist (if not, you can just create it).
Type in this and save the file:
export PATH=$PATH:YOUR_PATH_HERE
Run
source ~/.bash_profile