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…)


4 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

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