How to Install Node.js & NPM on Ubuntu 22.04 LTS

Node.js is an open-source, cross-platform, back-end JavaScript runtime environment built on Chrome’s V8 engine to build fast and scalable network applications and back-end APIs. Node.js uses an event-driven, non-blocking IO module that makes it very lightweight and practical. It is a fantastic choice for data-intensive real-time applications across distributed devices.

NPM is a package manager for the JavaScript programming language maintained by NPM, Inc. NPM is the default package manager for the JavaScript runtime environment Node.js and is arguably the most available repository for Node.JS packages.

In the following tutorial, you will learn various methods of installing Node.js & NPM on Ubuntu 22.04 LTS Jammy Jellyfish desktop or server, along with some tips on using NVM with Node.js.

Update Ubuntu

First, ensure your system is up-to-date by running the following command before proceeding to avoid any conflicts.

sudo apt update && sudo apt upgrade

Install Required Packages

For users that plan to install using the NVM or NodeSource instead of the Ubuntu 22.04 default repository, please run the following command to install the required packages.

sudo apt install curl gnupg2 gnupg -y

Install Node.js Node.js & NPM – APT Method with Ubuntu Repository

The first and easiest method to install Node.js uses the Ubuntu repository. At the time of this tutorial, Ubuntu Jammy Jellyfish’s version comes within its repository is Node.JS 12.

To install the default version, use the following command.

sudo apt install nodejs

Install Node.js & NPM – APT Method with NodeSource Repository

The second and perhaps better way to install Node.js is to import and install the NodeSource repository with the LTS or latest release option. Currently, at the time of the creation of this tutorial, the current release is 18, and the LTS version is 16, but these will automatically change when the following current or lts version changes in time.

Import one of these two options below.

Node.js Current

Import the Node.js current repository.

curl -fsSL https://deb.nodesource.com/setup_current.x | sudo -E bash -

Next, install Node.js using the following command.

sudo apt install nodejs

Node.js LTS

Import the Node.js LTS repository.

curl -fsSL https://deb.nodesource.com/setup_lts.x | sudo bash -

Next, install Node.js using the following command.

sudo apt install nodejs

If you are chasing a specific version of Node.js, you can run the installation command to suit what version you require. Please note that if you are chasing multiple versions to install, using the NVM method to install Node.js can be more beneficial.

Import/Install Node.js 18

Import:

curl -fsSL https://deb.nodesource.com/setup_18.x | sudo -E bash -

Install:

sudo apt install nodejs

Import/Install Node.js 17

Import:

curl -fsSL https://deb.nodesource.com/setup_17.x | sudo -E bash -

Install:

sudo apt install nodejs

Import/Install Node.js 16

Import:

curl -fsSL https://deb.nodesource.com/setup_16.x | sudo -E bash -

Install:

sudo apt install nodejs

Import/Install Node.js 14

Import:

curl -fsSL https://deb.nodesource.com/setup_14.x | sudo -E bash -

Install:

sudo apt install nodejs

Import/Install Node.js 12

Import:

curl -fsSL https://deb.nodesource.com/setup_14.x | sudo -E bash -

Install:

sudo apt install nodejs

Install Node.js & NPM – APT Method with NVM

The last method is to install Node.js using the NVM manager using the following command.

curl https://raw.githubusercontent.com/creationix/nvm/master/install.sh | bash

NVM creates an environment entry to login to the current user session, which can be activated immediately using the following command.

source ~/.bashrc

Alternatively, log out and back in on your current session on your system.

Next, use the nvm list-remote command to list all available node versions available:

nvm list-remote

Example output:

How to Install Node.js & NPM on Ubuntu 22.04 LTS

Note that this list is extensive when using the command, and the above is just a sample of the output.

Next, install a specific version of Node from the output list above. The syntax would be nvm install <version number>.

Example only:

nvm install 18.0.0

Example output:

How to Install Node.js & NPM on Ubuntu 22.04 LTS

Typically, you will probably have more than one version of Node.js installed using the NVM.

However, you may want to list all versions on your system, which can be done using the following nvm ls command.

nvm ls

Example output:

How to Install Node.js & NPM on Ubuntu 22.04 LTS

From the output list, you can run or swap to a specific version as follows. In the case of the tutorial machine, the current version is 18.0.0 but let us change this to 16.15.0 with the example command.

nvm use 16.15.0

Example output:

How to Install Node.js & NPM on Ubuntu 22.04 LTS

Comments and Conclusion

In the tutorial, you have learned how to install Node.JS & NPM on Ubuntu 22.04 LTS Jammy Jellyfish desktop or server.

Overall, most developers would use the NodeSource repositories depending on what environment you are working in to determine which version you will use. Still, my recommendation is to use NVM if you are a developer, and it makes development easy by installing multiple versions and swapping between them.



Follow LinuxCapable.com!

Like to get automatic updates? Follow us on one of our social media accounts!