How to Install Node.js and npm on Ubuntu 22.04


5 min read

Install Node.js and npm on Ubuntu 22.04

Node.js is a cross-platform, open-source JavaScript runtime environment built on Chrome’s JavaScript, designed to execute JavaScript code outside a web browser. It is generally used to build fast and scalable server-side and networking applications. npm is the default package manager for Node.js and also the name of the world’s largest software registry.

In this post, we will explore three different ways of installing Node.js and npm on Ubuntu 22.04:

  • From the standard Ubuntu repositories. This is the easiest way to install Node.js and npm on Ubuntu and should be sufficient for most use cases. The version included in the Ubuntu repositories is v12.22.9.
  • From the NodeSource repository. Use this repository to install a different Node.js version than the one provided in the Ubuntu repositories. Currently, NodeSource supports Node.js v18.x, v17.x, v16.x, and v14.x.
  • Using nvm (Node Version Manager). This tool allows you to have multiple Node.js versions installed on the same machine. If you are Node.js developer, then this is the preferred way of installing Node.js.

Choose the installation method that is most appropriate for your environment. If you are not sure which Node.js version to install, consult the documentation of the application you’re going to deploy.

Installing Node.js and npm from the Ubuntu repository

At the time of writing, the Node.js version included in the default Ubuntu 22.04 repositories is v12.22.9 which is an older TLS version.

The installation is pretty straightforward. Run the following commands to update the package index and install Node.js and npm:

sudo apt updatesudo apt install nodejs npm

The command above will install a number of packages, including the tools necessary to compile and install native addons from npm.

Once done, verify the installation by running:

nodejs -v

Installing Node.js and npm from NodeSource

NodeSource is a company focused on providing enterprise-grade Node support. It maintains an APT repository containing multiple Node.js versions. Use this repository if your application requires a specific version of Node.js.

At the time of writing, NodeSource repository provides the following versions:

  • v18.x - The latest stable version.
  • v17.x
  • v16.x - The latest LTS version.
  • v14.x

We’ll install Node.js version 18.x:

  1. Run the following command as a user with sudo privileges to download and execute the NodeSource installation script:

    curl -sL | sudo -E bash -

    The script will add the NodeSource signing key to your system, create an apt repository file, install all necessary packages, and refresh the apt cache.

    If you need another Node.js version, for example, 16.x, change the setup_18.x with setup_16.x.

  2. Once the NodeSource repository is enabled, install Node.js and npm:

    sudo apt install nodejs

    The nodejs package includes both the node and npm binaries.

  3. Verify that the Node.js and npm were successfully installed by printing their versions:

    node -v
    npm -v

To be able to compile native addons from npm you’ll need to install the development tools :

sudo apt install build-essential

Installing Node.js and npm using NVM

NVM (Node Version Manager) is a bash script that allows you to manage multiple Node.js versions on a per-user basis. With NVM you can install and uninstall any Node.js version that you want to use or test.

Visit the nvm GitHub repository page and copy either the curl or wget command to download and install the nvm script:

wget -qO- | bash

Do not use sudo as it will enable nvm for the root user.

The script will clone the project’s repository from Github to the ~/.nvm directory:

=> Close and reopen your terminal to start using nvm or run the following to use it now:

export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/" ] && \. "$NVM_DIR/"  # This loads nvm
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"  # This loads nvm bash_completion

As the output above says, you should either close and reopen the terminal or run the commands to add the path to nvm script to the current shell session. You can do whatever is easier for you.

Once the script is in your PATH, verify that nvm was properly installed by typing:

nvm -v

To get a list of all Node.js versions that can be installed with nvm, run:

nvm list-remote

The command will print a vast list of all available Node.js versions.

       v14.19.2   (LTS: Fermium)
       v14.19.3   (Latest LTS: Fermium)
       v16.14.2   (LTS: Gallium)
       v16.15.0   (Latest LTS: Gallium)

To install the latest available version of Node.js, run:

nvm install node

The output should look something like this:

Now using node v18.2.0 (npm v8.9.0)
Creating default alias: default -> node (-> v18.2.0)

Once the installation is completed, verify it by printing the Node.js version:

node -v

Let’s install two more versions, the latest LTS version (16.15.0) and version 14.19.3:

nvm install --ltsnvm install 14.19.3

You can list the installed Node.js versions by typing:

nvm ls

The output should look something like this:

->     v14.19.3
default -> node (-> v18.2.0)
iojs -> N/A (default)
unstable -> N/A (default)
node -> stable (-> v18.2.0) (default)
stable -> 18.2 (-> v18.2.0) (default)
lts/* -> lts/gallium (-> v16.15.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.12 (-> N/A)
lts/fermium -> v14.19.3
lts/gallium -> v16.15.0

The entry with an arrow on the right (-> v14.19.3) is the Node.js version used in the current shell session, and the default version is set to v18.2.0. The default version is the version that will be active when opening new shells.

If you want to change the currently active version, enter:

nvm use 16.15.0
Now using node v16.15.0 (npm v8.5.5)

To change the default Node.js version, run the following command:

nvm alias default 16.15.0

For more detailed information about using the nvm script, visit the project’s GitHub page.


We have shown you three ways to install Node.js and npm on your Ubuntu 22.04 machine. The method you choose depends on your requirements and preferences. Even though installing the packaged version from the Ubuntu or NodeSource repository is easier, the nvm method gives you more flexibility for adding and removing different Node.js versions on a per-user basis.

Feel free to leave a comment if you have any questions.