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 16.04 LTS and 14.04 LTS
  • Linux Mint 17, 17.x, 18, and 18.x
  • Other Linux distributions based on either the Debian or Ubuntu versions above

Ubuntu 18.04 and Linux Mint 19 are not yet considered stable and as a result, there's no official Node.js repository (but you can install Node.js and npm from their official repositories until then, since they are up to date). A repository will be available once they reach the stable status.

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 8:
curl -sL https://deb.nodesource.com/setup_8.x | sudo -E bash -
sudo apt install nodejs

  • To install Node.js 9:
curl -sL https://deb.nodesource.com/setup_9.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>

No comments:

Powered by Blogger.