How To Install Node.js on Ubuntu 22.04

Node.js is a runtime environment that enables the execution of JavaScript code on the server side. It is designed to be lightweight and efficient, using the V8 JavaScript runtime. While Node.js is compatible with various operating systems, including Linux, its primary purpose is not confined to any specific OS.

The main goal of Node.js is to facilitate the development of scalable and high-performance server-side applications. It is particularly well-suited for creating web applications and APIs, employing an event-driven, non-blocking I/O model that enhances its efficiency in handling numerous concurrent connections.

In this article, we will be providing an in-depth detail on how to install Node.js on Ubuntu 22.04.

Method 1: Installing Node.js Using the Default apt Package Manager

Step 1:Updating Package Lists

Start by updating the package lists to fetch the latest information about available software.

sudo apt update && sudo apt upgrade
sudo apt update && sudo apt upgrade

Step 2: Installing Node.js Using the Package Manager

The next step involves installing Node.js using the package manager. This ensures a hassle-free installation and easy future updates.

sudo apt install nodejs

Verifying the Installation

Verify that Node.js has been successfully installed by checking its version.

node -v
node -v

Method 2: Installing Node.js Using the PPA Repository

The primary purpose of a PPA is to offer a convenient way for users to access software that may not be included in the default Ubuntu repositories. It enables developers to deliver the latest versions of their software, experimental features, or custom builds directly to users who choose to add the PPA to their system. This decentralized approach enhances the flexibility and responsiveness of the Ubuntu ecosystem, as users can easily obtain and install software that aligns with their specific needs or preferences.

However, it’s essential to exercise caution when adding PPAs, as they are maintained by individuals or groups outside the official Ubuntu development team. Users should only trust PPAs from reliable sources to ensure the security and stability of their system.

Installing Node.js using PPA Repository

We need to first make sure that curl is installed as it is needed to access the desired PPA repository on Ubuntu 22.04:

$ sudo apt install curl
sudo apt install curl

Next, we need to execute the below-mentioned commands to successfully install the Node.js:

sudo apt-get update && sudo apt-get install -y ca-certificates curl gnupg
curl -fsSL https://deb.nodesource.com/gpgkey/nodesource-repo.gpg.key | sudo gpg --dearmor -o /etc/apt/keyrings/nodesource.gpg
NODE_MAJOR=20
echo "deb [signed-by=/etc/apt/keyrings/nodesource.gpg] https://deb.nodesource.com/node_$NODE_MAJOR.x nodistro main" | sudo tee /etc/apt/sources.list.d/nodesource.list
sudo apt-get update && sudo apt-get install nodejs -y
sudo apt-get update && sudo apt-get install -y ca-certificates curl gnupg

These commands will update your package lists, install necessary dependencies, retrieve the GPG key for the Node Source repository, add the repository to your system, and finally install Node.js. Make sure to execute these commands in the order provided.

Method 3: Installing Node.js using Node Version Manager (NVM)

NVM allows you to easily install and switch between different versions of Node.js on your system. This is particularly useful when you’re working on projects that require specific Node.js versions, as NVM enables you to manage those versions seamlessly.

With NVM, you can install multiple Node.js versions side by side, and switch between them based on the requirements of different projects. This flexibility is valuable because different projects may depend on different Node.js versions due to compatibility or feature requirements.

Step 1: Install Node Version Manager

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

The command downloads the NVM installation script from the specified URL using curl and then immediately executes the script using bash. The purpose is to automate the installation of NVM on your system. Once NVM is installed, you can use it to manage and install different versions of Node.js.

Step 2: List All Version of Node.js

To list all versions of Node.js we can use  the below command:

$ source ~/.bashrc
$ nvm list-remote
$ source ~/.bashrc

Next, we need to scroll down to find the latest available versions of Node.js and then install any of them as per our requirements:

$ nvm install v21.6.2
nvm install v21.6.2

Installing Node Package Manager (NPM)

After installing Node.js, it is recommended to install npm (Node Package Manager) as well if you already didn’t. The NPM is the default package manager for Node.js, which is a JavaScript runtime that allows you to execute JavaScript code on the server side. It is used to manage and install packages (libraries and tools) for Node.js projects.

$ sudo apt install npm
sudo apt install npm

Uses of Node.js in Various Applications

  • Web Servers: It is commonly utilized for constructing web servers responsible for managing HTTP requests and responses. Its non-blocking architecture allows it to handle multiple connections simultaneously.
  • API Development: Node.js is widely adopted for building RESTful APIs, enabling different components of a software system to communicate seamlessly.
  • Real-time Applications: Due to its event-driven nature, Node.js proves effective in real-time applications like chat platforms, online gaming, and collaborative tools.
  • Microservices: Node.js is a popular choice for constructing microservices architecture, where small, independent services communicate to form a cohesive application.
  • Build Tools: Node.js is integrated into various build tools and task runners, such as Grunt and Gulp, to automate repetitive tasks in the development process.

Conclusion

 Node.js is a handy tool for running JavaScript on servers. It’s lightweight, works on different systems, and is great for building fast and scalable server applications. The article explains three ways to install Node.js on Ubuntu 22.04. Method 1 uses the default package manager for a straightforward setup. Method 2 talks about adding a repository for more flexibility but advises caution. Method 3 introduces NVM, a tool that lets you easily manage different Node.js versions. The article also touches on where Node.js shines, like making web servers, APIs, real-time apps, microservices, and build tools. Overall, Node.js is a solid choice for various server-side needs.

If you’re a developer diving into app development, ensure your hosting setup can handle Node.js demands. Ultahost offers reliable NodeJS hosting with dedicated resources for guaranteed speed and stability, ideal for resource-intensive tasks.

FAQ

What is Node.js?
Why install Node.js on Ubuntu 22.04?
How do I check if Node.js is already installed on my Ubuntu 22.04 system?
How do I check the installed Node.js version?

Related Post

How to Enable or Disable IPV6 on Ubuntu Linux

IPv6, also known as Internet Protocol version 6, offers...

How to Install Chromium on Ubuntu 22.04

Chromium is an open-source web browser project that ser...

How to Setup Apache Virtual Hosts on Ubuntu 2

Apache Virtual Hosts offer a powerful way to manage mul...

How to List Linux Users on Ubuntu

Among the complexity of Linux systems user accounts ser...

How to Install Perl on Ubuntu

Perl, short for Practical Extraction and Reporting Lang...

How to Install Fail2ban on Ubuntu 22.04

Fail2Ban provides a protective shield for Ubuntu 22.04 ...

Leave a Comment