How to install NodeJS and NPM on Mac using Homebrew

Howto Mac

Share

In this tutorial we will learn to install NodeJS and NPM on Mac using Homebrew.

Step 1: Install Homebrew

Homebrew is "The missing package manager for macOS".

Open terminal and type the following command.

$ /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

This will install Homebrew on your Mac. To check the version type the following command.

$ brew -v

Output

YUSUF-MacBook-Pro:~ yusufshakeel$ brew -v
Homebrew 1.1.11
Homebrew/homebrew-core (git revision be9c; last commit 2017-03-09)

Why use Homebrew to install NodeJS?

If you are installing NodeJS via the installer from https://nodejs.org/ then you have to use sudo to make sure that it installs correctly. After that you have to make changes in your system $PATH by adding the path of the node executable. And if you want to uninstall node then you have track all the files that were created and get rid of them. In short its a long process.

That's why Homebrew is used. It makes the job easy. It will install/uninstall Node easily.

Step 2: Install Node via Homebrew

In the terminal type the following command to install Node.

$ brew install node

If everything installed successfully then you can type in the following command in the terminal to check the Node and NPM version.

$ node -v
v7.7.2
$ npm -v
4.1.2

Possible issues/errors that may occur

YUSUF-MacBook-Pro:~ yusufshakeel$ brew install node
==> Installing dependencies for node: icu4c
==> Installing node dependency: icu4c
==> Downloading https://homebrew.bintray.com/bottles/icu4c-58.2.sierra.bottle.tar.gz
Already downloaded: /Users/yusufshakeel/Library/Caches/Homebrew/icu4c-58.2.sierra.bottle.tar.gz
==> Pouring icu4c-58.2.sierra.bottle.tar.gz
==> Caveats
This formula is keg-only, which means it was not symlinked into /usr/local.

macOS provides libicucore.dylib (but nothing else).

If you need to have this software first in your PATH run:
  echo 'export PATH="/usr/local/opt/icu4c/bin:$PATH"' >> ~/.bash_profile
  echo 'export PATH="/usr/local/opt/icu4c/sbin:$PATH"' >> ~/.bash_profile

For compilers to find this software you may need to set:
    LDFLAGS:  -L/usr/local/opt/icu4c/lib
    CPPFLAGS: -I/usr/local/opt/icu4c/include

==> Summary
    /usr/local/Cellar/icu4c/58.2: 242 files, 65MB
==> Installing node 
==> Downloading https://homebrew.bintray.com/bottles/node-7.7.2.sierra.bottle.tar.gz
######################################################################## 100.0%
==> Pouring node-7.7.2.sierra.bottle.tar.gz
Error: The `brew link` step did not complete successfully
The formula built, but is not symlinked into /usr/local
Could not symlink share/doc/node/gdbinit
/usr/local/share/doc/node is not writable.

You can try again using:
  brew link node
==> Using the sandbox
==> Caveats
Bash completion has been installed to:
  /usr/local/etc/bash_completion.d
==> Summary
    /usr/local/Cellar/node/7.7.2: 3,148 files, 40.2MB

If you look above you will see that we have some issues.

macOS provides libicucore.dylib (but nothing else).
If you need to have this software first in your PATH run:
echo 'export PATH="/usr/local/opt/icu4c/bin:$PATH"' >> ~/.bash_profile
echo 'export PATH="/usr/local/opt/icu4c/sbin:$PATH"' >> ~/.bash_profile

So, in terminal type the following command.

$ echo 'export PATH="/usr/local/opt/icu4c/bin:$PATH"' >> ~/.bash_profile
$ echo 'export PATH="/usr/local/opt/icu4c/sbin:$PATH"' >> ~/.bash_profile

We also got an error "Error: The `brew link` step did not complete successfully".

So, type the following command.

$ brew link node

Output

YUSUF-MacBook-Pro:~ yusufshakeel$ brew link node
Linking /usr/local/Cellar/node/7.7.2... 
Error: Could not symlink share/man/man1/node.1
Target /usr/local/share/man/man1/node.1
already exists. You may want to remove it:
  rm '/usr/local/share/man/man1/node.1'

To force the link and overwrite all conflicting files:
  brew link --overwrite node

To list all files that would be deleted:
  brew link --overwrite --dry-run node

The above output says:

You may want to remove it:
rm '/usr/local/share/man/man1/node.1'

So, use the following command.

$ sudo rm '/usr/local/share/man/man1/node.1'

Now, to list all files that would be deleted use the following command.

$ brew link --overwrite --dry-run node

Output

YUSUF-MacBook-Pro:~ yusufshakeel$ brew link --overwrite --dry-run node
Would remove:
/usr/local/share/systemtap/tapset/node.stp
/usr/local/lib/dtrace/node.d
YUSUF-MacBook-Pro:~ yusufshakeel$ brew link --overwrite node
Linking /usr/local/Cellar/node/7.7.2... 
Error: Could not symlink share/systemtap/tapset/node.stp
/usr/local/share/systemtap/tapset is not writable.

Now we got the Error: Could not symlink share/systemtap/tapset/node.stp
/usr/local/share/systemtap/tapset is not writable.

To fix this we have to change the username and group of the systemtap directory.

Type the following command in the terminal.

$ cd /usr/local/share
$ sudo chown -R yourusername:yourgroupname systemtap

Output

YUSUF-MacBook-Pro:~ yusufshakeel$ cd /usr/local/share
YUSUF-MacBook-Pro:share yusufshakeel$ pwd
/usr/local/share
YUSUF-MacBook-Pro:share yusufshakeel$ ls -la
total 0
drwxrwxr-x  13 root          admin   442 Nov 29 00:08 .
drwxr-xr-x  25 root          wheel   850 Feb 22 10:22 ..
drwxrwxr-x   3 root          wheel   102 Oct  5  2015 systemtap
YUSUF-MacBook-Pro:share yusufshakeel$ sudo chown -R yusufshakeel:admin systemtap
Password:
YUSUF-MacBook-Pro:share yusufshakeel$ ls -la
total 0
drwxrwxr-x  13 root          admin   442 Nov 29 00:08 .
drwxr-xr-x  25 root          wheel   850 Feb 22 10:22 ..
drwxrwxr-x   3 yusufshakeel  admin   102 Oct  5  2015 systemtap

Now, run the --overwrite --dry-run command again.

$ brew link --overwrite --dry-run node

Output

YUSUF-MacBook-Pro:~ yusufshakeel$ brew link --overwrite --dry-run node
Would remove:
/usr/local/share/systemtap/tapset/node.stp
/usr/local/lib/dtrace/node.d
YUSUF-MacBook-Pro:~ yusufshakeel$ brew link --overwrite node
Linking /usr/local/Cellar/node/7.7.2... 
Error: Could not symlink lib/dtrace/node.d
/usr/local/lib/dtrace is not writable.

Now, we got the Error: Could not symlink lib/dtrace/node.d
/usr/local/lib/dtrace is not writable.

To fix this we have to change the username and group of the dtrace directory.

In the terminal type in the following command.

$ cd /usr/local/lib
$ sudo chown -R yourusername:yourgroupname dtrace

Output

YUSUF-MacBook-Pro:share yusufshakeel$ cd /usr/local/lib
YUSUF-MacBook-Pro:lib yusufshakeel$ pwd
/usr/local/lib
YUSUF-MacBook-Pro:lib yusufshakeel$ ls -la
total 52344
drwxrwxr-x  22 root          admin       748 Nov 24 12:30 .
drwxr-xr-x  25 root          wheel       850 Feb 22 10:22 ..
drwxrwxr-x   3 root          wheel       102 Aug 24  2016 dtrace
YUSUF-MacBook-Pro:lib yusufshakeel$ sudo chown -R yusufshakeel:admin dtrace
YUSUF-MacBook-Pro:lib yusufshakeel$ ls -la
total 52344
drwxrwxr-x  22 root          admin       748 Nov 24 12:30 .
drwxr-xr-x  25 root          wheel       850 Feb 22 10:22 ..
drwxrwxr-x   3 yusufshakeel  admin       102 Aug 24  2016 dtrace

Now, run the --overwrite --dry-run command again. Hopefully you will get no error. So, its time to run the --overwrite command.

$ brew link --overwrite node

Output

YUSUF-MacBook-Pro:~ yusufshakeel$ brew link --overwrite node
Linking /usr/local/Cellar/node/7.7.2... 7 symlinks created

Congratulation! You have successfully installed NodeJS and NPM on your Mac via Homebrew.

Share

Recently Added