Path problem with Emacs on Mac OS X


One of the problems you might encounter using a native Emacs on top of Mac OS X is that your custom PATH variable doesn’t really translate into your running Emacs instance. This might show up as making it impossible to run things like Ruby or Ioke from inside of Emacs – complaining that it can’t find the executable in question.

Another thing you might notice is that your Ruby environment is messed up – all the gems you so carefully installed isn’t around, and overall things just seem weird. One thing that could cause this is when you have a custom built Ruby installation in for example /opt/local or /usr/local instead of /usr. But Emacs just seems to pick up the original versino that shipped with OS X.

The reason for all these weird behaviors is that if Emacs is started from the Dock, or using the application directly, it doesn’t actually pick up the carefully prepared PATH variable you have in your terminal. Instead it will go ahead and use the default system PATH, which is very restrictive.

The way I prefer to solve this is to make sure that OS X plist version of the PATH variable is the same as my terminal one. I do that by resetting this plist on every login. Of course, OS X will not actually reload this without a reboot. If you just want to fix this once – and you’re reasonably sure you won’t change the path again, you can execute this command from the terminal and restart. Everything should be fine after that. The magic incantation looks like this:

defaults write $HOME/.MacOSX/environment PATH “$PATH”

(Note, I’ve had to solve this problem twice, since the first time I encountered it I didn’t blog about it. So now I’m blogging so it might also stick in my long term memory. Maybe it’s enough if it’s in my blog term memory…)


10 Comments, Comment or Ping

  1. Alexey Borzenkov

    I’m not sure, but perhaps adding a file in /etc/paths.d might work as well, although it seems that it appends paths, not prepends them. Maybe changing /etc/paths might work on a system level then…

    December 12th, 2009

  2. Alexey:

    No, that doesn’t work. The problem is that the plist PATH variable doesn’t actually have anything to do with system level PATH, so neither of your solutions will work unless you do the “defaults write” as shown above.

    December 12th, 2009

  3. It’s also possible to set the PATH from within Emacs itself, if for whatever reason you want to maintain a separate path within the editor (perhaps to include directories with Emacs-specific binaries or whatnot). You can find an example of this in the first several lines of http://github.com/al3x/emacs/blob/master/customizations/my-shell.el

    Also note that some modes, such as magit or full-ack, rely on the exec-path variable being set, which seems to be distinct from PATH. I set exec-path early in my Emacs configuration to ensure that all modes that rely on it can find the binaries they depend on: http://github.com/al3x/emacs/blob/master/init.el

    December 12th, 2009

  4. grombie

    after execution of the following line:
    defaults write $HOME/.MacOSX/environment PATH “$PATH”

    my terminal hangs during start. I have to press “CTRL-C” after launching the terminal in order to be able to type in commands.

    Furthermore my emacs.app does not know how to execute “dired” for example. It complains “no such file or directory, ls”.

    Can you help me solving this issue? Thank you

    March 4th, 2010

  5. Mikkel

    Seems that the double quotes in the shell-command caused problems on my system. Maybe your blog software turns them into something unicode?

    For me the solution was to omit the double quotes from the command, but others may have to type regular double quotes instead of copy-pasting them from the blogpost.

    March 19th, 2010

  6. Anil

    Ola, if possible can you share your emacs config on github?

    April 3rd, 2010

  7. This is really great. Thanks for posting. I previously used /etc/launchd.conf to solve this problem but that requires a reboot. With your solution I only need restart applications that require a new environment variable. Here’s what I do now:

    http://gist.github.com/365474

    Kudos!

    April 14th, 2010

  8. Jacob Poulsgaard Tjørnholm

    Ola, for some reason your method doesn’t work for me (Snow Leopard + Emacs 23). I did fix the quotes and the value is set in the plist, but Emacs doesn’t pick it up.

    Anyway, what I need in Emacs is just he path that is defined in /etc/paths, so I can set it from my init.el:
    http://gist.github.com/410752

    May 23rd, 2010

  9. David

    Thanks for posting this. My problem was actually with a different application not being able to see things on my “normal” PATH. I suspected that perhaps app’s received a restricted running environment for security purposes, which you’ve basically confirmed here. Pity there doesn’t seem to be a fix from within the application itself (makes sense, security-wise, but is inconvenient for some of us!).

    September 5th, 2010

  10. John

    Another way to solve the problem is to create a new Mac app that sets the right environment and then runs Emacs.app. Drag the new app into the Dock, instead of Emacs.app. For more complete directions, see ‘Using Emacs.app from the Dock’ in http://www.emacswiki.org/emacs/EmacsForMacOS#toc21

    July 19th, 2014

Reply to “Path problem with Emacs on Mac OS X”