Install Node.js on Ubuntu 22.04

This tutorial is going to take you through on how to Install Node.js on Ubuntu 22.04. Node.js is a cross-platform, open-source back-end JavaScript runtime environment that uses the V8 engine to execute JavaScript code outside of a web browser. Node.js allows developers to utilize JavaScript to create command-line tools and server-side scripting, which involves running scripts on the server before sending the page to the user’s browser. As a result, Node.js symbolizes a “JavaScript everywhere” paradigm, bringing web application development together around a single programming language rather than separate languages for server-side and client-side scripts.

The event-driven architecture of Node.js allows for asynchronous I/O.
These design choices are intended to improve throughput and scalability in web applications that have a lot of input/output activities, as well as for real-time Web applications (e.g., real-time communication programs and browser games).

How to Install Node.js on Ubuntu 22.04

At the time of writing this article, node js version 16 is the current with the Long Term Support (LTS). Navigate to Node,js website and check the current version.
In this article we are going to install node.js in 3 different methods which includes.

Install Node.js on Ubuntu 22.04 via Default Repository

  • Start by updating your system using the command below.
sudo apt update
  • 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) ...
  • Check which version of node.js is installed using the command below.
node -v
  • After node js installation, Install the node package manager which will allow you to install Node.js modules and packages.
sudo apt install npm

You have successfully installed Node.js on Ubuntu 22.04.

Install Node.js on Ubuntu 22.04 via NodeSource PPA Repository

  • PPA (personal package archive) published by NodeSource can be used to install various Node.js versions.
  • In this example we are going to install node.js version 16. Create the APT sources list for the Nodesource Nodejs 16 repo using the command below.
curl -sL https://deb.nodesource.com/setup_16.x | sudo bash -
  • The install node.js using the following command.
sudo apt -y install nodejs
  • Confirm node.js version installed.
node -v

Install Node.js on Ubuntu 22.04 via Node Version Manager

  • First things first, download and install Node Version Manager.
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/master/install.sh | bash
  • Then add Access to NVM using the command below.
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
  • Next, install node.js version of your choice.
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
  • Using the command below, you can see if you have several node js installed on your system.
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
  • In case you have several versions installed, you can switch to specific version e.g.
nvm use 16.14.2

Introduction to Node.js on Ubuntu 22.04

  • Fire up your favorite text editor, in this example I will 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}/`);
});
  • Save the file by pressing ctrl+s then close it by pressing ctrl+x if you are using nano text editor in your example.
  • Start your server by running the command node followed by your file name as shown below.
node demo.js
  • If everything goes well, you should be able to access your page in the browser through URL and port that was specified in the code above e.g. 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 Ubuntu 22.04.

Read more on Node.js Documentation

Other Tutorials

Install MongoDB compass GUI on Ubuntu 22.04

Install Sublime Text on Ubuntu 22.04

Install AngularJS on Ubuntu 22.04

System administrator | Software Developer | DevOps

Leave a Comment