Update Node.js on Ubuntu
How To

Update Node.js version in Ubuntu

Pratham Patel
Pratham Patel

Table of Contents

Node.js is an open source JavaScript runtime environment. It is also cross-platform.

The easiest way to upgrade Node.js is using the apt package manager. Do so with the following command:

sudo apt-get install --only-upgrade nodejs

There are other ways to upgrade Node.js manually. To know how to do that, continue reading.

Check Node.js version

Before you actually upgrade Node.js, do check if the currently installed version is what you want or it is outdated.

To check the version of Node.js, run the following command:

node -v

This is what it looks like on my computer:

$ node -v
v12.22.9

I have version 12.22.9 installed (v12.x).

At the time of writing this, v18.x and v16.x are the latest and LTS versions, respectively.

Method 1: Upgrade from Ubuntu's repositories

The recommended method to upgrade and install any software on your Linux distribution is to use the first-party repositories. The maintainers have gone through the effort of making all software compatible with each other, removing most dependency issues.

So, the easiest way to upgrade Node.js to a newer version is to install it from the official repositories. To do that, use the following command:

sudo apt-get install --only-upgrade nodejs

If there are any upgrades, you will be able to install them just like any other normal software package.

Method 2: Install and update the latest Node.js from official repositories

The problem with using Ubuntu provided Node.js is that it gets outdated (not insecure) over time.

To install an even newer version of Node.js than what is available in Ubuntu's repositories, you need to use the repositories made available by Node.js themselves.

But, before you add Node.js repositories, remove the nodejs package that was installed from Ubuntu's repositories. This is to prevent dependency conflicts. It won't remove installed npm packages.

Run the following command to do so:

sudo apt-get remove nodejs

Below are the commands to add Node.js repositories based on the version available in said repository:

Node.js v18.x:

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

Node.js v17.x:

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

Node.js v16.x:

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

Node.js v14.x:

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

Node.js LTS:

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

Node.js Current (whatever is the latest version):

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

Choose a version that you want/need and run the respective command to add the repository.

💡
This will work even if you have already installed Node from the Node.js repository. Say, you are using Node.js version 16 and you want to upgrade to version 18, just use the installer script of your desired version as specified above. The script rewrites the entry in /etc/apt/sources.list.d/nodesource.list file with the new version details.

Once the repository is added, run the following command to install node.js:

sudo apt-get install nodejs

That will install the desired version of node.js for you. The updates will be provided along with the system updates.

React is also installed along with Node. So, you can start your React JS project immediately.

That's about it. Let me know if you have any questions.