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:
Just did this really easy and quick. First create a ~/.bash_profile from terminal:
touch .bash_profile
then
open -a TextEdit.app .bash_profile
add
export TOMCAT_HOME=/Library/Tomcat/Home
save documement and you are done.
After chasing the Environment Variables preference pane and discovering that the link is broken and a search on Apple's site seems to indicate they've forgotten about it... I started back onto the trail of the elusive launchd process.
On my system (Mac OS X 10.6.8) it appears that variables defined in environment.plist are being reliably exported to apps launched from Spotlight (via launchd). My trouble is that those vars are not being exported to new bash sessions in Terminal. I.e. I have the opposite problem as portrayed here.
NOTE: environment.plist looks like JSON, not XML, as described previously
I was able to get Spotlight apps to see the vars by editing ~/MacOSX/environment.plist and I was able to force the same vars into a new Terminal session by adding the following to my .profile file:
eval $(launchctl export)
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 IDEA 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 OS X the applications are not started as in other Unix'es; they don't inherit the parent's shell variables. All the other modifications won't work for a reason that is unknown to me. Maybe someone else can clarify about this.
/etc/launchd.conf is not used in OS X v10.10 (Yosemite), OS X v10.11 (El Capitan), macOS v10.12 (Sierra), or macOS v10.13 (High Sierra).
From the launchctl
man page:
/etc/launchd.conf file is no longer consulted for subcommands to run during early boot time;
this functionality was removed for security considerations.
The method described in this Ask Different answer works for me (after a reboot): applications launched from the Dock or from Spotlight inherit environment variables that I set in ~/Library/LaunchAgents/my.startup.plist
. (In my case, I needed to set LANG
, to en_US.UTF-8
, for a Sublime Text plugin.)
It's quite simple. Edit file .profile
(vi, nano, Sublime Text or other text editor) file. You can found it at the ~/
directory (user directory) and set like this:
export MY_VAR=[your value here]
Example with Java home:
export JAVA_HOME=/Library/Java/JavaVirtualMachines/current
Save it and return to the terminal.
You can reload it with:
source .profile
Or close and open your terminal window.
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.
Open a terminal prompt
Type sudo vi /etc/launchd.conf
(note: this file might not yet exist)
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.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 -XX:MaxPermSize=512m"
setenv M2_HOME /Applications/Dev/apache-maven
setenv JMETER_HOME /Applications/Dev/jakarta-jmeter
Save your changes in vi and reboot your Mac. Or use the grep/xargs command which is shown in the code comment above.
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.