Friday, October 27, 2017

How to Easily Update NodeJS and NPM on Ubuntu 16.04LTS

The Problem

You are installing nodejs with apt-get on Ubuntu 16.04LTS and it is nodejs version 4.x but you want the latest version 8.x. You also want to be able to install updates easily as they are available.

The Solution

We are going to use node and npm it self to update our outdated nodejs and npm installation that came default from the Ubuntu packages. We accomplish this by using the very handy Node Version Manager from npm called "n". Yep it's only one letter called "n" :))

Let's get started

Use apt-get to install the default outdated version of nodejs and npm.
test@localhost:~$ sudo apt-get install nodejs
test@localhost:~$ sudo apt-get install npm
After it installs both of those packages it's time to update it to node 8.x. 

Update nodejs to 8.x

First we are going to install the very handy node package called "n". 
test@localhost:~$ sudo npm install -g n
It's time to update node it self. It's very easy just run this simple command.
test@localhost:~$ sudo n latest
You can confirm that your have the latest version by running the following command and you should see the output similar to this.
test@localhost:~$ node -v
v8.8.1
test@localhost:~$

Update npm to latest version

First check what version of npm you have currently installed by running this command. On my server it was 5.4.2 as you can see below.
test@localhost:~$ npm -v
5.4.2
test@localhost:~$ 
Now lets update npm using it self. After it installs run the same command as above to see what version you have.
test@localhost:~$ sudo npm install -g npm@latest
test@localhost:~$ npm -v
5.5.1
test@localhost:~$

Celebrate

There you have it. You are running now the latest version of NodeJS and NPM on Ubuntu 16.04LTS. You can us "n" to install other versions of node and switch between them. Here are the links for reference.
You can find me on twitter @LZAntal  or instagram @LZAntal