Skip to main content

Installing Node.js on Ubuntu

This page includesshows twohow methodto of installinginstall Node.js on an Ubuntu 22 host.
One

as

It only shows how to install a single, global Node.js version.
The

other,

If usingyou need multiple Node.js version support, see this page to work with NVM (Node Version Manager.Manager): NVM - Node.js Version Manager

NOTE: If you will need to work with multiple versions of Node.js on the same host, such as performing automated builds of Angular apps for different Angular versions, then these steps won't work for you.
Instead, you need to install NVM to manage those versions of Node.js.

Removing Old Versions

Before installing Node.js, it may beis necessary to remove any existing version of Node from the host.

Removing Old Versions

Be sure to remove any existing versions of Node.js before continuing.
Normally, you can do this through the package manager that installed it.

NOTE: If you're running Ubuntu 22, you will need to follow this: Ubuntu 22: Removing Existing Node.js

Check if Node.js was installed via APT: 

dpkg -l | grep nodejs

If found, remove it with this:

sudo apt remove --purge nodejs npm -y
sudo apt autoremove -y

If Node.js was installed, via NVM (Node Version Manager), and you want to remove a version that NVM is maintaining, do this:

nvm uninstall <version>

Ex; nvm uninstall 16 or nvm uninstall 18

Install Single Node.js Version

NOTE: This will install a single Node.js version.
See the later section for installing multiple Node.js versions.

Use this to install the desired version of Node.js:

sudo apt 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
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
sudo apt-get update
sudo apt-get install nodejs -y

Verify the node version with this: sudo node -v

If you wanted a single Node.js version, you can stop here.

Installing Multiple Node.js Versions (Using NVM)

From a terminal, navigate to your home folder with: cd ~/

Run the following:

curl -fsSL https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.5/install.sh | bash

Then, add the following to your shell profile (~/.bashrc, ~/.profile, or ~/.bash_profile):

export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"

Apply Changes:

source ~/.bashrc

Verify NVM is installed:

nvm --version

Once confirmed installed, you can install the desired Node.js versions that NVM will manage for you.

Install Managed Node.js Version

Follow these steps for each version of Node.js that NVM will manage.

Once installed, you need to tell NVM to install the desired Node.js versions that it will manage for you:

nvm install 16

nvm install 20

Usage

With the desired versions of Node.js installed, you can use these commands to switch between them:

To run Node.js v16 (for Angular v14):

nvm use 16.20.2

To run Node.js v20 (for Angular v17):

nvm use 20.18.3

NOTE: Each time you tell NVM to switch versions, it may require answering a UAC popup.

And once NVM has switched in the desired Node.js version, you can call this, to verify:

node -v

image.png