LDSTechForumProjects

Setting environment variables globally in OSX

Setting Global Environment Variables in OSX

In *nix environments you use the .profile or .bash_profile (among others depending upon your shell) to set environment variables. In OSX these environment variables only apply to terminal sessions, not to bundled/GUI applications including those launched from spotlight. You may have properly set JAVA_HOME in your .profile file, and have them working from the command line, only to find them not effective when performing a task from your favorite IDE. For example when I run a maven install on one of my projects I get

Unable to find javadoc command: The environment variable JAVA_HOME is not correctly set

To remedy this, add something like this to the /etc/launchd.conf modified of course to the proper path.

# 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 enviroment variables.
# You also need to surround multiple values in quotes.
#
setenv JAVA_HOME /Library/Java/JavaVirtualMachines/jdk1.7.0_11.jdk/Contents/Home

You can use /usr/libexec/java_home from the command line to determine the proper path, but I could not get that to work embedded in the launchd.conf file like it does in your .bash_profile.

Notes

Contrary to what I've read, .bash_profile is processed even on non-login shells, at least in OSX.

I did not find ~/.MacOSS/environment.plist, nor ~/.launchd.conf nor ~/launchd.conf to be effective. At least not for a terminal session. I wasn't interested in a solution that did not work for both the terminal and applications so I stopped testing there.

Testing was done in Mountain Lion (v10.8.3 build 12D78) on Monday May 13, 2013.


Resources

http://www.dowdandassociates.com/content/howto-set-an-environment-variable-in-mac-os-x-home-slash-dot-macosx-slash-environment-dot-plist/

http://www.digitaledgesw.com/node/31

[http://cumptrnrd.wordpress.com/2012/09/18/setting-environment-variables-in-os-x-10-8-mountainlion/

http://stackoverflow.com/questions/135688/setting-environment-variables-in-os-x

http://stackoverflow.com/questions/415403/whats-the-difference-between-bashrc-bash-profile-and-environment

This page was last modified on 13 May 2013, at 14:14.

Note: Content found in this wiki may not always reflect official Church information. See Terms of Use.