Setting environment variables on OS X Setting environment variables on OS X bash bash

Setting environment variables on OS X


Bruno is right on track. I've done extensive research and if you want to set variables that are available in all GUI applications, 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 (note: this file might not yet exist)

  3. Put contents like the following into the file

    # Set environment variables here so they are available globally to all apps# (and Terminal), including those launched via Spotlight.## After editing this file run the following command from the terminal to update# environment variables globally without needing to reboot.# NOTE: You will still need to restart the relevant application (including# Terminal) to pick up the changes!# grep -E "^setenv" /etc/launchd.conf | xargs -t -L 1 launchctl## See http://www.digitaledgesw.com/node/31# and http://stackoverflow.com/questions/135688/setting-environment-variables-in-os-x/## Note that you must hardcode the paths below, don't use environment variables.# You also need to surround multiple values in quotes, see MAVEN_OPTS example below.#setenv JAVA_VERSION 1.6setenv JAVA_HOME /System/Library/Frameworks/JavaVM.framework/Versions/1.6/Homesetenv GROOVY_HOME /Applications/Dev/groovysetenv GRAILS_HOME /Applications/Dev/grailssetenv NEXUS_HOME /Applications/Dev/nexus/nexus-webappsetenv JRUBY_HOME /Applications/Dev/jrubysetenv ANT_HOME /Applications/Dev/apache-antsetenv ANT_OPTS -Xmx512Msetenv MAVEN_OPTS "-Xmx1024M -XX:MaxPermSize=512m"setenv M2_HOME /Applications/Dev/apache-mavensetenv JMETER_HOME /Applications/Dev/jakarta-jmeter
  4. Save your changes in vi and reboot your Mac. Or use the grep/xargs command which is shown in the code comment above.

  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 IDEA and other GUI applications you launch via Spotlight.


Don't expect ~/.launchd.conf to work

The man page for launchctl says that it never worked:

DEPRECATED AND REMOVED FUNCTIONALITY

launchctl no longer has an interactive mode, nor does it accept commands from stdin. The /etc/launchd.conf file is no longer consulted for subcommands to run during early boot time; this functionality was removed for security considerations. While it was documented that $HOME/.launchd.conf would be consulted prior to setting up a user's session, this functionality was never implemented.

How to set the environment for new processes started by Spotlight (without needing to reboot)

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 PATH /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:/sbinlaunchctl 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.

This includes any shells already running under Terminal.app, although if you're there you can set the environment more directly, e.g. with export PATH=/opt/local/bin:/opt/local/sbin:$PATH for bash or zsh.

How to keeping changes after a reboot

New method (since 10.10 Yosemite)

Use launchctl config user path /bin:/usr/bin:/mystuff. See man launchctl for more information.

Previous method

The launchctl man page quote at the top of this answer says the feature described here (reading /etc/launchd.conf at boot) was removed for security reasons, so ymmv.

To keep changes after a reboot you can set the environment variables from /etc/launchd.conf, like so:

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

launchd.conf is executed automatically when you reboot.

If you want these changes to take effect now, you should use this command to reprocess launchd.conf (thanks @mklement for the tip!)

egrep -v '^\s*#' /etc/launchd.conf | launchctl

You can find out more about launchctl and how it loads launchd.conf with the command man launchctl.


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.