LinuxOpen Source SoftwareTutorials

How to install newer node.js and npm on Debian 10 (Buster)

The current LTS version of node (a.k.a. node.js) is 14.15.x and many node developers use this version to build their applications. However the node.js packages of most Linux distributions come with an older version.

In the example of the current Debian stable version, Debian 10 (codename Buster), comes with node 10.21.0:

root@geek:~# apt-cache show nodejs
Package: nodejs
Version: 10.21.0~dfsg-1~deb10u1
Installed-Size: 156
Maintainer: Debian Javascript Maintainers <pkg-javascript-devel@lists.alioth.debian.org>
Architecture: amd64
[...]

Note the package name in Debian is nodejs.

The same also applies to the npm package, the node.js package manager:

root@geek:~# apt-cache show npm
Package: npm
Version: 5.8.0+ds6-4+deb10u2
Installed-Size: 8446
Maintainer: Debian Javascript Maintainers <pkg-javascript-devel@lists.alioth.debian.org>
Architecture: all
[...]

This can cause major problems when trying to install/build a node application using npm install:

root@geek:~/my-node-app# npm install bower gulp
npm WARN npm npm does not support Node.js v10.21.0
npm WARN npm You should probably upgrade to a newer version of node as we
npm WARN npm can't make any promises that npm will work with this version.
npm WARN npm Supported releases of Node.js are the latest release of 4, 6, 7, 8, 9.
npm WARN npm You can find the latest version at https://nodejs.org/
[...]

Download newer node.js LTS version and integrate into the system

The current LTS version can be downloaded from the nodejs site and extracted:

root@geek:~# wget https://nodejs.org/dist/v14.15.4/node-v14.15.4-linux-x64.tar.xz
root@geek:~# tar -xf node-v14.15.4-linux-x64.tar.xz

The extracted tar archive contains the following structure:

root@geek:~# cd node-v14.15.4-linux-x64
root@geek:~/node-v14.15.4-linux-x64# ls -la
total 184
drwxr-xr-x 2 1001 1001  4096 Jan  4 14:10 bin
-rw-r--r-- 1 1001 1001 53322 Jan  4 14:10 CHANGELOG.md
drwxr-xr-x 3 1001 1001  4096 Jan  4 14:10 include
drwxr-xr-x 3 1001 1001  4096 Jan  4 14:10 lib
-rw-r--r-- 1 1001 1001 80117 Jan  4 14:10 LICENSE
-rw-r--r-- 1 1001 1001 29006 Jan  4 14:10 README.md
drwxr-xr-x 5 1001 1001  4096 Jan  4 14:10 share

The bin folder contains the node (node.js) binary as well as symlinks for npm and npx.

root@geek:~/node-v14.15.4-linux-x64/bin# ls -la
total 72144
-rwxr-xr-x 1 1001 1001 73873832 Jan  4 14:10 node
lrwxrwxrwx 1 1001 1001       38 Jan  4 14:10 npm -> ../lib/node_modules/npm/bin/npm-cli.js
lrwxrwxrwx 1 1001 1001       38 Jan  4 14:10 npx -> ../lib/node_modules/npm/bin/npx-cli.js

The npm and npx files can be ignored for now.

Copy the node binary to /usr/local/bin/:

root@geek:~/node-v14.15.4-linux-x64# cp -p bin/node /usr/local/bin/

Now to make sure /usr/local/bin/node takes precedence of (possibly) other node versions installed on that system, use update-alternatives:

root@geek:~# update-alternatives --install /usr/bin/npm npm /usr/local/bin/npm-cli.js 1
update-alternatives: using /usr/local/bin/npm-cli.js to provide /usr/bin/npm (npm) in auto mode

It should now be safe to assume that all scripts and software using the node command, will use the just installed LTS version:

root@geek:~# node -v
v14.15.4

Download newer npm version and integrate into the system

NPM

The easiest and fastest way to install npm is to use the install shell script from npmjs.com:

root@geek:~# curl -L https://www.npmjs.com/install.sh | sh
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  3755  100  3755    0     0   4396      0 --:--:-- --:--:-- --:--:--  4396
fetching: https://registry.npmjs.org/npm/-/npm-6.14.11.tgz
removing existing npm
up to date in 0.119s
installing npm@latest
/usr/local/bin/npm -> /usr/local/lib/node_modules/npm/bin/npm-cli.js
/usr/local/bin/npx -> /usr/local/lib/node_modules/npm/bin/npx-cli.js
+ npm@6.14.11
added 435 packages from 889 contributors in 6.307s
successfully installed npm@latest

As seen in the output, npm itself is actually a javascript file named npm-cli.js. Even though the install script should create a symbolic link in /usr/local/bin/ (when run as root or with sudo), it is best to use update-alternatives here, too:

root@geek:~# update-alternatives --install /usr/bin/npm npm /usr/local/lib/node_modules/npm/bin/npm-cli.js 1
update-alternatives: using /usr/local/lib/node_modules/npm/bin/npm-cli.js to provide /usr/bin/npm (npm) in auto mode

root@geek:~# update-alternatives --display npm
npm - auto mode
  link best version is /usr/local/lib/node_modules/npm/bin/npm-cli.js
  link currently points to /usr/local/lib/node_modules/npm/bin/npm-cli.js
  link npm is /usr/bin/npm
/usr/local/lib/node_modules/npm/bin/npm-cli.js - priority 1

The npm command can now be executed and it should use the just installed version:

root@geek:~# npm -v
6.14.11

Claudio Kuenzler
Claudio has been writing way over 1000 articles on his own blog since 2008 already. He is fascinated by technology, especially Open Source Software. As a Senior Systems Engineer he has seen and solved a lot of problems - and writes about them.

You may also like

Comments are closed.

More in:Linux