Skip to content

Instantly share code, notes, and snippets.

@Overbryd
Created January 24, 2012 09:58
Show Gist options
  • Save Overbryd/1669348 to your computer and use it in GitHub Desktop.
Save Overbryd/1669348 to your computer and use it in GitHub Desktop.
Correcting $PATH in MacOSX 10.7 for homebrew

$PATH in MacOSX 10.7

MacOSX has a truly global path setting that precedes any other setting like ~/.bash_profile. The file /private/etc/paths is a list of pathnames. The order from top to bottom defines the resulting order in the $PATH variable. After loading /private/etc/paths there is a directory /private/etc/paths.d/ with files in the same style. Those are appended to the $PATH variable.

The default content of /private/etc/paths looks like this:

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

The resulting $PATH variable looks like this:

$ echo "$PATH"
# => "/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin"

Homebrew

When using homebrew in its standard way, all your packages end up in /usr/local. This is a problem if you want to install software via homebrew that should replace system default installations.

Git is a good example.

The system default $ /usr/bin/git --version outputs git version 1.7.4.4. Your homebrew installed git (at the time of writing) $ /usr/local/bin/git --version outputs git version 1.7.8.3.

But without changing the default path combination, you end up using system Git instead of homebrew Git.

Correcting $PATH globally

There is various workarounds and fixes for this problem. For example one could tackle this problem for a specific application. TextMate for example allows you to set TM_GIT to a git executable of your choice. But why bothering with application specific settings, when you can fix the problem at its root.

I propose that /usr/local/bin comes before /usr/bin

Here are the contents of my corrected /private/etc/paths. I've moved /usr/local/bin on top.

/usr/local/bin
/usr/bin
/bin
/usr/sbin
/sbin
--- /private/etc/paths.old 2012-01-24 10:34:35.000000000 +0100
+++ /private/etc/paths 2012-01-24 10:34:53.000000000 +0100
@@ -1,5 +1,5 @@
+/usr/local/bin
/usr/bin
/bin
/usr/sbin
/sbin
-/usr/local/bin
@teleservices
Copy link

Excellent! - my philosophy about $PATH is that local paths should always come first,

My $PATH (after is gets tweaked along the way) winds up looking something like this:

.:~/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin

Allows me to run programs in my current directory, personal bin directory, locally (i.e., homebrew programs), etc.

This is the unix way to do this...

@darren
Copy link

darren commented Apr 7, 2013

how about the security considerations? /usr/local/bin/ is user writable, so what if someone put some malicious executables that replace commonly used system tools like ls' andrm' etc. in the directory?

@sharat
Copy link

sharat commented Oct 7, 2013

On editing this file, it says

/private/etc/paths isn't owned by me.

@shanesmith
Copy link

@isarat, that's because it's owned by root, if you're editing through the command line you'll need to use sudo

@chaitanyamannem
Copy link

Thank you very much :)

@CourtJesterG
Copy link

I heard about this, going to give it a try. I still have trouble with paths and such in terminal, even if I adjusted in my bash_profile. Curious does this include after you put usr/local up top that when you do the which command it wil actually put to the correct item? Example which gcc; usually this will always point to usr/bin do to the fact homebrew has certain names for packages, and versions as well of packages. Example if I put gcc48 or what ever that is installed in usr/local it will than point to that one. I hate it when someone tries explaining something to me, and than asks just use which or echo $ . Everytime I know, it won't point to the item they are talking about unless I specify by the whole package name or version that is in usr/local.

  • Recently I did an bash_aliases profile for Python since I have both Python2.7 & Python3 installed in usr/local/bin Am hoping this is correct and working correct, cause the system kept using Python2.7 even if my path was correctly stated in bash_profile. I put in bash_aliases:

alias python=python3

alias 2=python2.7

alias 3=python3

THAN had put this in my bash_profile:

this is comment out- Load .bash_aliases if it exists
test -f ~/.bash_aliases && source ~/.bash_aliases

now when I do, which python -It points to usr/local/bin Though I noticed if I do echo $python or even say python3 nothing happens.

  • I am wondering if I could do this for llvm, clang, and gcc also any tools. I read up about Xcode, and yeah did install the command line tools, they are what install in usr/bin. Yet if you use Xcode it will use theirs by default and I think the same for the terminal it uses by default what is inside the Xcode.app in contents /developers and such. So even with paths seem set in terminal it seems Xcode might be still being used with there tools. I read some people making symlinks and such, but heard this is bad and aliases aren't symlinks so they won't break during an upgrade.

This is what am understanding but don't know if is true or not. Please provide any input if you can on here or email me Richard.Shawn.Faust@gmail.com , Thanks!

@privatenumber
Copy link

In zsh, the path is declared in ~/.zshrc
Does this global setting work with zsh as well?

Edit: it doesn't

@paskal
Copy link

paskal commented Nov 13, 2014

@hirokiosame, thanks a lot! Just what I was looking for — editing /private/etc/paths don't work with zsh.

@ShivangiM
Copy link

I am on MACOX10.11.6 and did change my paths as mention above, restarted my terminal but I still get default python instead of homebrew python

@lizixroy
Copy link

Hi ShivangiM, I am having the same issue that system python is used despite that I put /usr/local/bin before /usr/bin in my /etc/paths file. Did you solve this issue?

@mcandre
Copy link

mcandre commented Dec 19, 2017

I am unable to modify /etc/paths in macOS 10.13 High Sierra. I am using sudo, my user is a member of the wheel group, SIP is disabled, and the file has chmod 0644 permissions. Yet all attempts to modify the file result in Permission denied for some reason.

@theeternalsw0rd
Copy link

@mcandre, maybe the file is locked. Try sudo chflags nouchg /etc/paths before editing.

@pratyushtewari
Copy link

my philosophy about $PATH is that local paths should always come first,

@teleservices yes!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment