How to install Node.js and npm on Ubuntu 22.04

2 gen 2024 5 min di lettura
How to install Node.js and npm on Ubuntu 22.04
Indice dei contenuti

Introduction

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

In this tutorial we will explore three different ways to install Node.js and npm on Ubuntu 22.04:

  • From the NodeSource repository. Use this repository to install a different version of Node.js 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 versions of Node.js installed on the same computer. If you are a Node.js developer, this is the preferred way to install Node.js.
  • From the standard Ubuntu repositories. This is the easiest way to install Node.js and npm on Ubuntu. The version included in the Ubuntu repositories is 12.x However, Nodejs 10 is no longer maintained and should not be used in production.

Choose the installation method most appropriate for your environment. If you're not sure which version of Node.js to install, consult the documentation for the application you'll be deploying.

Installing Node.js and npm from NodeSource

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

The first step is to install the dependencies needed to add a new repository. These packages will most likely already be installed on your system, but some packages may be missing:

sudo apt update
sudo apt install ca-certificates curl gnupg

Next, import the Nodesource repository GPG key to your system:

sudo mkdir -p /etc/apt/keyrings
curl -fsSL https://deb.nodesource.com/gpgkey/nodesource-repo.gpg.key | sudo gpg --dearmor -o /etc/apt/keyrings/nodesource.gpg

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

  • v21.x - The latest stable version.
  • v20.x: The latest LTS version.
  • v18.x: The previous LTS version.
  • v16.x - Ed. EOL

We will install Node.js version 20.x. If you need another version of Node.js, for example, 18.x change NODE_MAJOR=20 to NODE_MAJOR=18. Run the following command to create the NodeSource repository file:

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

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

sudo apt update
sudo apt install nodejs

The nodejs package contains both node and npm binaries.

To verify that Node.js and npm have been installed correctly, run the following command to print their versions:

node --version
v20.10.0
npm --version
10.2.3

To be able to build native add-ons from npm, you will 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 versions of Node.js on a per-user basis. With NVM you can install and uninstall any version of Node.js you want to use or test.

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

wget -qO- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash

Don't use sudo, as it will enable it for the nvm root user.

The script will clone the project repository from GitHub into 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.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm
 [ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion" # This loads nvm bash_completion

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

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

nvm -v
0.39.7

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

nvm list-remote

The command will print a huge 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)
 v17.0.0
 v17.0.1...
 v18.1.0
 v18.2.0

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

nvm install node

The output should look like this:

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

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

node -v
v18.2.0

We install two more versions, the latest LTS version (16.15.0) and version 14.19.3:

nvm install --lts
nvm install 14.19.3

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

nvm ls

The output should look like this:

-> v14.19.3
 v16.15.0
 v18.2.0
 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 a right arrow ( -> v14.19.3 ) is the version of Node.js 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 version of Node.js, run the following command:

nvm alias default 16.15.0

For more detailed information on using the nvm script, visit the project's GitHub page.

Installing Node.js and npm from the Ubuntu repository

As of now, the Node.js version available in the Ubuntu 22.04 v12.22.9 repositories is no longer supported and will not receive security updates starting April 30, 2022.

The installation process is quite simple. Run the following commands to update the package index and install Node.js and npm:

sudo apt update
sudo apt install nodejs npm

The above command will install a number of packages, including the tools needed to compile and install native add-ons from npm.

Once finished, verify the installation by running:

nodejs -v
v12.22.9

Conclusion

We've shown you three ways to install Node.js and npm on your Ubuntu 22.04 computer. The method you choose depends on your needs and preferences. While installing the packaged version from the Ubuntu or NodeSource repository is easier, the nvm method offers more flexibility to add and remove different versions of Node.js on a per-user basis.

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

Support us with a

Successivamente, completa il checkout per l'accesso completo a Noviello.it.
Bentornato! Accesso eseguito correttamente.
Ti sei abbonato con successo a Noviello.it.
Successo! Il tuo account è completamente attivato, ora hai accesso a tutti i contenuti.
Operazione riuscita. Le tue informazioni di fatturazione sono state aggiornate.
La tua fatturazione non è stata aggiornata.