How to Install Node.js and NPM on Debian 11

how to install node.js and npm on debian 11

Node.js is an open-source and cross-platform JavaScript runtime environment built on Chrome’s V8 JavaScript engine. It is designed for non-blocking, event-driven servers and is primarily used for traditional websites and back-end API services.

installing node.js and npm on debian 11

In this tutorial, we will show you at least three different ways to install Node.js and npm on a Debian 11 server. We believe in showing multiple methods of installing Node.js will benefit any user, since we all want to

Prerequisites

  • Debian 11 VPS
  • Access to the root user account (or access to an admin account with root privileges)

Log in to the Server & Update the Server OS Packages

First, log in to your Debian 11 server via SSH as the root user:

# ssh root@IP_Address -p Port_number

You will need to replace ‘IP_Address‘ and ‘Port_number‘ with your server’s respective IP address and SSH port number. Additionally, replace ‘root’ with the username of the admin account if necessary.

Before starting, you have to make sure that all Debian OS packages installed on the server are up to date. You can do this by running the following commands:

# apt update
# apt upgrade

Method 1: Install Node.js and npm from Debian Repository

The simple and easiest way to install Node.js and npm is to install them from the Debian default repository. However, it does not contain the latest Node.js version. At the time of writing this tutorial, Node.js version 12.22.5 is included by default on Debian 11 repositories.

Install the Node.js and npm with the following command:

# apt install nodejs npm

Once both packages are installed, verify the Node.js version using the following command:

# node -v

You should get the following output:

v12.22.5

You can also check the npm version using the following command:

# npm -v

You should get the following output:

7.5.2

Method 2: Install Node.js and npm from NodeSource

If you want to install a different version of Node.js than the one provided in the Debian repositories, you can use this method. NodeSource maintains an APT repository and contains multiple Node.js versions.

At the time of writing this tutorial, the NodeSource repository provides v17, v16, v14, and v12 versions.

We will install the latest LTS  v16 version.

Install the curl with the following command:

# apt install curl

Download and run the Node.js installation script by running the following command:

# curl -sL https://deb.nodesource.com/setup_16.x | bash -

This will add the GPG key and Node.js repository to the APT.

Note: If you want another version of Node.js  (for example 14x,) you just need to change the setup_16.x with setup_14.x.

Install the Node.js version 16.x by running the following command:

# apt install nodejs

The Node.js package contains both the node and npm binaries. Once installed, verify the installed version of Node.js with the following command:

# node -v

You should get the following output:

v16.13.0

You can also verify the npm version with the following command:

# npm -v

You should get the following output:

8.1.0

Method 3: Install Node.js and npm with NVM

NVM is also known as “Node Version Manager” is a script that allows you to manage multiple versions of Node.js.

You will need to download and install NVM in your system. You can download and run the script manually with the following command:

# curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.37.2/install.sh | bash

You will need to close and reopen the terminal to add the path to the nvm script to the current shell session.

Verify the NVM version with the following command:

# nvm --version

You should get the following output:

0.37.2

You can list all Node.js versions that can be installed with NVM using the following command:

# nvm list-remote

This command will show you a long list of all Node.js versions.

Install the latest stable version of Node.js with the following command:

# nvm install node

You should get the following output:

Downloading and installing node v17.1.0...
Downloading https://nodejs.org/dist/v17.1.0/node-v17.1.0-linux-x64.tar.xz...
############################################################################################################################################ 100.0%
Computing checksum with sha256sum
Checksums matched!
Now using node v17.1.0 (npm v8.1.2)
Creating default alias: default -> node (-> v17.1.0)

Once the installation is completed, verify the Node.js version with the following command:

# node --version

You should get the following output:

v17.1.0

You can also verify the npm version with the following command:

# npm -v

You should get the following output:

8.1.2

If you want to install the latest LTS version, run the following command:

Need a fast and easy fix?
✔ Unlimited Managed Support
✔ Supports Your Software
✔ 2 CPU Cores
✔ 2 GB RAM
✔ 50 GB PCIe4 NVMe Disk
✔ 1854 GeekBench Score
✔ Unmetered Data Transfer
NVME 2 VPS

Now just $43 .99
/mo

GET YOUR VPS
# nvm install --lts

You can now list all installed Node.js versions using the following command:

# nvm ls

You should get the following output:

-> v16.13.0
v17.1.0
system
default -> node (-> v17.1.0)
iojs -> N/A (default)
unstable -> N/A (default)
node -> stable (-> v17.1.0) (default)
stable -> 17.1 (-> v17.1.0) (default)
lts/* -> lts/gallium (-> v16.13.0)
lts/argon -> v4.9.1 (-> N/A)
lts/boron -> v6.17.1 (-> N/A)
lts/carbon -> v8.17.0 (-> N/A)
lts/dubnium -> v10.24.1 (-> N/A)
lts/erbium -> v12.22.7 (-> N/A)
lts/fermium -> v14.18.1 (-> N/A)
lts/gallium -> v16.13.0

If you want to switch to the current Node.js version, run the following command:

# nvm use 17.1.0

You should get the following output:

Now using node v17.1.0 (npm v8.1.2)

In the above tutorial, you learned three different ways to install Node.js on Debian 11 server. If you are not sure which Node.js version to install, consult the documentation of the application you are going to deploy.

Of course, you don’t have to do any of this if you use one of our Node.js VPS Hosting services, in which case you can simply ask our expert Linux admins to set up this for you. They are available 24×7 and will take care of your request immediately.

PS. If you liked this post please share it with your friends on the social networks using the buttons on the left or simply leave a reply below. Thanks.

Leave a Comment