How to Properly Upgrade Node.js to a Newer Version in Ubuntu

This beginner's tutorial discusses various ways to properly and carefully upgrade Node version on your Ubuntu system.
Warp Terminal

If you are using Node and NPM and need to upgrade Node to a newer version on Ubuntu, this tutorial will help you.

Please note that I am not talking about regular minor updates (node 16.x to 16.y) to your Node install. Like most software on Ubuntu, Node also gets minor updates along with the system update.

I am talking about major version upgrades here. For example, if you are using Node version 16.x and would like to upgrade it to version 18 or version 20.

It is unlikely that Ubuntu will provide a major new version upgrade to Node. This is why you should install Node directly from the official Nodesource repositories.

Node versions are a complicated mechanism. You should play attention to what version your Node application requires and what versions your distribution supports.

In this tutorial, I'll discuss these methods of Node version upgrade:

  • Installed using NodeSource Repository (recommended)
  • Update Node.js using the n package
  • Use NVM to get new Node versions

Method 1: Update Node.js through NodeSource Repository

This is the method we used when we had to upgrade our Ghost CMS to a newer version that required using Node version 18 instead of version 16.

🚧
If you are doing it on a production server, I strongly recommend taking a snapshot. In case your web service or application doesn't play well with the new Node version, you have the assurance of restoring it to original state.

You should visit the NodeSource Repository and check for the supported versions for your current distribution.

πŸ“‹
At the time of writing this, for Ubuntu 22.04 and Debian 12 Bookworm, Node.js version 18, 20, and 21 are supported.
Supported Node version for Ubuntu

Node provides a script for each active major version. You just have to replace YY with the version you want in the following command:

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

Let’s say you have installed version 18 of Node.js using the NodeSource repository, and you want to update it to version 21.

To do it, just run:

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

This will replace the sources file to point to version 21. Now, just update your system, to update the Node.js version to 21.

sudo apt update && sudo apt upgrade
A New Node.js Version available on system updates
A New Node.js Version available on system updates
🚧
Keep caution while updating NPM and don't upgrade to a broken version.

To upgrade the NPM to the latest version:

sudo npm install -g npm@latest

Method 2: Upgrade Node.js through NPM and N package

For obvious reasons, most of us install the Node Package Manager (NPM), during the installation of Node.js. You can update the Node.js using NPM.

To achieve this, you need the n package, which is available via NPM.

sudo npm install -g n

Now, to upgrade to an LTS version, you can use:

sudo n lts

Run node --version to check the version. If the change is not reflected, close the terminal and reopen it. Similarly, use the command below to reflect the change in the current shell itself:

hash -r
Update to the LTS version of Node.js using the n package.
Update Node.js using NPM

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

sudo n latest
Update to the latest version of Node.js using the n package manager.
Update Node.js to the latest version

You can change between the downloaded versions by making use of the n interactive selection.

sudo n

Scroll through the previously downloaded versions by you, select one, and press enter to make that version.

Change the Node.js version to another from the list of downloaded versions using "n" interactive
Change the Node Version

That’s it. You are good to go.

It is possible to remove all other versions except the currently installed version. Open a terminal and run:

sudo n prune
Remove the downloaded but uninstalled versions of Node.js using the n package.
Remove Uninstalled Node.js versions

Method 3: Update using Node Version Manager (NVM)

To get the latest LTS version of node and migrate your existing installed packages, use:

nvm install --reinstall-packages-from=current 'lts/*'

You can look for the latest releases available using:

nvm ls-remote --lts

To use the version you installed, you can use:

nvm use --lts

Now, if you need the latest supported NPM version on the current Node version, you can run on a terminal:

nvm install-latest-npm

Conclusion

As you can see, the entire Node version is a complex structure. You need to approach is carefully. See which version you need, which version is supported by your Linux system and then upgrade it. Production servers should be backed up properly before you do such changes.

I hope you find this tutorial helpful. Let me know if you have any questions.

About the author
Sreenath

Sreenath

Linux tinkerer by day, Lord of the Rings fanatic by night. When I'm not busy conquering the command line, you can find me kicking back and watching some football while quoting JRR Tolkien

It's FOSS

Making You a Better Linux User

It's FOSS

Great! You’ve successfully signed up.

Welcome back! You've successfully signed in.

You've successfully subscribed to It's FOSS.

Success! Check your email for magic link to sign-in.

Success! Your billing info has been updated.

Your billing was not updated.