How to update NodeJS to the latest version?

Introduction

In this short post we will update NodeJS to the latest version using npm (Node Package Manager). I will update  NodeJS 8.1.0 to NodeJS 10.13 in Ubuntu 18.04.

 

Update NodeJS using NPM

If you have NodeJS installed on your computer then you also have NPM, because they come together.

NPM (Node Package Manager) is usually used for installing packages in NodeJS that is it allows you to download and reuse the code written by other developers.

Besides that, NPM can also update itself and NodeJS. First, we need to find which versions of NPM and NodeJS we are using:

orkhans@matrix:~$ npm -v 
3.5.2 
orkhans@matrix:~$ node -v 
v8.10.0

In my case NPM is version 3.5.2 and NodeJS is v8.10.0.

 

Step 1. Update NPM

The following command updates NPM to the latest version:

orkhans@matrix:~$ sudo npm install npm@latest -g

After the completion of the program close the terminal and re-open it again. Then make sure that you have the updated version of NPM:

orkhans@matrix:~$ npm -v
6.4.1

 

Step 2. Install n package

Now we have to install n package which will update our NodeJS. As we already know we use NPM for managing packages, therefore install n package globally using the following command:

orkhans@matrix:~$ sudo npm install -g n
[sudo] password for orkhans: 
/usr/local/bin/n -> /usr/local/lib/node_modules/n/bin/n
+ n@2.1.12
added 1 package from 4 contributors in 1.052s

 

Step 3.  Update NodeJS using n

Now we will update NodeJS using n package. When using n we can use several options depending on which version of NodeJS we want to install.

n x.y.z – installs the specific version of NodeJS, for example: n 9.6.1

n latest –  installs the latest version of NodeJS

n stable – installs the latest stable version of NodeJS

n lts – installs the latest LTS version

 

I will install the latest LTS version( 10.13.0 at the moment of writing this post):

orkhans@matrix:~$ sudo n lts
     install : node-v10.13.0
       mkdir : /usr/local/n/versions/node/10.13.0
       fetch : https://nodejs.org/dist/v10.13.0/node-v10.13.0-linux-x64.tar.gz
   installed : v10.13.0
orkhans@matrix:~$ node -v
v10.13.0

 

Now I have version 10.13.0 installed as you can see in the output.

Conclusion

Now you will be able to easily update both NPM and NodeJS whenever you need. Thank you for reading:)

l

Tags:

Add a Comment