How to Install Node.js on Ubuntu 22.04

Choose a different version or distribution

Introduction

Before we begin talking about how to install Node.js on Ubuntu 22.04, let's briefly understand – What is Node.js?

Node.js is a powerful and popular runtime environment that allows developers to build server-side applications using JavaScript. It uses an event-driven, non-blocking I/O model, making it efficient and scalable. Node.js has a vast ecosystem of libraries and frameworks, enabling developers to create fast and lightweight web applications.

With its ability to handle many concurrent connections, Node.js is ideal for real-time applications, APIs, and microservices. Its versatility and easy integration with databases and front-end frameworks make it a top choice for modern web development.

In this tutorial, you will install Node.js on Ubuntu 22.04 in three different ways. We will also address a few FAQs on how to install Node.js on Ubuntu 22.04.

Advantages of Node.js

  1. Efficient and Scalable: Node.js uses an event-driven, non-blocking I/O model, making it highly efficient and scalable for handling numerous concurrent connections.
  2. JavaScript Everywhere: Node.js allows developers to use JavaScript for both server-side and client-side development, promoting code reusability and faster development cycles.
  3. Vast Ecosystem: Node.js has a rich ecosystem of libraries and frameworks, providing developers with a wide range of tools to build robust and feature-rich applications.
  4. Real-time Applications: With its ability to handle real-time data and events, Node.js is ideal for building applications that require instant updates and communication between clients and servers.
  5. Easy Integration: Node.js seamlessly integrates with databases, front-end frameworks, and third-party services, enabling developers to easily build and connect various components of an application.

Prerequisites to Install Node.js on Ubuntu 22.04

This tutorial assumes you're running Ubuntu 22.04. You should set up a non-root user account with sudo privileges on your system before you start.

Method 1 – Installing Node.js with Apt from the Default Repositories

Node.js is included in Ubuntu 22.04's default repositories and can be used to provide a consistent user experience across many platforms. The version in the repositories as of this writing is 12.22.9. Although this won't be the most recent version, it should be reliable and sufficient for quick language experimentation.

Warning: Node.js version 12.22.9, which is part of Ubuntu 22.04, is an LTS release, or long-term support release. Although it is officially out of date, it should still be supported until Ubuntu 24.04 is released.

You can use the apt package manager to get this version. First, update your local package index by typing:

sudo apt update

Next install Node.js:

sudo apt install nodejs

When asked to confirm installation, press Y. Press ENTER to accept the defaults, and continue if you are prompted to restart any services. Verify the version number of node to ensure that the installation was successful:

node -v
Output
v12.22.9

This is all there is, getting started with Node.js if the package in the repositories meets your needs. Most of the time, you'll also need to install npm, the Node.js package manager. Install the npm package using apt to accomplish this:

sudo apt install npm

You can use this to install Node.js modules and packages.

At this point, you've used apt and the default Ubuntu software repositories to successfully install Node.js and npm. The following section will demonstrate how to install different versions of Node.js using an alternate repository.

You can install an alternative version of Node.js by using a PPA (personal package archive) maintained by NodeSource.

Method 2 – Installing Node.js with Apt Using a NodeSource PPA

You can use a NodeSource PPA (personal package archive) to install an alternative version of Node.js. Node.js is available in more versions on these PPAs than in the official Ubuntu repositories. As of the time of writing, versions 14, 16, and 18 of Node.js are available.

To have access to the PPA's packages, we must first install it. Use curl to retrieve the installation script for your desired version from your home directory, ensuring to change 18.x with your preferred version string (if different).

cd ~
curl -sL https://deb.nodesource.com/setup_18.x -o nodesource_setup.sh

Further information about the available versions can be found in the NodeSource documentation.

Using nano (or your chosen text editor), you may check the contents of the downloaded script:

nano nodesource_setup.sh

In this case, NodeSource applies their own logic to ensure the necessary instructions are being provided to your package manager based on distro and version requirements. Using third party shell scripts is not always seen as a recommended practise. Exit your editor and run the script with sudo if you are convinced it is secure to do so.

sudo bash nodesource_setup.sh

Your configuration will include the PPA, and your local package cache will be immediately updated. Now follow the same steps as in the previous section to install the Node.js package. Before installing the new version, it might be a good idea to completely uninstall your older Node.js packages with sudo apt remove nodejs npm.

sudo apt install nodejs

Run node with the -v version flag to confirm that the new version has been installed:

node -v
Output
v18.7.0

You don't need to install npm separately because the NodeSource nodejs package already includes the node binary.

By using apt and the NodeSource PPA, you have now successfully installed Node.js and npm. The following section will demonstrate how to use the Node Version Manager to install and manage several versions of Node.js.

Method 3 – Installing Node Using the Node Version Manager

Using nvm, the Node Version Manager, is another flexible method of installing Node.js. You can simultaneously install and maintain numerous independent Node.js versions and their associated Node packages with this piece of software.

Visit the project's GitHub page to learn how to install NVM on your Ubuntu 22.04 system. From the README file that appears on the home page, copy the curl command. You can obtain the installation script's most recent version by doing this.

It is usually a good idea to audit the script to ensure it isn't doing anything you disagree with before piping the command through to bash. The | bash segment at the end of the curl command can be removed to do that:

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

Take a look and make sure you're satisfied with the modifications. Run the command once more with | bash appended after you are finished. Depending on the most recent nvm version, the URL you use will change, but as of right now, the script can be downloaded and executed by typing:

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

Your user account will now have the nvm script installed. You must first source your .bashrc file before using it:

source ~/.bashrc

You can now ask NVM which Node versions are available:

nvm list-remote
Output
. . .
       v16.11.1
       v16.12.0
       v16.13.0   (LTS: Gallium)
       v16.13.1   (LTS: Gallium)
       v16.13.2   (LTS: Gallium)
       v16.14.0   (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

It's a lengthy list! By typing any of the release versions you see, you can install a specific version of Node. For example, to get version v16.14.0 (another LTS release), type:

nvm install v16.14.0

You can see which versions you have installed by typing:

nvm list
Output
->     v16.14.0
default -> v16.14.0
iojs -> N/A (default)
unstable -> N/A (default)
node -> stable (-> v16.14.0) (default)
stable -> 16.14 (-> v16.14.0) (default)
lts/* -> lts/gallium (-> v16.14.0)
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.10 (-> N/A)
lts/fermium -> v14.19.0 (-> N/A)
lts/gallium -> v16.14.0

The first line of this displays the current version (-> v16.14.0), which is followed by a few named aliases and the versions those aliases point to.

Note: You might notice a system entry here if you also have a version of Node.js installed through apt. You can use the nvm use system to activate the system-installed version of Node.

These aliases can also be used to install a release. Use the following commands, for instance, to install fermium:

nvm install lts/fermium
Output
Downloading and installing node v14.19.0...
Downloading https://nodejs.org/dist/v14.19.0/node-v14.19.0-linux-x64.tar.xz...
################################################################################# 100.0%
Computing checksum with sha256sum
Checksums matched!
Now using node v14.19.0 (npm v6.14.16)

Using the same method as in the previous sections, you may confirm that the installation was successful by typing:

node -v
Output
v14.19.0

As anticipated, the proper version of Node is installed on our system. There is also a compatible version of npm.

Removing Node.js

Depending on how it was installed, Node.js can be removed using either apt or nvm. Use apt remove to remove the version from the system repositories:

sudo apt remove nodejs

By default, apt remove keeps any local configuration files created since installation. Use apt purge if you don't want to keep the configuration files for future use:

sudo apt purge nodejs

Find out if the Node.js version you installed with nvm is the most recent active version before uninstalling it:

nvm current

If the version you're aiming for isn't the one that's currently in use, you can try these things:

nvm uninstall node_version
Output
Uninstalled node node_version

The chosen Node.js version will be uninstalled using this command.

You must first deactivate nvm in order to enable your modifications if the version you want to delete is the one that is currently in use:

nvm deactivate

Using the uninstall command from earlier, you may now remove the current version. This deletes all files related to the targeted Node.js version.

FAQs to Install Node.js on Ubuntu 22.04

How can I check the installed version of Node.js?

To check the installed version of Node.js, open the terminal and type node -v. It will display the installed version number.

Does installing Node.js also install npm?

Yes, npm (Node Package Manager) is installed automatically when you install Node.js. You can verify its installation by running npm -v in the terminal.

Can I install a specific version of Node.js?

Yes, you can install a specific version of Node.js using the n or nvm package managers. They allow you to manage and switch between different Node.js versions easily.

How do I update Node.js to the latest version?

To update Node.js to the latest version, you can use the Node Version Manager (nvm). Run the command nvm install node --reinstall-packages-from=node to update to the latest stable version.

What is the LTS version of Node.js?

LTS (Long-Term Support) is a stable version of Node.js with long-term support for production use. You can install the LTS version by using nvm install --lts or check the official Node.js website for the latest LTS release.

How do I uninstall Node.js from Ubuntu 22.04?

To uninstall Node.js from Ubuntu 22.04, use the command sudo apt remove nodejs in the terminal. It will remove the Node.js package from your system.

Can I install global npm packages without sudo?

Yes, you can avoid using sudo for installing global npm packages by changing the npm configuration. Follow the instructions in the npm documentation to configure a different global installation directory.

Are there alternative methods to install Node.js on Ubuntu 22.04?

Yes, apart from the official package manager, you can install Node.js using nvm (Node Version Manager) or by downloading the binary package from the Node.js website and manually installing it.

Conclusion

Node.js can be installed and used in a variety of ways on an Ubuntu 22.04 server. Your circumstances will determine which of the methods listed above is best for you. The simplest option is to use the bundled version from the Ubuntu repository, although there is also more freedom when using nvm or a NodeSource PPA.

If you have any suggestions or queries, kindly leave them in the comments section.