How to Update Node.js to the Latest Version

update node version

Node.js is an open-source and cross-platform JavaScript runtime environment used to run JavaScript code on the server-side. It is primarily used for non-blocking, event-driven servers, traditional web sites and back-end API services.

You already know how to install Node.js and NPM using three different ways. If your application is running on the Node.js server then I would recommend updating Node.js version regularly to improve the security. There are several ways you can update your Node.js version in Linux system.

In this tutorial, you will learn –

Prerequisites

  • A server running Ubuntu 20.04 with Node.js Installed.
  • A root password is configured on your server.

Update Node.js Version Using NVM

The Simple and easiest way to update Node.js version is with Node Version Manager. NVM allows you to install and manage multiple Node.js versions.

First, update your system packages with the following command:

apt-get update -y

Next, go to the NVM Git Repository page then download and run the NVM installation script using the following command:

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

Next, close and reopen the terminal or run the following command to activate the profile.

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

Next, verify the NVM version with the following command:

nvm --version

Output:

0.37.2

Next, print a list of all available Node.js version using the following command:

nvm list-remote

Output:

       v14.15.0   (LTS: Fermium)
       v14.15.1   (LTS: Fermium)
       v14.15.2   (LTS: Fermium)
       v14.15.3   (LTS: Fermium)
->     v14.15.4   (Latest LTS: Fermium)
        v15.0.0
        v15.0.1
        v15.1.0
        v15.2.0
        v15.2.1
        v15.3.0
        v15.4.0
        v15.5.0
        v15.5.1
        v15.6.0

Next, install your desired version of Node.js from the above output as shown below:

nvm install node-version

For example, to install the Node.js version v15.6.0., run the following command:

nvm install v15.6.0

Update Node.js Version Using NPM

NPM stands for “Node Package Manager” is a package manager for Node.js. It is used for installing and managing Node.js dependencies.

If Node.js is installed in your system then you have NPM also installed.

First, clear the NPM cache using the command below:

npm cache clean -f

Next, you will need to install the “n” Node’s version manager in your system. You can install it with the following command:

npm install -g n

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

n stable

Output:

  installing : node-v14.15.4
       mkdir : /usr/local/n/versions/node/14.15.4
       fetch : https://nodejs.org/dist/v14.15.4/node-v14.15.4-linux-x64.tar.xz
   installed : v14.15.4 to /usr/local/bin/node
      active : v14.15.4 at /root/.nvm/versions/node/v14.15.4/bin/node

To install the latest release of Node.js, run the following command:

n latest

Output:

  installing : node-v15.6.0
       mkdir : /usr/local/n/versions/node/15.6.0
       fetch : https://nodejs.org/dist/v15.6.0/node-v15.6.0-linux-x64.tar.xz
   installed : v15.6.0 to /usr/local/bin/node
      active : v14.15.4 at /root/.nvm/versions/node/v14.15.4/bin/node

To install the specific (v15.0.0) Node.js version, run the following command:

n v15.0.0

Output:

  installing : node-v15.0.0
       mkdir : /usr/local/n/versions/node/15.0.0
       fetch : https://nodejs.org/dist/v15.0.0/node-v15.0.0-linux-x64.tar.xz
   installed : v15.0.0 to /usr/local/bin/node
      active : v14.15.4 at /root/.nvm/versions/node/v14.15.4/bin/node

Update Node.js Version from Binary Packages

You can also update the Node.js version by downloading Node.js tarball from its official website.

First, go to the Node.js official download page and download the latest version of Node.js tarball with the following command:

wget https://nodejs.org/dist/v14.15.4/node-v14.15.4-linux-x64.tar.xz

Once the package is downloaded, install the xz-utils package to extract the downloaded file:

apt-get install xz-utils -y

Next, extract the downloaded tarball and install the Node.js with the following command:

tar -C /usr/local --strip-components 1 -xvJf node-v14.15.4-linux-x64.tar.xz

Conclusion

In this post, you learned how to update Node.js version using three different ways. You can now choose your desired way to update the Node.js version.

About Hitesh Jethva

I am Hitesh Jethva Founder and Author at LinuxBuz.com. I felt in love with Linux when i was started to learn Linux. I am a fan of open source technology and have more than 15+ years of experience in Linux and Open Source technologies.

View all posts by Hitesh Jethva