How to set environment variables to an application on OSX Mountain Lion?

Unfortunately, this seems to be the best option for setting a global environment variable in OS X 10.8.x Mountain Lion:

For temporary environment variables, run this command in Terminal.app, and restart any apps that need to access the variable:

launchctl setenv MYVARIABLE value

To make an environment variable persistent across reboots, create /etc/launchd.conf and add a line like this for each variable, then reboot your entire system:

setenv MYVARIABLE value

This worked for me to set a global environment variable that could be inherited by IntelliJ IDEA CE 12.0 on OS X 10.8.2. Not very elegant, but it works.

Alternatively, you can set the environment variable in Terminal.app, then launch the App from which you want to access the environment variable from the command-line. The launched app will inherit the environment from your terminal session. In Terminal.app, set the environment variable and launch another app with a command like open -a "App Name":

export MYVARIABLE=value
open -a "IntelliJ IDEA 12 CE"

This opens IntelliJ IDEA, and my code can access $MYVARIABLE in its environment.

Leave a Comment