How to Install Node.js on Ubuntu 22.04

How to Install Node.js on Ubuntu 22.04. In this post, we introduce Node.js, its advantages, features then show you different ways to install Jode.js on Ubuntu 22.04 server.

What is Node.js?

First of all, Node.js is a JavaScript runtime environment that allows developers to write server side, create dynamic web page content, and build networking applications. It has an event loop, which means it handles multiple requests at the same time and it processes them in whichever order they come in. This makes Node.js very efficient for real time applications, like chat apps or live video streaming apps, because it doesn’t need to wait for one request to finish before handling the next one.

Ryan Dahl introduced Node.js in 2009, a JavaScript based server environment that is free and open source. The purpose of designing the platform was to help developers easily build web app development projects.

Additionally, Node.js also offers a non blocking I/O model that makes it lightweight and efficient, so it doesn’t use much memory or CPU resources, even when working with lots of data at the same time. Most developers also use the Node.js framework for writing command line tools.

What is more, Node.js is a popular platform that delivers high throughput and scalability as well as stores various JavaScript modules that aid in the development process. With the help of its modules, developers easily execute their apps on Linux, OS X, and Windows. Today, many high profile companies like Netflix, Uber, Walmart, eBay, etc., use Node.js.

Features of Node.js

  • Fast and Lightweight: Node.js is a lightweight, open source environment that is used to build scalable and fast web applications.
  • Event Driven: Non blocking I/O model with event looping makes Node.js different from other programming languages and environments like PHP, Ruby on Rails, ASP .NET, etc., as it does not block the execution of the code when waiting for an operation to complete or for data from an external source such as database or API call.
  • Do not buffer: The best part about the Node.js framework is it does not buffer data.
  • Fast Code Execution: Node.js uses V8 JavaScript Runtime motor to run codes fast.
  • Supports Asynchronous Programming: Node.js supports asynchronous programming which means that it runs multiple tasks in parallel without any blocking or waiting for one task to finish before executing the next one. Simply because of its event looping mechanism, which monitors all tasks and executes them in turn (when they are ready).

Advantages of Node.js

There are also a number of advantages that make it a good tool for developers to use in their projects. Check out some of the main benefits of the Node.js framework.

Scalable and Quick to Learn

Well, Node.js framework uses JavaScript for building web applications. No doubt, JavaScript is one of the most widely used programming languages, and most developers find it simple to work with. Also, Node.js is easy to learn and takes little time. Additionally, it provides excellent scalability and guarantees that the servers are efficiently handling requests.

Fast and Supports Asynchronous Programming

The Node.js framework is a fast, scalable, and lightweight framework that also provides excellent support for asynchronous programming and is famously non blocking, making it ideal for real time applications that require a constant flow of data from multiple sources.

Compatible with various systems

Compatibility with various operating systems, including Windows, Unix, Linux, and Mac OS X, as well as mobile devices. It creates a self sufficient executable commands when combined with the proper package.

Manage Multiple Requests at the Same Time

Node.js offers non blocking I/O systems, allowing developers to handle several requests at once. In nutshell, it effectively manages multiple requests at once when compared to other languages. Additionally, with Node.js, developers execute incoming requests faster and in a systematic manner.

High Level Performance

The V8 JavaScript engine from Google is used by Node.js to translate JavaScript scripts into machine code. Making code implementation simpler, quicker, and more efficient as a result. With Node.js, the code execution speed also gets improved because non blocking I/O operations are supported by the frameworks.

We shall move to main part of the article about how to install Node.js on Ubuntu 22.04.

How to Install Node.js on Ubuntu 22.04

In this section, we navigate through  three different ways to install Node.js on Ubuntu 22.04 server.

Requirements

  • An Ubuntu 22.04 server is installed on your system
  • A root user or a user with sudo privileges.

Update the System

Before starting, it is always a good idea to update all the system packages to the latest version. You can update all the packages with the following command.

				
					apt update -y
apt upgrade -y
				
			

Once all of your system packages are up to date. You then proceed to install Node.js.

Install Node.js From Ubuntu Repository

One of the simple and easiest ways to install Node.js is to install it from the Ubuntu default repository. You simply install it with the following command.

				
					apt install nodejs -y
				
			

After the installation, please verify the Node.js version with the following command.

				
					node -v
				
			

You get the Node.js version in the following output.

				
					v12.22.9
				
			

To install the NPM, run the following command.

				
					apt install npm -y
				
			

Now, verify the NPM version using the following command.

				
					npm -v
				
			

You should see the NPM version in the following output.

				
					8.5.1
				
			

To remove both Node.js and NPM from your system, run the following command.

				
					apt remove nodejs npm -y
				
			

Install Node.js from Node Source Repository

Since Ubuntu repository does not provide the latest version of Node.js. In this case, you add the Node source repository to your system to get the latest Node.js version.

First, install the Curl command line utility with the following command.

				
					apt install curl -y
				
			

Next, add the Node source repository to your system with the following command.

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

Once the Node.js repository is added to your system, you install the Node.js package using the following command.

				
					apt install nodejs -y
				
			

Now, verify the Node.js version using the following command.

				
					node -v
				
			

You should see the Node.js version in the following output.

				
					v18.13.0
				
			

Install Node.js Using NVM

NVM is a “Node.js Version Manager” that provides a simple and easier way to install and manage multiple versions of Node.js on a single local environment. It also helps developers to switch between different versions of Node with ease.

Firstly, install the NVM by running the NVM installation script.

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

Once NVM is installed on your server, run the following command to add the NVM path to your system.

				
					source ~/.bashrc
				
			

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

				
					nvm list-remote
				
			

You should see the following output.

				
					v18.12.0 (LTS: Hydrogen)
v18.12.1 (LTS: Hydrogen)
v18.13.0 (Latest LTS: Hydrogen)
v19.0.0
v19.0.1
v19.1.0
v19.2.0
v19.3.0
v19.4.0
				
			

From the above list, pick the Node version v19.0.1 and install it using the NVM command as shown below.

				
					nvm install v19.0.1
				
			

You should see the following output.

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

				
			

If you want to list all installed Node versions, run the following command.

				
					nvm list
				
			

You will get the following output.

				
					-> v19.0.1
default -> v19.0.1
iojs -> N/A (default)
unstable -> N/A (default)
node -> stable (-> v19.0.1) (default)
stable -> 19.0 (-> v19.0.1) (default)

				
			

Also  you mayverify the installed Node.js version using the following command.

				
					node -v
				
			

You should see the Node.js version in the following output.

				
					v19.0.1
				
			

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

				
					nvm install --lts
				
			

You will get the following output.

				
					Installing latest LTS version.
Downloading and installing node v18.13.0...
Downloading https://nodejs.org/dist/v18.13.0/node-v18.13.0-linux-x64.tar.xz...
######################################################################### 100.0%
Computing checksum with sha256sum
Checksums matched!
Now using node v18.13.0 (npm v8.19.3)
				
			

To verify all installed Node.js versions, run the following command.

				
					nvm ls
				
			

You should see the following output.

				
					-> v18.13.0
v19.0.1
default -> v19.0.1
iojs -> N/A (default)
unstable -> N/A (default)
node -> stable (-> v19.0.1) (default)
stable -> 19.0 (-> v19.0.1) (default)
				
			

If you want to switch from your default version to the 19.0.1 version, run the following command.

				
					nvm use 19.0.1
				
			

You should see the following output.

				
					Now using node v19.0.1 (npm v8.19.2)
				
			

To check your current Node.js version, run the following command.

				
					nvm current
				
			

You should see the following output.

				
					v19.0.1
				
			

If you want to remove Node.js version v18.13.0 from your system, run the following command.

				
					nvm uninstall v18.13.0
				
			

You will get the following output.

				
					Uninstalled node v18.13.0
				
			

Thank you for reading How to Install Node.js on Ubuntu 22.04. We shall conclude this article blog. 

How to Install Node.js on Ubuntu 22.04 Conclusion

In this post, we explained different ways to install Node.js on Ubuntu 22.04 server. You can now choose your preferred method to install Node.js depending on your requirements. Node.js is a full stack JavaScript, supporting both client side and server side applications. With its excellent features, you won’t need additional developers to handle the frontend and backend development independently. As a result, the Node.js frameworks helps you to save time and money.

Do explore our content about Node.js by navigating to this section of our blog. 

Avatar for Hitesh Jethva
Hitesh Jethva

I am a fan of open source technology and have more than 10 years of experience working with Linux and Open Source technologies. I am one of the Linux technical writers for Cloud Infrastructure Services.

0 0 votes
Article Rating
Subscribe
Notify of
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x