In Mountain Lion how do I set, in one place, the PATH environment variable for all newly launched applications?

Solution 1:

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.

Solution 2:

Expanding on the previous answer, to change the path in /etc/launchd.conf, you'll need to do the following.

  1. Make sure you have Administrator privileges.

  2. Create a new text file with the following content, modified to fit your needs:

    setenv PATH /usr/local/bin:/usr/local/sbin:/usr/bin:/usr/sbin:/bin:/sbin:/Users/YourUserName/bin
    
  3. Save it in your home directory as launchd.conf.

  4. Open Terminal.app (or your favorite substitute) and type:

    sudo mv ~/launchd.conf /etc

    Depending on your previous usage of the sudo command, you may get a short "be careful doing what you're doing" message, but either way you'll need to enter your password. All the command does is move the file to the /etc directory, which is not directly accessible when saving a file in TextEdit, for example. sudo (short for "Super User DO") is used because Administrator privileges are needed to write to that directory.

  5. Reboot your computer.

And you should be all set. Good luck!