How To Install And Configure Node.js And npm In Ubuntu, Debian Or Linux Mint

Node.js logo


Node.js (and npm) is available in the official Debian, Ubuntu and Linux Mint repositories, but depending on the OS version you're using, they might have some old versions that need to be updated.

This article will explain how to install Node.js and npm from the NodeSource repository as well as how to set up npm so you can use it to properly install Node.js packages globally in your user folder, so it doesn't mess with any permissions.

Install Node.js and npm from the Node.js repository in Debian, Ubuntu and Linux Mint


An alternative way of installing Node.js and managing multiple active versions is using NVM, so you may want to check it out. This guide will use the official Node.js-provided repository instead.

These instructions are for:
  • Debian 8 (jessie), Debian 9 (stable), Debian testing and Debian unstable
  • Ubuntu 18.10, 18.04 LTS, 16.04 LTS and 14.04 LTS
  • Linux Mint 17, 17.x, 18, 18.x, 19 and 19.x
  • Other Linux distributions based on either the Debian or Ubuntu versions above

1. Install curl (used to download the official Node.js installation script which adds the repository and key) and build-essential (which will be used to compile and install native addons):

sudo apt install curl build-essential

2. Run the Node.js installation script (provided by nodejs.org).

Nodejs 8 is available for all Ubuntu, Linux Mint and Debian supported versions, including Ubuntu 14.04 / Linux Mint 17. Nodejs 11 is the latest version and is only available for Ubuntu 16.04 and newer, Debian 8 and newer, and Linux Mint 18 and newer.

  • To install Node.js 8:
curl -sL https://deb.nodesource.com/setup_8.x | sudo -E bash -
sudo apt install nodejs

  • To install Node.js 11:
curl -sL https://deb.nodesource.com/setup_11.x | sudo -E bash -
sudo apt install nodejs

  • To install Node.js 12:
curl -sL https://deb.nodesource.com/setup_12.x | sudo -E bash -
sudo apt install nodejs

The nodejs package from the official NodeSource repository includes npm.

Configure npm to install packages globally without root


Using npm installed from either the Ubuntu repositories or the Node.js repository requires running under root by default to install packages. This should be avoided, as per many articles around the web.

To get npm to install packages globally in your home folder (and add the folder to your PATH), you can use a simple script available here. This script does not work if you use NVM!

You can download the script and run it using these commands:

cd && wget https://raw.githubusercontent.com/glenpike/npm-g_nosudo/master/npm-g-nosudo.sh
chmod +x npm-g-nosudo.sh
./npm-g-nosudo.sh

After following the instructions, source your .bashrc file:

source ~/.bashrc

Now you will be able to install npm packages globally without root / sudo, like this:

npm install -g <some package>


Node.js logo image is from Wikipedia.