How Can I Update My NodeJS to the Latest Version on Ubuntu


NodeJS is one of the most popular tools for any type of project. It’s a JavaScript runtime environment, a cross-platform and open-source tool. Due to its versatile usage, NodeJS has huge active community support, leading to frequent minor updates. Developers practice NodeJS for the backend in MEAN or MERN Stack development.

There are several ways to update NodeJS to the latest version on Ubuntu using NPM (Node Package manager) and NVM (Node Version Manager).

Inspired by the importance of NodeJS, this post will address the possible methods to update NodeJS on Ubuntu. The process is explained on the latest distribution of Ubuntu, i.e., Ubuntu 22.04.

Method 1: Update NodeJS With NVM (Node Version Manager)

The first method, NVM, is the most preferred way of updating your NodeJS to the latest version or managing multiple versions of NodeJS.

Before going into the steps to update your NodeJS, use the update command to resynchronize your packages with their source files.

$ sudo apt update

As you can see, the update command has fetched the upgradeable packages. Now, let’s move on to updating NodeJS to the latest version.

Step 1: Installing NVM Using curl

We will use the curl method to install the NVM via the command.

$ curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.38.0/install.sh | bash

NVM is installed on your Linux machine.

Step 2: Verify NVM Installation

Before moving to the verification process, refresh your terminal using the command below, close it manually, or re-open it.

$ source ~/.bashrc

After restarting your terminal run this command to check your NVM version installed.

$ nvm --version

Step 3: NodeJS Version Already Installed

Let’s complete another step of the verification process: confirm which NodeJS you are using on your system.

$ nvm ls

This step will help in version control, and it will also help in deciding which NodeJS version you are on and which one to hop on next.

Step 4: Get a List of Versions to Update

With NVM; you can select your desired version of the NodeJS and update by using the ls-remote command. This will give you all the versions of NodeJS, including the most recent stable and most recent releases.

$ nvm ls-remote

After getting the list, copy the version number of the most recent version of NodeJS. Then use that number and the install command to install and update the NodeJS.

$ nvm install 19.8.1

With this now, you have updated your NodeJS to the latest version available. Follow the guide if you want to learn more ways of updating NodeJS with other methods.

Method 2: Update Node.js With NPM (Node Package Manager)

Node Package Manager or NPM is also a version control manager for Node. This is used to install, update, and remove the NodeJS packages.

Step 1: Clean the Cache File

Before starting with the actual updating of the NodeJS package, it is necessary to clear the cache file associated with NPM.

$ sudo npm cache clean -f

We are using sudo to get administrator privileges, and with the -f tag, the command will do a forced clear cache. As seen in the output, we have cleared the cache file of npm.

Step 2: Install Node’s Version Manager “n”

To install the update of the NodeJS using NPM, you will need first to install the version manager package, and to that, run the following command in the terminal.

$ sudo npm install -g n

Step 3: Update the Recent Stable Version of NodeJS

By running the following command, the NPM will automatically find the most recent stable version of NodeJS and install it on your system.

$ sudo n stable

Note: It is recommended to use the stable version as it has fewer bugs, errors, and issues.

From the output, it is visible that you have installed the stable Node version by running the stable version update command.

Install the Latest Version (Not Stable)

However, if you want to be the early bird and test all the latest features of NodeJS, you can use the latest tag with the n command. This will update the latest version of NodeJS on your machine.

$ sudo n latest

The stable version of NodeJS at the time was 18.15.0, updated when you used the stable command. However, using the latest tag, you have no update to the most recent version of NodeJS, 19.8.1.

You can manually update the latest package if your system is not picking up the latest Node version with automatic picking commands. Instead of using the stable of the latest tags, you can directly input the version number you are looking for. Another condition where this command is useful is installing a certain version to avoid clashes between development packages.

$ sudo n 19.8.1

Your NodeJS package is now updated to the latest version.

Wrap-Up

In this article, you have learned two different methods of upgrading the NodeJS package to the latest version on Ubuntu. The first method makes use of the NVM, and the second method utilizes NPM to update NodeJS to the latest version on Ubuntu, i.e., Jammy Jellyfish 22.04.

Print Friendly, PDF & Email
Categories