Install Node.js on Debian 11

This tutorial is going to take you through how to Install Node.js on Debian 11. Node.js is a cross-platform, open-source back-end JavaScript runtime environment that runs JavaScript code outside of a web browser using the V8 engine. Developers can use Node.js to make command-line tools and server-side scripting, which involves running scripts on the server before transmitting the page to the user’s browser.

As a result, Node.js represents a “JavaScript everywhere” paradigm, uniting web application development under a single programming language rather than two separate languages for server-side and client-side scripts. Asynchronous I/O is possible thanks to Node.js’ event-driven architecture. These design choices are aimed at increasing throughput and scalability in web applications with a lot of input/output activities, as well as real-time Web applications (e.g., real-time communication programs and browser games).

How to Install Node.js on Debian 11

Node js version 16 is the most recent version with Long Term Support(LTS) at the time of writing this article.
Check the current version of Node.js on the Node.js website. We’ll go through three different ways to install node.js in this article.

Install Node.js on Debian 11 via Debian Repository

  • Start by updating your system using the command below.
sudo apt update
  • Then upgrade your system.
sudo apt upgrade -y
  • After upgrading your system Install the development tools you’ll need to compile source code and packages.
sudo apt install build-essential -y
  • Next install node.js using the following command.
sudo apt install nodejs

Sample output

Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following packages were automatically installed and are no longer required:
  default-jdk-headless libxt-dev
  openjdk-11-jdk-headless
Use 'sudo apt autoremove' to remove them.
The following NEW packages will be installed:
  nodejs
0 upgraded, 1 newly installed, 0 to remove and 1 not upgraded.
Need to get 26.2 MB of archives.
After this operation, 122 MB of additional disk space will be used.
Get:1 https://deb.nodesource.com/node_16.x focal/main amd64 nodejs amd64 16.14.2-deb-1nodesource1 [26.2 MB]
Fetched 26.2 MB in 6s (4,734 kB/s) 
Selecting previously unselected package nodejs.
(Reading database ... 228089 files and directories c
urrently installed.)
Preparing to unpack .../nodejs_16.14.2-deb-1nodesour
ce1_amd64.deb ...
Unpacking nodejs (16.14.2-deb-1nodesource1) ...
Setting up nodejs (16.14.2-deb-1nodesource1) ...
Processing triggers for man-db (2.9.1-1) ...
  • To check version installed run the command below.
node -v
  • Install the node package manager to enable the installation of Node.js modules and packages.
sudo apt install npm

You have successfully installed Node.js on Debian 11.

Install Node.js on Debian 11 via NodeSource PPA Repository

  • Start by installing curl using the command below.
sudo apt install curl
  • NodeSource’s PPA (personal package archive) can be used to install various Node.js versions.
  • We’re going to install node.js version 16 in this example. Use the command below to add APT sources list for the Nodesource Nodejs 16 repo.
curl -sL https://deb.nodesource.com/setup_16.x | sudo bash -
  • Next install node.js using the command below.
sudo apt -y install nodejs
  • Confirm node.js version installed.
node -v

Install Node.js on Debian 11 via Node Version Manager

  • Download and install Node Version Manager.
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/master/install.sh | bash
  • Run the following command to gain Access to NVM Functionality.
source ~/.bashrc
  • Using NVM, you can check which versions of Node.js are currently available.
nvm list-remote

Sample output

v14.17.5   (LTS: Fermium)
   v14.17.6   (LTS: Fermium)
   v14.18.0   (LTS: Fermium)
   v14.18.1   (LTS: Fermium)
   v14.18.2   (LTS: Fermium)
   v14.18.3   (LTS: Fermium)
   v14.19.0   (LTS: Fermium)
   v14.19.1   (Latest LTS: Fermium)
    v15.0.0
    v15.0.1
    v15.1.0
    v15.2.0
    v15.2.1
    v15.3.0
    v15.4.0
    v15.5.0
    v15.5.1
    v15.6.0
    v15.7.0
    v15.8.0
    v15.9.0
   v15.10.0
   v15.11.0
   v15.12.0
   v15.13.0
   v15.14.0
    v16.0.0
    v16.1.0
    v16.2.0
    v16.3.0
    v16.4.0
    v16.4.1
    v16.4.2
    v16.5.0
    v16.6.0
    v16.6.1
    v16.6.2
    v16.7.0
    v16.8.0
    v16.9.0
    v16.9.1
   v16.10.0
   v16.11.0
   v16.11.1
   v16.12.0
   v16.13.0   (LTS: Gallium)
   v16.13.1   (LTS: Gallium)
   v16.13.2   (LTS: Gallium)
   v16.14.0   (LTS: Gallium)
   v16.14.1   (LTS: Gallium)
   v16.14.2   (Latest LTS: Gallium)
    v17.0.0
    v17.0.1
    v17.1.0
    v17.2.0
    v17.3.0
    v17.3.1
    v17.4.0
    v17.5.0
    v17.6.0
    v17.7.0
    v17.7.1
    v17.7.2
  • Install the version of node.js that you like.
nvm install v16.14.2

Sample output

Downloading and installing node v16.14.2...
Downloading https://nodejs.org/dist/v16.14.2/node-v16.14.2-linux-x64.tar.xz...
############################################# 100.0%
Computing checksum with sha256sum
Checksums matched!
Now using node v16.14.2 (npm v8.5.0)
Creating default alias: default -> v16.14.2
  • You can see if you have several node js installed on your system by using the command below.
nvm ls

Sample output

->     v16.14.2
default -> v16.14.2
iojs -> N/A (default)
unstable -> N/A (default)
node -> stable (-> v16.14.2) (default)
stable -> 16.14 (-> v16.14.2) (default)
lts/* -> lts/gallium (-> v16.14.2)
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.11 (-> N/A)
lts/fermium -> v14.19.1 (-> N/A)
lts/gallium -> v16.14.2
  • If you have multiple versions installed, you can choose one, for example.
nvm use 16.14.2

Getting started with Node.js on Debian 11

  • Open your preferred text editor; in this case, I’ll use nano.
nano demo.js

Then paste the sample code below.

const http = require('http');

const hostname = '127.0.0.1';
const port = 5000;

const server = http.createServer((req, res) => {
  res.statusCode = 200;
  res.setHeader('Content-Type', 'text/plain');
  res.end('Hello from itnixpro');
});

server.listen(port, hostname, () => {
  console.log(`Server running at http://${hostname}:${port}/`);
});
  • If you’re using nano text editor, save the file by hitting ctrl+s and then close it by pressing ctrl+x.
  • Start your server by typing node followed by the name of your file, as shown below.
node demo.js
  • If everything works as expected, you should be able to visit your page in the browser using the URL and port that you specified, such as 127.0.0.1:5000 as shown below.
  • You have reached the end of the article, Congratulations. You have learned how to Install Node.js on Debian 11.

Read more on Node.js Documentation

Other Tutorials

Install PostgreSQL on Ubuntu 22.04

Install Docker on Ubuntu 22.04

Install pgAdmin on Ubuntu 22.04

System administrator | Software Developer | DevOps

Leave a Comment