116

When I do 'open .profile' in the terminal I have the following:

export PATH=$PATH:/usr/local/git/bin 

Now I installed node.js for Mac and it says,

Make sure that /usr/local/bin is in your $PATH.

How can I add /usr/local/bin to export PATH=$PATH:/usr/local/git/bin?

Avi Turner
  • 10,234
  • 7
  • 48
  • 75
shin
  • 31,901
  • 69
  • 184
  • 271
  • 28
    I have voted to re-open this quesiton. It is about Node.js as well as Git (which is a tool that programmers use). – KatieK Sep 18 '13 at 04:26
  • 9
    Yeah. If not here, where. – bobobobo Jan 08 '14 at 16:43
  • 7
    Minor bit of information: I don't know about earlier versions of OS X, but as of Yosemite at least, /usr/local/bin is included in $PATH by default. You can check what's in your $PATH by running echo $PATH. – Ross Henderson May 18 '15 at 16:28
  • MacOS generates PATH (partly) from /etc/paths, which is where you'll find /usr/local/bin listed, as well as /usr/bin, /bin, /usr/sbin, and /sbin. See Also (see https://apple.stackexchange.com/questions/79306 – TextGeek May 21 '21 at 22:27
  • echo $PATH will print your path. If you see /usr/local/bin between some colons, then it's in your path. source: https://stackoverflow.com/a/19202049/1459653 – Mark Gavagan May 28 '21 at 14:17

6 Answers6

132

The PATH variable holds a list of directories separated by colons, so if you want to add more than one directory, just put a colon between them:

export PATH=$PATH:/usr/local/git/bin:/usr/local/bin

That syntax works in any Bourne-compatible shell (sh, ksh, bash, zsh...). But zsh, which is the default shell in recent versions of MacOS, also exposes the PATH another way - as a variable named (lowercase) $path, which is an array instead of a single string. So you can do this instead:

path+=(/usr/local/git/bin /usr/local/bin) 

In either case, you may want to check to make sure the directory isn't already in the PATH before adding it. Here's what that looks like using the generic syntax:

for dir in /usr/local/git/bin /usr/local/bin; do
   case "$PATH" in 
     $dir:*|*:$dir:*|*:$dir) :;; # already there, do nothing
     *) PATH=$PATH:$dir          # otherwise add it
   esac
done

And here's a zsh-specific version:

for dir in /usr/local/git/bin /usr/local/bin; do
  if (( ${path[(i)$dir]} > $#path )); then
    path+=($dir)
  fi
done

But in Zsh you can also just mark the array vars as accepting only unique entries:

typeset -TU PATH path

and even make your own pathlike variables mirrored in arrays:

typeset -TU PYTHONPATH pythonpath
Mark Reed
  • 91,912
  • 16
  • 138
  • 175
  • 9
    While you can do this, it's not the OS X way. Paths on OS X are built using `/usr/libexec/path_helper`, called from the default `/etc/profile`. Start at `man path_helper` then add your paths in files in `/etc/paths.d`. You will find that pretty much every path setting example from other OSs includes `$PATH` because none of them seem to be able to commit to being the first one in the chain... – Synchro Jul 07 '14 at 12:54
  • 1
    I was just answering the question of how to add multiple directories to the PATH. You could also create multiple entries in `/etc/paths.d` or a single entry with multiple directories, one per line, but that doesn't help you at the prompt, and is just an indirect way of accomplishing the same thing. Plus, even though the question is tagged `osx`, this approach has the advantage of working on Linux and other UNIX-like systems as well. – Mark Reed Jul 07 '14 at 14:32
  • The problem I find on Linux is that's it's horribly inconsistent and not a good example to follow. You'll find different sources telling you to use .profile, .bashrc, /etc/profile, /etc/environment and so on, and none of them want to take responsibility of saying "this is the right place to set the system path", so you end up taking the cross-your-fingers-and-hope approach of tacking $PATH onto everything, especially programatically. Install npm from homebrew and the paths work magically because it does it the right way. – Synchro Jul 07 '14 at 14:42
  • Thanks - worked for me. I confirmed it by running `node --version` – marika.daboja May 12 '20 at 09:48
  • Thanks a lot... I didn't know about the ZSH version. I come from a `bash` background and never took the time to learn ZSH. Good to know! – Ziyan Junaideen Oct 11 '21 at 19:55
  • To be clear, the bash version works fine in zsh. But zsh also exposes PATH (and other pathlike variables, including CDPATH, FPATH, and MODULE_PATH) as arrays for easier manipulation. – Mark Reed Oct 11 '21 at 21:03
23

Try placing $PATH at the end.

export PATH=/usr/local/git/bin:/usr/local/bin:$PATH
sushil
  • 2,641
  • 3
  • 18
  • 24
6

To make the edited value of path persists in the next sessions

cd ~/
touch .bash_profile
open .bash_profile

That will open the .bash_profile in editor, write inside the following after adding what you want to the path separating each value by colon.

export PATH=$PATH:/usr/local/git/bin:/usr/local/bin:

Save, exit, restart your terminal and enjoy

spb
  • 4,049
  • 6
  • 22
  • 30
Daniel Raouf
  • 2,307
  • 1
  • 22
  • 28
4

I've had the same problem with you.

cd to ../etc/ then use ls to make sure your "paths" file is in , vim paths, add "/usr/local/bin" at the end of the file.

2

I tend to find this neat

sudo mkdir -p /etc/paths.d   # was optional in my case
echo /usr/local/git/bin  | sudo tee /etc/paths.d/mypath1
american-ninja-warrior
  • 7,397
  • 11
  • 46
  • 80
1

In MAC OS Catalina, this are the steps that worked for me, all the above solutions did help but didn't solve my problem.

  1. check node --version, still the old one in use.
  2. cd ~/
  3. atom .bash_profile
  4. Remove the $PATH pointing to old node version, in my case it was /usr/local/bin/node/@node8
  5. Add & save this to $PATH instead "export PATH=$PATH:/usr/local/git/bin:/usr/local/bin"
  6. Close all applications using node (terminal, simulator, browser expo etc)
  7. restart terminal and check node --version
NadZ
  • 1,024
  • 9
  • 10