MacOS – In Mountain Lion how to set, in one place, the PATH environment variable for all newly launched applications

environment-variablesmacospath

On Ask Different there are many questions regarding the PATH environment variable and how to set it for applications launched by a shell, by spotlight or via the finder (typical questions: How to set PATH for Finder-launched applications or How to change PATH environment variable…).

Answers range from sh startup scripts to /etc/paths to /etc/launchd.conf. Here I am after the answer for a unified setting of PATH for all apps under Mac OS X Mountain Lion 10.8.2.

Best Answer

After many trials with different options I conclude:

Is there a unified way to set the PATH for applications launched by different means (like shell script, Finder and Spotlight)? Well, yes and no:

  • No: Setting PATH in shell start up scripts (e.g. .bashrc) works only for apps launched by shells.
  • No: Setting PATH for the current launchd process via launchctl setenv PATH <...> does not work for me at all.
  • Yes: Setting PATH in /etc/launchd.conf (and restarting the system) will make the appropriate PATH visible in applications launched by Spotlight, Dock and Finder and subsequently (local) shells.
  • No: Remotely invoked shells do not inherit the environment. (See ...why do remote shells via ssh do NOT inherit the environment..)

Notes for set-up of shells:

  1. For correct functioning in local shells it is important to switch off the use of /usr/libexec/path_helper, because the default paths in /etc/paths might change the order of the paths in PATH.**

  2. To accommodate remote shells you need to re-import the launchd environment into the shell. Use something like this in your .bashrc:

# the vexatious path stuff
eval $(launchctl export)

Unfortunate consequences of the whole issue are:

  • No way to have these unified settings on a per user basis. (~/.launchd.conf does not work.)
  • No easy way to construct the PATH from different sources using techniques like home directory expansion et al.