views:

19424

answers:

8

What is the proper way to modify environment variables like PATH in OS X? I've looked on google a little bit and found 3 different files to edit:

  • /etc/paths
  • ~/.profile
  • ~/.tcshrc

I don't even have some of these, and I'm pretty sure that .tcshrc is wrong, since osx uses bash now. Anybody have any idea where these variables, especially PATH, are defined?

Edit: I'm running OS X 10.5

+15  A: 

You set them in

~/.MacOSX/environment.plist

See http://snipurl.com/apple_environment

For PATH in the Terminal, you should be able to set in .bash_profile or .profile (you'll probably have to create it though)

tim_yates
thanks John...just beat me :)
tim_yates
This is only if you actually expect them to be used by graphical apps. Since these don't typically use environment variables, it's not a very good place to set them.
Chris Hanson
There's some very good examples of graphical apps that use environment variables. IntelliJ for example, likes to be able to see M2_HOME to know where Maven lives. To get it to see the variable, you'll need to set it in /etc/launchd.conf instead of environment.plist.
Matthew McCullough
For reference: using `preferences.plist` was less than ideal with OS X 10.5 since at that time `preferences.plist` was not read for applications launched through spotlight, see comment by Louis to Matthew's answer and http://email.esm.psu.edu/pipermail/macosx-emacs/2010-May/002113.html . For OS X 10.6 `environment.plist` works just like it should.
Janus
+3  A: 

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.

John Millikin
+1  A: 

for a single user modification, use ~/.profile of the ones you listed, the following link explains when the different files are read by bash

http://telin.ugent.be/~slippens/drupal/bashrc_and_others

if you want to set the environment variable for gui applications you need the ~/.MacOSX/environment.plist file

mmaibaum
A: 

Google is your friend, so does the official documentation: http://developer.apple.com/documentation/MacOSX/Conceptual/OSX_Technology_Overview/CommandLine/chapter_950_section_4.html

Zsolt Botykai
Setting variables in the environment.plist only provides variables to apps or shells started via a _direct_ invocation from Finder. If started from Spotlight, you'll find it doesn't yield a variable that can be seen in a terminal or other GUI app. Quite odd.
Matthew McCullough
+1  A: 

well, I'm unsure about /etc/paths and ~/.MacOSX/environment.plist those are new.

But with bash, you should know that .bashrc is executed with every new shell invocation and .bash_profile is only executed once at startup. Don't know how often this is with macos, I think the distinction has broken down with the window system launching everything.

Personally, I eliminate the confusion by creating a .bashrc with everything I need and then do:

ln -s .bashrc .bash_profile
mike511
+4  A: 

Sometimes all of the previous answers simply don't work. If you want to have access to a system variable (like M2_HOME) in Eclipse or in IntelliJ the only thing that works for me in this case is:

First (step 1) edit /etc/launchd.conf to contain a line like this: "setenv VAR value" and then (step 2) reboot.

Simply modifying .bash_profile won't work because in osx the applications are not started as in other UNIX'es, they don't inherit the parents shell variables. All the other modifications won't work for a reason that is unknown to me. Maybe someone else can clarify about this.

Bruno Ranschaert
Applications started from Spotlight or by any other means all have /etc/launchd.conf read by their parent process, thus making that an appealing choice for where to set environment variables visible in all apps and shells.
Matthew McCullough
@Bruno: See my answer for another solution, which avoids a reboot - http://stackoverflow.com/questions/135688/setting-environment-variables-in-os-x/3756686#3756686
Matt Curtis
+27  A: 

Bruno is right on track. I've done extensive research and if you want to set variables that are available in all GUI apps, your only option is /etc/launchd.conf

Please note that environment.plist does not work for applications launched via Spotlight. This is documented by Steve Sexton here.

1) Open a terminal prompt

2) Type sudo vi /etc/launchd.conf

3) Put contents like the following into the file

    setenv JAVA_VERSION 1.6
    setenv JAVA_HOME /System/Library/Frameworks/JavaVM.framework/Versions/1.6/Home
    setenv GROOVY_HOME /Applications/Dev/groovy
    setenv GRAILS_HOME /Applications/Dev/grails
    setenv NEXUS_HOME /Applications/Dev/nexus/nexus-webapp
    setenv JRUBY_HOME /Applications/Dev/jruby

    setenv ANT_HOME /Applications/Dev/apache-ant
    setenv ANT_OPTS -Xmx512M

    setenv MAVEN_OPTS -Xmx1024M
    setenv M2_HOME /Applications/Dev/apache-maven

    setenv JMETER_HOME /Applications/Dev/jakarta-jmeter

4) Save your changes in VI and reboot your Mac.

5) Prove that your variables are working by opening a Terminal window and typing export and you should see your new variables. These will also be available in IntelliJ and other GUI apps you launch via Spotlight.

Matthew McCullough
So you're saying the right answer is wrong? I have no had any success with environment.plist.
Yar
I'm saying that the accepted answer (environment.plist) has not been successful for me. I've successfully used the launchd.conf approach on 10.5 and 10.6 on four machines.
Matthew McCullough
Is there any way of doing this **without** doing a **system reboot**?
Sorin Sbarnea
Not that I'm aware of. I don't know of any "reload" type of command that causes this to be re-read. A reboot seems to always be necessary.
Matthew McCullough
I also had to use /etc/launchd.conf and reboot for it to take affect.
David Collie
The limitation mentioned above applies to MacOS X 10.5.However MacOS X 10.6 does not have this limitation anymore and setting the values inside environment.plist works fine even for apps launched via spotlight. So the selected answer is correct for Snow Leopard ;-)
Louis Jacomet
Setting `launchd.conf` is one way, but needs a reboot (to restart launchd). If you want to avoid a reboot, see my answer http://stackoverflow.com/questions/135688/setting-environment-variables-in-os-x/3756686#3756686
Matt Curtis
+4  A: 

You can set the environment used by launchd (and, by extension, anything started from Spotlight) with launchctl setenv. For example to set the path:

launchctl setenv /opt/local/bin:/opt/local/sbin:/usr/bin:/bin:/usr/sbin:/sbin

Or if you want to set up your path in .bashrc or similar, then have it mirrored in launchd:

PATH=/opt/local/bin:/opt/local/sbin:/usr/bin:/bin:/usr/sbin:/sbin
launchctl setenv PATH $PATH

There's no need to reboot (though you will need to restart an app if you want it to pick up the changed environment.)

Matt Curtis
Very nice one! The advantage of using environment.plist though seems to be that OS X honours the contents of that files without the hassle of having to fire up a terminal first. Anyway, I think your answer mainly concentrated on avoiding the necessity of a reboot, so thx for that.
kapuzineralex
@kapuzineralex Yes it avoids a reboot, also it changes the environment for programs started from Spotlight, which `environment.plist` does not do.
Matt Curtis