Friday, January 15, 2016

Set OS X Environment Variables

There are several ways to set environment variables for Mac OS X:
  • For command line applications running in a console / terminal
  • For GUI applications run from Spotlight (desktop)
  • For both command line and GUI applications

Here is a Stack Overflow thread discussing how to set environment variables:
Setting environment variables in OS X?

Set environment variables for command line applications in terminal


Edit .bash_profile or .profile or .bashrc in your user directory (~ or /Users/username), something like this:
# User specific environment and startup programs
 
PATH=/usr/local/bin:/usr/local/git/bin:$PATH:$HOME/bin
export ORACLE_HOME=/Users/myuser/Oracle/instantclient_11_2
export DYLD_LIBRARY_PATH=$ORACLE_HOME
export LD_LIBRARY_PATH=$ORACLE_HOME
#DYLD_LIBRARY_PATH=/Users/myuser/Oracle/instantclient_11_2/lib
 
export PATH
#export DYLD_LIBRARY_PATH
 
alias ll="ls -l"

Set environment variables for GUI applications


Note: Starting with Yosemite (version 10.10), this method is no longer used.  See the "DEPRECATED AND REMOVED FUNCTIONALITY" at the end when you "man launchctl".

Edit /etc/launchd.conf
sudo vi /etc/launchd.conf
Add environment variables like this:
# 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, see MAVEN_OPTS example below.
 
#Java:
setenv JAVA_VERSION 1.7
#Mac OS X JAVA (1.6.0_65) is here:
#setenv JAVA_HOME /System/Library/Frameworks/JavaVM.framework/Home
#setenv JAVA_HOME /System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home
 
#JAVA 1.7 is here (see /usr/bin/java*):
setenv JAVA_HOME /Library/Java/JavaVirtualMachines/jdk1.7.0_51.jdk/Contents/Home
 
 
#Maven:
setenv M2_HOME /opt/maven
setenv MAVEN_OPTS "-Xmx1024M -XX:MaxPermSize=512m"
 
#Oracle:
setenv NLS_LANG /Users/myuser/Oracle/instantclient_11_2
#setenv ORACLE_HOME /Users/myuser/Oracle
#setenv DYLD_LIBRARY_PATH /Users/myuser/Oracle/instantclient_11_2
#setenv LD_LIBRARY_PATH /Users/myuser/Oracle/instantclient_11_2
#setenv TNS_ADMIN /Users/myuser/Oracle/instantclient_11_2/network/adminsetenv JAVA_HOME /System/Library/Frameworks/JavaVM.framework/Versions/1.6/Home
 
#Other:
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
You can also set environment variables for applications started by Spotlight from the command line, although they will only be available until the computer is rebooted.  You do it like this:
launchctl setenv M2_HOME /opt/maven
It will be available to GUI applications that launch from Spotlight but will disappear when the computer is shut down.  If you want the environment variables to remain after rebooting edit /etc/launchd.conf.

Set Path


There is only one reliable way to set the path for both the command shell and the GUI.  Modify /etc/paths.
sudo vim /etc/paths

No comments:

Post a Comment

DBeaver vs. SQL Developer: DBeaver fail!

DBeaver claims to be the best database editor. Thanks to some bad UI, something as simple as switching the database schema in the SQL editor...