71

By default, OS X 10.6 uses /usr/libexec/path_helper to add the following paths listed in the file /etc/paths:

/usr/bin
/bin
/usr/sbin
/sbin
/usr/local/bin

This means that /usr/bin comes before /usr/local/bin on the path. This results in the version of git installed by Xcode 4 in /usr/bin to be called instead of the version installed by Homebrew into /usr/local/bin.

Which leads me to my question, is there a problem with having /usr/local/bin come before /usr/bin in the path? Is there a specific reason that Apple defaults to having /usr/bin come before /usr/local/bin?

How to change order of /usr/bin and /usr/local/bin

Is it a problem to move /usr/local/bin from the bottom of the file /etc/paths to the top? Doing so would impact the path for more than just when I fire up Terminal, since /usr/libexec/path_helper could be used by other resources (I'm uncertain about this).

While redundant, it seems safer for me to add /usr/local/bin to the path in ~/.bash_profile, which would mean that /usr/local/bin would be on the path twice.

Community
  • 1
  • 1
Matthew Rankin
  • 457,139
  • 39
  • 126
  • 163

5 Answers5

27

No, and no. They're just weird ... local by definition should override.

Forrest Voight
  • 2,249
  • 16
  • 21
  • Any idea if there is a problem with modifying `/etc/paths` file or should I just add `/usr/local/bin` to my `~/.bash_profile`? – Matthew Rankin Mar 19 '11 at 20:51
  • 4
    Either should work fine, but I would try to stick with only changing per-user stuff so the system isn't accidentally wrecked when an incompatible local version is installed. – Forrest Voight Mar 20 '11 at 04:42
  • @MatthewRankin Edit the /etc/paths will be no problem!!! I also encounter this issue earlier, thank you for this question. : ) – Juanito Fatas Mar 27 '12 at 21:56
3

I had trouble with same problem, and found the link below from googling.

https://discussions.apple.com/thread/3588837?start=0&tstart=0

They said that modifying /etc/paths is not a good idea for security reasons.

Ohgyun Ahn
  • 777
  • 1
  • 10
  • 16
2

I found all the above useful, especially @Ohgyun Ahn's warning. So I suggest a compromise, which I just implemented:

Edit /etc/paths or /private/etc/paths (as it is in OS X 10.8) and override git alone. That escapes any security implications (unrelated to git, anyway) while implementing the up-to-date git for use by all programs that actually check the system-wide path.

  1. Create a new directory to be used in the override, for example /usr/local/git-override/
  2. Make new symlinks from git-override to homebrew's git programs. Just remake the git symlinks from /usr/local/bin.
  3. add /usr/local/git-override to the top of (/private)/etc/paths.

Hope that's helpful to someone else.

Community
  • 1
  • 1
Complex
  • 151
  • 1
  • 4
2

I would just like to add that if you want to keep your PATH clean (no duplicate entries), you can add the following to your .bash_profile to achieve the desired effect:

# remove /usr/local/bin and /usr/bin
export PATH=`echo ":$PATH:" | sed -e "s#:/usr/local/bin:#:#g" -e "s/^://" -e "s/:$//"`
export PATH=`echo ":$PATH:" | sed -e "s#:/usr/bin:#:#g" -e "s/^://" -e "s/:$//"`
# add /usr/local/bin and /usr/bin in that order
export PATH="/usr/local/bin:/usr/bin:$PATH"

I learned that little trick from http://ntk.me/2013/05/04/path-environment-variable/

Edit: Very important! Don't get the order that those are removed wrong! If you do, sed won't work and you'll be left with /usr/local/bin:/usr/bin: as your PATH!

It's also worth noting that the others suggesting that this could introduce some security problems are correct. Please be sure to understand the risks involved!

vmrob
  • 2,966
  • 29
  • 40
1

Since modifying the order of /etc/paths seams to be discouraged for system stability and security ... here my solution which is based on the answer of @vmrob

read PATH < <(echo "$PATH" | sed  \
    -e 's/^/:/' -e 's/$/:/'       \
    -e 's_:/usr/local/bin:_:_g'   \
    -e 's_:/usr/local/sbin:_:_g'  \
    -e "s_:/usr/bin:/bin:_:$HOME/bin:/usr/local/bin:/usr/bin:/bin:_"        \
    -e "s_:/usr/sbin:/sbin:_:$HOME/sbin:/usr/local/sbin:/usr/sbin:/sbin:_"  \
    -e 's/^://' -e 's/:$//')
export PATH
166_MMX
  • 590
  • 5
  • 18