Macos – How to change the default version of a unix executable

macosunixwhich

I currently have two versions of the same executable installed on my OSX machine: one sits in usr/bin, and the other is a Macports installation. When I run which, the terminal returns the location to the usr/bin version. Is there a uniform way to change the default installation, or does every application have their own command option for this?

Best Answer

You need to modify the PATH environment variable. This is the standard used to search for executables under Unix.

When something on Unix looks for an application that is given with a relative path (eg: clang rather than /usr/bin/clang), PATH is split on the : character, and then searched in order.

eg: if your PATH is /opt/local/bin:/bin:/usr/bin then clang will look for /opt/local/bin/clang, and if that is missing, /bin/clang, etc.

Technically, every process has their own way to do this - nothing forces them to follow the convention of using $PATH, but it has been that way for long enough that pretty much everything does.

The other consideration is that every process has an environment of it's own - a copy of PATH included in that. That includes launchd, which is what is responsible for the UI and any application launched from that.

So, you can modify this in your shell init scripts (.bash_profile, .bashrc, .profile, etc), but that won't work if, eg, you launch vim or emacs as a GUI application.

You can also modify /etc/paths, or add a file to /etc/paths.d, and that will form part of the standard environment for everything - including every application launched on the Mac. (You need to log out and back in to have that permeate every part of the system, though.)

Related Question