Is there a problem with having /usr/local/bin before /usr/bin on the path in OS X?

MacosPath

Macos Problem Overview


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.

Macos Solutions


Solution 1 - Macos

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

Solution 2 - Macos

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.

Solution 3 - Macos

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.

Solution 4 - Macos

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!

Solution 5 - Macos

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMatthew RankinView Question on Stackoverflow
Solution 1 - MacosForrest VoightView Answer on Stackoverflow
Solution 2 - MacosOhgyun AhnView Answer on Stackoverflow
Solution 3 - MacosComplexView Answer on Stackoverflow
Solution 4 - MacosvmrobView Answer on Stackoverflow
Solution 5 - Macos166_MMXView Answer on Stackoverflow