How to Install Node.js on Ubuntu 18.04 Bionic Beaver Linux

Objective

The objective is to install Node.js the cross-platform JavaScript runtime environment on Ubuntu 18.04 Bionic Beaver Linux from standard Ubuntu 18.04 repository or by use of Node Version Manager, NVM.

This tutorial is available for other Ubuntu Versions:

Operating System and Software Versions

  • Operating System: – Ubuntu 18.04 Bionic Beaver

Requirements

Privileged access to your Ubuntu System as root or via sudo command is required.

Conventions

  • # – requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command
  • $ – requires given linux commands to be executed as a regular non-privileged user

Other Versions of this Tutorial

Ubuntu 20.04 (Focal Fossa)

Instructions

Install Node.js using Standard Ubuntu 18.04 Repository

Perhaps the fastest and easiest way to install Node.js on Ubuntu 18.04 is to perform an installation from a standard Ubuntu 18.04 repository. This will also ensure that you end up with a most stable and tested Node.js version at the cost of slightly lower version number. The following linux command:

$ sudo apt install nodejs

Additionally, you may want to also install Node.js package manager npm:

$ sudo apt install npm

Confirm installed versions:

$ nodejs --version
v6.12.0
$ npm --version
3.5.2

To remove Node.js execute:

$ sudo apt purge nodejs


Install Node.js using NodeSource

NodeSource is a former PPA repository for Node.js. To install Node.js version 8 execute:

$ curl -sL https://deb.nodesource.com/setup_8.x | sudo -E bash -
$ sudo apt-get install -y nodejs

For Node.js version 10 run:

$ curl -sL https://deb.nodesource.com/setup_10.x | sudo -E bash -
$ sudo apt-get install -y nodejs

Test the Node.js version:

$ nodejs --version
v10.7.0

Install Node.js using Node Version Manager (NVM)

Using NVM is most flexible and recommended way to install Node.js if the Node.js version installed from the above standard Ubuntu 18.04 repository does not fit your needs. NVM allows you to install any Node.js version as well as switch between Node.js versions in a very simple manner. Let’s begin by installation of Node Version Manager. Update the version number if necessary:

$ wget -qO- https://raw.githubusercontent.com/creationix/nvm/v0.33.8/install.sh | bash

To update your shell environment with new NVM settings either close and re-open your terminal session or enter:

$ source ~/.profile

NVM should now be available:

$ nvm --version
0.33.8

As already mentioned, nvm allows for an install of any available Node.js version. The following linux command will list all Node.js versions available:

$ nvm ls-remote

For example, to find the latest long term support version you may run:

$ nvm ls-remote | grep -i "latest lts"
         v4.8.7   (Latest LTS: Argon)
        v6.12.3   (Latest LTS: Boron)
         v8.9.4   (Latest LTS: Carbon)

Once you have chosen the Node.js version eg. v8.9.4 you may install it using the following linux command:

$ nvm install 8.9.4

All done. Check versions:

$ node --version
v8.9.4
$ npm --version
5.6.0


Basic guide to NVM

Switch Node.js Version

Given that you have installed the Node.js version you wish to use, you can switch between version by using use option:

$ nvm use 9.5.0
Now using node v9.5.0 (npm v5.6.0)

List all Node.js Installed versions

To list all currently installed Node.js versions run:

$ nvm ls
         v8.9.4
->       v9.5.0

Set default Node.js Version

To set default Node.js version execute:

$ nvm alias default 8.9.4
default -> 8.9.4 (-> v8.9.4)

Then to use pre-set default Node.js version simply run:

$ nvm use default
Now using node v8.9.4 (npm v5.6.0)

Remove Node.js

In order to remove currently-active Node.js version you first must deactivate it or switch to another version. Once ready you can remove any Node.js version using:

$ nvm uninstall 8.9.4
Uninstalled node v8.9.4