How to install any version of Node.js on Ubuntu 16.04 using NVM

How to install any version of Node.js on Ubuntu 16.04 using NVM

In this article we'll show you how to install Node.js on Ubuntu using NVM (Node Version Manager), a bash script that allows you to manage multiple active node.js versions.

1. Install Build Essentials and libssl-dev

Before installing NVM, you will need the build-essentials and libssl-dev packages. The build-essentials is a reference for all the packages needed to compile a debian package that generally includes the gcc/g++ compilers an libraries and some other utils. The libssl-dev package is part of the OpenSSL project's implementation of the SSL and TLS cryptographic protocols for secure communication over the Internet. As first step update the package lists using:

sudo apt-get update

And install the packages using:

sudo apt-get install build-essential libssl-dev

Once the installation finishes, you will be able to install NVM.

2. Download and run NVM installation script

The NVM installation is based on a SH file that you can download this script in the repository at Github here. Alternatively in case that you have curl available from the command line, you can download it directly using the following command:

Note

The script may have a newer version, so be sure to visit the repository to see which is it.

curl -sL https://raw.githubusercontent.com/creationix/nvm/v0.33.7/install.sh -o install_nvm.sh

Once the download finishes, run the script with bash:

bash install_nvm.sh

This will start the installation of NVM and will take a time. Once the installation finishes, you can delete the installation script (install_nvm.sh) of NVM using:

rm install_nvm.sh

After the installation of NVM, you need to restart the console (or remote sesssion) in order to be able to use nvm.

3. Installing a Node.js version

If you already restarted the console or remote session (in case of accessing ubuntu from a SSH terminal), you will be able to use nvm on the command line. As previously mentioned, NVM allows you to install and use any version of Node.js, so the first you need to know is the list of versions that you can retrieve with the following command:

nvm ls-remote

This will list all the available versions of Node.js:

...
v7.8.0
v7.9.0
v7.10.0
v7.10.1
v8.0.0
v8.1.0
v8.1.1
v8.1.2
...
v8.9.1
v8.9.2
v8.9.3
v9.0.0
v9.1.0
v9.2.0
v9.2.1

As next you can proceed with the installation of the version that you want. At the date of this article, the latest available versions are Stable 8.9.3 LTS and 9.2.1 for the latest features. In this case we want the stable version 8.x so we'll install it with NVM using:

nvm install 8.9.3

The installation process of the specified Node version will start:

Downloading https://nodejs.org/dist/v8.9.3/node-v8.9.3-linux-x64.tar.xz...
######################################################################## 100.0%
Now using node v8.9.3 (npm v5.5.1)
Creating default alias: default -> 8.9.3 (-> v8.9.3)

After the installation finishes and the first version is defined as default, you can use Node.js from your command line. You can test it printing the version of Node.js with the following command (in our case it will output 8.9.3):

node -v

The version with the alias as default will be automatically used everytime on your terminal.

4. Installing another version of Node.js

NVM is fortunately for the developers very flexible. Sometimes some Node.js based projects will use the latest features of Node.js (using a non-stable version of Node), so you may want to install an extra version and switch according to your needs. In this case we'll install Node 9.2.1 along with the 8.x version:

nvm install 9.2.1

The installation process will start:

Downloading https://nodejs.org/dist/v9.2.1/node-v9.2.1-linux-x64.tar.xz...
######################################################################## 100.0%
Now using node v9.2.1 (npm v5.5.1)

And as shown in the message, you will be using now the 9.2.1 version, however what happened to the 8.9.3 version? Don't worry it is available too, thanks to NVM you can switch from one version to another using the following command:

Note

With this command you can switch from one version to another whenever you need to as long as the version is available (installed).

nvm use 8.9.3

You can do the same with any installed version and you can enjoy of Node.js in your Ubuntu server. Every distribution has npm installed by default and every version is package independent (a plugin installed on a specific version won't be installed automatically on others).

Happy coding !

This could interest you

Become a more social person