After a few hours of trying to install the latest version of Node.js / npm and failing to do it using the package manager because the package on the repositories is not the latest. I came across a few tutorials and below are the steps that are guaranteed to work.
Open your terminal window and run the following commands
sudo apt-get update
sudo apt-get install build-essential libssl-devEither
wget -qO- https://raw.githubusercontent.com/creationix/nvm/v0.31.3/install.sh | NVM_DIR=$HOME/.nvm bash
OR
curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.31.3/install.sh | NVM_DIR=$HOME/.nvm bashexport NVM_DIR=$HOME/.nvm
[ -s $NVM_DIR/nvm.sh ] && . $NVM_DIR/nvm.sh
To check if the nvm is installed and running correctly run the command below, it should output: nvm
command -v nvm
Run the following command to see the list of versions available and to see the latest version
nvm ls-remote
After checking the list in the output, find the latest version, you should see these words beside it:
(Latest LTS: Argon). At the moment of writing this it was v4.4.7
Run the commands
nvm install v4.4.7
npm install npm -g
To make sure the installation is successful, run the following commands and you should see the version
node -v
npm -v
That is it.