Skip to main content

Installing Node.js on Ubuntu 22

Here's notes on how to install a single Node.js version on an Ubuntu 22 host.

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.

Ubuntu 22 comes with an existing version of node.js, v12, that is difficult to upgrade.

This is because the libnode-dev package is holding onto it, and we must release it, first.

Remove Conflicts

Run these to remove the conflicting package:

sudo apt-get remove libnode-dev

sudo apt-get update

Now, you can remove old versions of node.js:

sudo apt remove nodejs
cd /etc/apt/sources.list.d 
sudo rm nodesource.list
sudo apt --fix-broken install
sudo apt update
sudo apt remove nodejs
sudo apt remove nodejs-doc

Install Node

Once that’s done, you can 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