How To Install Node.js On Ubuntu 18.04

Introduction

Node.js is a powerful, open-source JavaScript runtime environment designed to enable the implementation of server-side JavaScript code. It's a cross-platform package that allows developers to run JavaScript code on a machine like a standalone application. The platform is primarily used to create robust back-end, server-side applications, but it's also useful as a front-end solution.

This tutorial will help you install Node.Js on Ubuntu 18.04 Bionic Beaver Linux via three options: from Ubuntu repository, from NodeSource repository, and using the Node Version Manager (NVM).

Part 1: Installing Node.js From Ubuntu 18.04 Repository

Node,js is a reliable package that is available from the repository of latest distribution of Ubuntu (18.04). In the first part of this tutorial will install Node.js and the Node.js Package Manager (NPM) from Ubuntu 18.04 repository.

Step one - Installing Node.js

First, update the packages index, then install the Node.js:

$ sudo apt update
$ sudo apt install nodejs

Next, execute the command below to verify this installation:

$ nodejs --version

This should give you the following output:

v8.10.0

The version of Node.js package in Ubuntu repository is referred to as nodejs and not node to avert conflict with another package in the repository.

Step 2 - Installing Node.js Package Manager (npm)

Installing Node.js Package Manager makes it easy to download npm packages. Run the command below to install npm:

$ sudo apt install npm

Once the installation is done, execute the command below to verify it:

$ npm --version

This will give you the following output:

3.5.2

Part two: Installing Node.js From NodeSource Repository

NodeSource is a popular company that offers enterprise-class Node support. The company maintains a reliable repository that features the most recent version of Node.js. Follow the steps below to install Node.js package and npm.

Step 1 - enabling NodeSource Repository

Execute the command below to activate the NodeSource repository:

$ curl -sL https://deb.nodesource.com/setup_8.x | sudo bash -

The command above adds a signing key for NodeSource to your system, creates a source repository file for apt, installs all the required packages, and refreshes the apt cache. The setup_8.x is an LTS version of Node.js;the value can be changed to version 10.x with setup_10.x.

Step 2 - Installing Node.js as well as the npm

By now the NodeSource repository is activated, and the stage is set for Node.js installation. Run the command below to install Node.js and the npm:

$ sudo apt install nodejs

To verify Node.js installation, execute the command below:

$ node --version

This will give you the following results:

V8.11.3

Next, run the command below to verify npm installation:

$ npm --version

This will give you the output below:

5.6.0

That is it! You have successfully installed Node.js from the NodeSource repository.

Part 3: Installing Node,js using the Node Version Manager (NVM)

NVM is a helpful bash script that is phenomenal at managing numerous versions of active Node.js. With NVM, it's a breeze to install or uninstall a specific version of Node.js you want to use. In this part of our tutorial, we’ll utilize NVM to install Node.js on Ubuntu 18.04.

Step 1- Downloading NVM Script

Execute the command below to download the install script for NVM:

$ curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.11/install.sh | bash

The script will automatically clone Node Version Manager repository from Github to ~/.nvm directory. In addition, it will add an nvm path to the ZSH or Bash profile.

The output of the download-nvm-script will look like this:

=> Close and reopen your terminal to start using nvm or run the following to use it now:

export NVM_DIR="$HOME/.nvm"
[ -s"$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh"  # This loads nvm
[ -s"$NVM_DIR/bash_completion" ] && . "$NVM_DIR/bash_completion"  # This loads nvm bash_completion

The output provides precise instructions, and you should reopen and close your terminal to add the nvm script path to the current session. Alternatively, you can execute the commands to get the same results.

Once you add the script to your PATH, run the command below to verify nvm installation:

$ nvm --version

This will give you the following output:

0.33.11

Step 2 - Using NVM To Install Node.js

The NVM is successfully installed which means, we can easily install Node.js. Run the command below to install Node.js:

$ nvm install node

The installation should not take long. Once done, run the command below to verify Node.js installation:

$ node --version

This will give you the output below:

v10.6.0

We can proceed and leverage the power of NVM to install two more Node.js versions. We can now install version 4.9.1 and the most recent  LTS version. To accomplish this, run the command below:

$ nvm install --lts
$ nvm install 4.9.1

Next, execute the following command, to view a list of the installed Node.js:

$ nvm ls

This will give you the output below:

how to install node.js on ubuntu 18.04

From the above output, you can clearly see that v4.9.1 is the Node.js version utilized in the current session. Besides, its clear v10.6.0 is the default version; this Node.js version will be active once a new shell is opened.

You can run the command below to switch the current active Node.js version:

$ nvm use 8.11.3

Next, execute the command below to verify it:

$ nvm current

This will give you the following output:

V8.11.3

To make v8.11.3 your default Node.js version run the command below:

 $ nvm alias default 8.11.3

Part 4: Further Implementations

Installing Development Tools

If you want to compile or install native add-ons from the Node.js Package Manager, you must install the required development tools. Now, execute the command below to install the required packages:

$ sudo apt install gcc g++ make

Uninstalling Node.js

Run the command below, to uninstall npm packages as well as Node.js:

$ sudo apt remove nodejs npm

Installing The Yarn Package Manager

Yarn is a helpful JavaScript Package Manager that integrates seamlessly with npm. The package is developed to remedy a plethora of issues with npm such as reducing network connectivity errors and speeding up the installation process.

To install Yarn, we’ll use the apt commands:

Step 1- Adding Yarn Yum Repository

Execute the commands below to add a GPG key and a repository:

$ curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | sudo apt-key add -
$echo"deb https://dl.yarnpkg.com/debian/ stable main" | sudo tee /etc/apt/sources.list.d/yarn.list

Step 2 - Installing Yarn

Since the repository has been added successfully, you can now run the following commands to install Yarn:

$ sudo apt update
$ sudo apt install yarn

Alternatively, if you are utilizing the Node.js Version Manager, use the commands below to skip:

$ sudo apt update
$ sudo apt install --no-install-recommends yarn

Next, run the commands below to verify the installation:

$ yarn --version

The output should be similar to:

1.7.0

Conclusion

That's it! The tutorial has detailed the three different methods you can use to install Node.js and its package manager (nvm). Of the three ways, it’s highly recommended you use the Node.js Version Manager as it delivers unprecedented flexibility for installing various versions of Node.js.

Check out these top 3 Best web hosting services

Was this article helpful?