91

Apps launched via Finder seem to not respect the PATH as set in .bash_profile. So when I try to run code from an IDE (Intellij) I no longer have access to programs in /usr/local/bin, which is normally added to my path in the Terminal.

Apparently .MacOSX/environment.plist used to be the way to do this, but it no longer works in Lion.

How can I set the PATH for Finder-launched applications?

3

8 Answers 8

43

If you are on 10.7 and not 10.8, the solution below works well:

I had the same problem with eclipse, but now I've added e.g. the following to my .bash_profile and then it worked.

export PATH=some_path:another_path
launchctl setenv PATH $PATH

In case you want to leave the original path intact use

p=$(launchctl getenv PATH)
launchctl setenv PATH /my/new/path:$p

instead (or just launchctl setenv PATH /my/new/path:$(launchctl getenv PATH)).

Note: Changing the launchctl PATH will not take effect until the Dock is "restarted". A new Dock process will automatically start after the current one is killed with the command:

killall Dock
15
  • 1
    I ended up using: "launchctl setenv PATH $PATH". Appending the existing launchctl path via "$p" ends up repeating the path each time you open a shell. May 20, 2012 at 13:06
  • 4
    This does not work on OS X 10.8 - tried with Eclipse and IntelliJ - running set|grep PATH from them will always return PATH=/usr/bin:/bin:/usr/sbin:/sbin
    – sorin
    Sep 11, 2012 at 14:39
  • 1
    Doesn't work for my either (10.8.1)
    – nohillside
    Sep 12, 2012 at 4:42
  • 4
    You might try restarting the dock after running launchctl: osascript -e 'tell app "Dock" to quit'. That seemed to fix it for me. Nov 7, 2012 at 16:44
  • 2
    This works for me in 10.12, but only as a one off. After restarting the machine the effects are lost.
    – Dover8
    Jan 19, 2017 at 15:34
28

On OS X 10.10 Yosemite, I used this command:

sudo launchctl config user path <my path setting>

Be aware that his sets the launchtl PATH for all users. This worked well for my use case. Note that you'll be asked to reboot your machine for the effects to take hold.

You must restart all applications for this to have effect. (It doesn't apply to applications that are reopened at login after rebooting.) (Thanks @Brecht Machiels.)

6
  • 2
    This doesn't seem to have an effect on the PATH environment variable of applications that are reopened at login (that were open when shutting down). Jul 8, 2016 at 14:35
  • 7
    This answer worked for me. Specifically, to access Brew-managed executable, I had to sudo launchctl config user path /usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin and reboot.
    – jpsecher
    Dec 8, 2016 at 11:43
  • 1
    This is the best answer here for adding /usr/local/bin for Homebrew based applications. Folks please vote this answer up! It works in 10.11 and 10.12 as well (not tested in 10.13 myself). No need to mess with environment.plist in most cases. Oct 7, 2017 at 16:39
  • 3
    This post explains how to reverse this setting or set launchctl system defaults.
    – John
    Apr 1, 2020 at 21:41
  • 2
    Confirmed to work on macOS 10.15 Catalina.
    – idbrii
    Jul 9, 2020 at 20:39
22

To answer you question to your 'new' problem, I've decided to write another answer - because it is easier to explain with samples.

One way to load the environment variables on startup of your tool (IDE) of choice is like it can be done with eclipse - I think there must be a similar structure in your tool (IDE) too.

How it can be done in eclipse - https://stackoverflow.com/questions/829749/launch-mac-eclipse-with-environment-variables-set

(slightly re-written about the environment variables)

Create an empty text file called "eclipse.sh" in the Eclipse application bundle directory /Applications/Eclipse.app/Contents/MacOS

Open the eclipse.sh in a text editor and enter the following contents:

#!/bin/sh

. ~/.bash_profile

logger "$(dirname \"$0\")/eclipse"

exec "$(dirname \"$0\")/eclipse" "$@"

In the Terminal set the executable flag of the shell script eclipse.sh, i.e.:

chmod +x /Applications/Eclipse.app/Contents/MacOS/eclipse.sh

Open the Eclipse.app Info.plist and change the value for the key CFBundleExecutable from eclipse to eclipse.sh.

MacOS X does not automatically detect that the Eclipse.app's Info.plist has changed. Therefore you need to force update the LaunchService database in the Terminal by using the lsregister command:

/System/Library/Frameworks/CoreServices.framework/Frameworks/LaunchServices.framework/Support/lsregister -v -f /Applications/Eclipse.app

The next time you launch Eclipse.app from the Dock or from the Finder the environment variables should be set.

2
  • +1 for a clear and accurate explanation, and for the fact that over six years later, this still works with High Sierra, 10.13.x.
    – dgnuff
    Oct 15, 2018 at 23:12
  • Still works in Big Sur, for the record.
    – eborisch
    Nov 17, 2021 at 19:56
14

On Mountain Lion all the /etc/paths and /etc/launchd.conf editing doesn't take any effect!

Apple's Developer Forums say:

"Change the Info.plist of the .app itself to contain an "LSEnvironment" dictionary with the environment variables you want.

~/.MacOSX/environment.plist is no longer supported."

So I directly edited the app's Info.plist (right click on "AppName.app" (in this case SourceTree) and then "Show package contents")

Show Package Contents

and added a new key/dict pair called:

<key>LSEnvironment</key>
<dict>
     <key>PATH</key>
     <string>/Users/flori/.rvm/gems/ruby-1.9.3-p362/bin:/Users/flori/.rvm/gems/ruby-1.9.3-p362@global/bin:/Users/flori/.rvm/rubies/ruby-1.9.3-p326/bin:/Users/flori/.rvm/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:</string>
</dict>

(see: LaunchServicesKeys Documentation at Apple)

enter image description here

now the App (in my case SourceTree) uses the given path and works with git 1.9.3 :-)

PS: Of course you have to adjust the Path entry to your specific path needs.

3
  • Is this true? I see lots of posts with contradictory information, some of which are clearly old, but some of which seems recent. I don't even have (on 10.8.2) a /etc/launchd.conf anyway. Presumably, even if the rules not state that apps should use their Info.plist files for paths, they could still be using other files -- /etc/launchd.conf, /etc/paths/, or /etc/paths.d/*, or `~/.MacOSX/environment.plist. Is it safe to say then that, in practice, paths for GUI apps in Mountain Lion could be set in any of these files?
    – orome
    Feb 3, 2013 at 17:47
  • 3
    This Info.plist was the only thing what worked for me, after trying lauchd.conf, etc/paths etc with my Maverics and eclipse. Actually this did not work immediately also, you need to remember two things: 1. run /System/Library/Frameworks/CoreServices.framework/Frameworks/LaunchServices.framework/Support/lsregister -v -f /Applications/eclipse/Eclipse.app as given in another answer, after changing plist and 2. add full path there, you cannot use existing PATH as in your profile scripts.
    – JaakL
    May 5, 2014 at 20:30
  • This works on Sierra (10.12). But it is still necessary to run the lsregister command, as noted by @JaakL. Feb 1, 2017 at 14:49
2

On Mountain Lion (10.8.4), $PATH is treated specially somehow. launchctl setenv PATH /your/path:/here does not have any effect on the $PATH in Terminal.app or Emacs.app instances subsequently launched from the Dock or from the Finder (whereas launchctl setenv SPONG foo works fine). Also $HOME/.launchd.conf doesn't work. /etc/launchd.conf is the only way I have found to get PATH set correctly everywhere. Unfortunately, one cannot use envars such as $HOME there, so all the users on my laptop have /Users/nb/bin on their $PATH. That's only me, so I don't care.

1
  • You should find that the Terminal process picks up the PATH value you set, but: when you create a new terminal it starts a login shell, which—if you're using bash—executes /etc/profile, which initializes PATH to the value returned from /usr/libexec/path_helper. If you choose Shell > New Command… and run env (not in a shell) you should find that PATH is the value you set via launchd.
    – Chris Page
    Apr 22, 2016 at 20:46
1

Inspired by the answer of @brki I wrote this script. It takes the contents of /etc/paths and uses it to reconstruct the original path (and /usr/local/bin), then adds ~/bin and /some/other to it.

if [ -f /etc/paths ]; then
  ETC_PATHS=$(while read line; do
    echo -n "${line}:"
  done < /etc/paths)
  PATH_OVERRIDE=${ETC_PATHS}${HOME}/bin:/some/other
  echo "overriding original path /usr/bin:/bin:/usr/sbin:/sbin with ${PATH_OVERRIDE} so that gpg and node will be on path for spotlight launched programs"
  sudo launchctl config user path $PATH_OVERRIDE
fi

I have verified that this works on macOS Catalina 10.15.1

-1

Try setting path in your ~/.profile or ~/.bash_profile or ~/.zprofile (for zsh). This worked for me with VimR -- it wasn't reading the PATH when launched from the dock, but it worked when launched from the terminal. I was running on OSX 10.11 by the way.

I don't have a good enough handle on this to give you a great explanation of why it works, there are plenty of explanations online about the different config files and what they do: https://stackoverflow.com/questions/415403/whats-the-difference-between-bashrc-bash-profile-and-environment

Also, see a similar discussion here: https://stackoverflow.com/questions/3344704/path-variable-not-properly-set-in-gvim-macvim-when-it-is-opened-from-the-finder/24542893#24542893

1
  • 2
    The question asks how to do it for apps launched from Doc which is the same as launch from Finder which you say your answer does not work for - and we can exactly explain why it does/does not work
    – mmmmmm
    Oct 22, 2015 at 23:02
-3

On Mac OS X 10.8.4, Mountain Lion, the path environment incorporates the paths listed in this file:

/etc/paths

You can edit this file using a command line tool, such as vim using the following command:

sudo vim /etc/paths

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .