How To Install And Configure Node.js And npm In Ubuntu, Debian Or Linux Mint
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 (stretch), Debian 10 (buster), Debian testing and Debian unstable
- Ubuntu 20.10, 20.04, 19.10, 18.04 LTS and 16.04 LTS
- Linux Mint 20, 19 and 18
- Raspberry Pi using Debian (Raspbian) or Ubuntu, with armhf (ARM 32-bit hard-float, ARMv7) or arm64 (ARM 64-bit, ARMv8)
- Other Linux distributions based on either the Debian or Ubuntu versions above
The Node repository does not support ARMv6, so Raspberry Pi 1 is not supported.
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).
- To install Node.js LTS (version 14 right now):
curl -sL https://deb.nodesource.com/setup_lts.x | sudo -E bash -
sudo apt install nodejs
- To install Node.js Current (version 16 right now):
curl -sL https://deb.nodesource.com/setup_current.x | sudo -E bash -
sudo apt install nodejs
- To install Node.js 16:
curl -sL https://deb.nodesource.com/setup_16.x | sudo -E bash -
sudo apt install nodejs
- To install Node.js 15:
curl -sL https://deb.nodesource.com/setup_15.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
- To install Node.js 10:
curl -sL https://deb.nodesource.com/setup_10.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:
. ~/.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.