How To Install Node.js on Ubuntu 18.04 LTS

Blog primary image

Introduction

Node.js is a run time environment for JavaScript execution at server side. For general-purpose programming Node.js allows users to build web / network applications quickly. By leveraging JavaScript on both the front and backend, Node.js makes development more consistent and integrated.

In this guide, we'll show you how to get started with Node.js on an Ubuntu 18.04 LTS server.

Prerequisites

This guide assumes that you are using Ubuntu 18.04. Before you begin, you should have a non-root user account with sudo privileges set up on your system. You can learn how to do this by watching the video on how to install Ubuntu 18.04 as VM.

 Installing the Distro-Stable Version for Ubuntu

Ubuntu 18.04 contains a version of Node.js in its default repositories that can be used to provide a consistent experience across multiple systems. At the time of writing, the version in the repositories is 8.10.0. This will not be the latest version, but it should be stable and sufficient for quick experimentation with the language.

To get this version, you can use the apt package manager. Refresh your local package index by typing: 

$ sudo apt update

Install Node.js from the repositories:

$ sudo apt install nodejs

If the package in the repositories suits your needs, this is all you need to do to get set up with Node.js. In most cases, you'll also want to also install npm, the Node.js package manager. You can do this by typing:

$ sudo apt install npm

 This will allow you to install modules and packages to use with Node.js.

To check which version of Node.js and npm you have installed after these initial steps, type:

$ node -v
$ npm -v

 

Installing Using a PPA

To get a more recent version of Node.js you can add the PPA (Personal Package Archive) maintained by NodeSource. This will have more up-to-date versions of Node.js than the official Ubuntu repositories (previous step), and will allow you to choose between Node.js v6.x (supported until April of 2019), Node.js v8.x (the current LTS version, supported until December of 2019), Node.js v10.x (the second current LTS version, supported until April of 2021), and Node.js v11.x (the current release, supported until June 2019).

First, install the PPA in order to get access to its contents. From your home Downloads directory, use curl to retrieve the installation script for your preferred version, making sure to replace 10.x with your preferred version string (if different):

$ cd Downloads
$ curl -sL https://deb.nodesource.com/setup_10.x -o nodesource_setup.sh

You can inspect the contents of this script with nano (or your preferred text editor):

$ nano nodesource_setup.sh

Run the script under sudo:

$ sudo bash nodesource_setup.sh

The PPA will be added to your configuration and your local package cache will be updated automatically. After running the setup script from Nodesource, you can install the Node.js package in the same way you did above:

$ sudo apt install nodejs

To check which version of Node.js you have installed after these initial steps, type:

$ node -v
v10.15.3

The nodejs package contains the nodejs binary as well as npm, so you don't need to install npm separately.

npm uses a configuration file in your home directory to keep track of updates. It will be created the first time you run npm. Execute this command to verify that npm is installed and to create the configuration file:

$ npm -v
6.4.1

In order for some npm packages to work (those that require compiling code from source, for example), you will need to install the build-essential package:

$ sudo apt install build-essential

 

Comments