Installing Node.js on Linux

photo creditNadine Shaabana

Installing Node.js on macOS is as easy as brew install. However, on other unix systems, installing Node.js isn't quite as easy as apt-get install. First we need to execute the setup scripts.

Run Setup Scripts

For the current Node.js LTS:

curl -sL https://deb.nodesource.com/setup_6.x | sudo -E bash -
sudo apt-get install -y nodejs

For the latest:

curl -sL https://deb.nodesource.com/setup_8.x | sudo -E bash -
sudo apt-get install -y nodejs

Check Global Install Path

Afterwards, it is important to check your global install path. Unfortunately, this is not always set correctly, and may default to /usr, necessitating elevated permissions to install packages globally. This is well documented in the Node.js docs, but I will briefly reiterate the commands here:

First check your global install path:

npm config get prefix

If this directory is not user-writeable, it is strongly recommended to change it. The official recommendation is to use ~/.npm-global.

mkdir ~/.npm-global
npm config set prefix '~/.npm-global'
# Replace ~/.profile with your shell start script of choice
echo 'export PATH=~/.npm-global/bin:$PATH' >> ~/.profile
source ~/.profile