How to install Nodejs and NPM on Ubuntu 22.04

Written By :

Category :

Ubuntu

Posted On :

Share This :

First you need to check if there is any current version of node js installed on your ubuntu server by using this command.

node --version

If Node is already installed on the Ubuntu system then there should be some output.

Once you have verified version the next step is to install latest version.
At the time of writing this article the Latest Version of Node js20.8.0 which also includes NPM 10.1.0

Installation Instructions

If you have root access, you can omit the ‘sudo’ command as you already have full administrative privileges.

  • Download and import the Nodesource GPG key
sudo apt-get update
sudo apt-get install -y ca-certificates curl gnupg
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
  • Create deb repository
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

OptionalNODE_MAJOR can be changed depending on the version you need.
NODE_MAJOR=16
NODE_MAJOR=18
NODE_MAJOR=20

  • Run Update and Install
sudo apt-get update
sudo apt-get install nodejs -y

That’s it now the latest version of Node js and NPM should be available on your ubuntu system.
Updated version can be verified by using this command.

node --version && npm --version

Uninstall nodejs Ubuntu & Debian packages

To completely remove Node.js installed from the deb.nodesource.com package methods above:

apt-get purge nodejs &&\
rm -r /etc/apt/sources.list.d/nodesource.list &&\
rm -r /etc/apt/keyrings/nodesource.gpg