How to Install Node.js on Ubuntu 22.04? (Step-By-Step NodeJS Installation on Linux)

Published on December 27, 2024, Updated on January 06, 2025

Node.js is a runtime environment that allows developers to use JavaScript for building server-side applications. With its scalability and the familiarity of JavaScript from web development, it is a popular choice for backend development.

This guide will explore how to install Node.js on Ubuntu 20.04 server. 

Why Node.js is Popular?

Node.js is widely utilized for creating scalable and real-time web applications. Some common use cases include:

  • Developing RESTful APIs for seamless communication between services.
  • Implementing microservices architecture for modular and efficient applications.
  • Managing data streaming applications, such as those for video or audio content.
  • Creating command-line tools and automation scripts.
  • Enabling IoT applications to handle communication between devices and the cloud.
  • Setting up proxy servers or reverse proxies to balance traffic across multiple servers.
  • Designing scheduling tools, such as apps for planning social media posts like on Facebook.

Prerequisites

  • An Ubuntu 20.04 server should be configured and ready.
  • A non-root user account with sudo privileges to perform administrative tasks.

Methods How to Install Node.js on Ubuntu 22.04

You can install Node.js on the Ubuntu 22.04 system using three different methods that are listed below:

1. Installing Node.js Using Apt Repository

This approach is straightforward and suitable for most users. Ubuntu 20.04 includes a version of Node.js in its default repositories, ensuring compatibility and consistency across different systems. 

As of this writing, we are using the available version of 12.22.9. While this is not the latest release, it is stable and suitable for basic experimentation with Node.js. To install this version, update your local package index using the apt package manager.

$ sudo apt update

Install Node.js on Ubuntu 1

To install node.js on Ubuntu 22.04 using apt repository execute the following command:

$ sudo apt install nodejs

Install Node.js on Ubuntu 2

You will be asked to confirm the installation prompt. Press ‘y’ to proceed with the node.js installation.

Install Node.js on Ubuntu 3

To check the installed node.js version on Ubuntu 22.04 use this command:

$ node -v

Install Node.js on Ubuntu 4

If the version of Node.js available in the repositories meets your requirements, you're all set up. However, in most cases, you'll also need to install npm, the Node.js package manager. To do this, simply run:

$ sudo apt install npm

This will enable you to install modules and packages for use with Node.js.

2. Installing Node.js with Node Version Manager (NVM) (Install NVM Ubuntu)

This option is ideal for developers who need to manage and switch between multiple Node.js versions for active development.

For most scenarios, the default repository installation is sufficient. If you need a particular version, the PPA method is recommended. For developers working on multiple projects requiring different Node.js versions, nvm is the most flexible solution.

A more flexible method for installing Node.js is by using NVM (Node Version Manager). This tool allows you to install and manage multiple versions of Node.js and their associated packages independently. 

At this point, you’ve successfully installed Node.js and npm using apt and the default Ubuntu software repositories. The next section will guide you on how to use an alternative repository to install different versions of Node.js.

To install NVM Ubuntu 20.04 system, go to the project's GitHub page and copy the curl command from the README file. This will fetch the latest version of the installation script.

Before running the script through bash, it's a good practice to review the script to ensure it does not perform any unwanted actions. You can do this by omitting the | bash part at the end of the curl command.

Examine the script carefully to ensure you're comfortable with the changes it will make. Once you're confident, run the command again, appending | bash at the end. The URL may vary depending on the latest version of NVM, but currently, you can download and execute the script with the following:

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

Install Node.js on Ubuntu 5

This will install the NVM script for your user account. To start using it, you'll need to source your .bashrc file first:

$ source ~/.bashrc

You can now use NVM to check which versions of Node.js are available:

$ nvm list-remote

Install Node.js on Ubuntu 6

To install a specific version of Node.js, simply use one of the release versions listed. For example, to install version v20.18.1, run:

$ nvm install v20.18.1

Install Node.js on Ubuntu 7

3. Installing Node.js via a NodeSource PPA

If you need a specific version of Node.js, you can use a PPA (Personal Package Archive) provided by NodeSource. These PPAs offer more version options than the default Ubuntu repositories. As of now, Node.js versions 18 and 20 are available.

To begin, add the NodeSource PPA to access its packages. From your home directory, use curl to download the installation script for your desired version, replacing 20.x with the version number you prefer if it's different.

$ cd ~

$ curl -sL https://deb.nodesource.com/setup_20.x -o /tmp/nodesource_setup.sh

Check the NodeSource documentation for details about the available Node.js versions.

You can review the content of the downloaded script using nano or any text editor of your choice.

$ nano /tmp/nodesource_setup.sh

Once you have reviewed the script and ensured it is safe to execute, close the editor. Then, run the script with sudo.

$ sudo bash /tmp/nodesource_setup.sh

The PPA will be added to your system, and your local package cache will be updated automatically. After that, you can install the Node.js package just like before.

$ sudo apt install nodejs

To confirm the installation of the new version, run the node command with the -v flag to check its version.

$ node -v

The NodeSource nodejs package includes both the Node.js binary and npm, so there’s no need to install npm separately. Now, you have successfully installed both Node.js and npm using apt and the NodeSource PPA. 

In the following section, we will guide you on how to use Node Version Manager (NVM) to install and manage multiple versions of Node.js.

How to Uninstall Node.js on Ubuntu 22.04?

You can uninstall Node.js either using apt or nvm, depending on how to install node.js on Ubuntu 22.04.

If the node.js is installed on your system using the apt repository then you can remove the Node.js package installed from the system repositories, running:

$ sudo apt remove nodejs

By default, this will keep any local configuration files created after installation. If you want to remove these configuration files as well, use apt purge:

$ sudo apt purge nodejs

If you installed Node.js using nvm, first check which version is currently active:

$ nvm current

If the version you want to remove is not the active one, you can uninstall it by running:

$ nvm uninstall node_version

This will remove the specified version of Node.js.

If the version you want to uninstall is the active version, deactivate it first to apply changes:

$ nvm deactivate

After deactivating, you can uninstall the current version with the same nvm uninstall command, which will remove all associated files for that version of Node.js.

Conclusion

As a web developer focusing on either frontend or backend development, having Node.js installed on your system is essential. We demonstrated in this guide several methods of how to install Node.js on Ubuntu 20.04 server, and the best choice depends on your specific requirements.

Installing the version from Ubuntu’s default repository is straightforward while using NVM or a NodeSource PPA provides greater flexibility for managing different versions. For further guidance on working with Node.js, explore tutorials on coding with Node.js.

Searching for a Trusted VPS or Dedicated Server Provider? Host World is Here for You!

Are you in need of a reliable hosting solution for your VPS or dedicated server? Or perhaps you’re looking for high-quality SSL services to secure your connections? Look no further—Host World offers a full range of hosting solutions tailored to your needs. With a global reach and a commitment to exceptional customer service, Host World is the go-to choice for businesses around the world.

Why wait? Experience unparalleled hosting services today. Choose Host World and take your business to the next level!