How To Install Node.js on Ubuntu 20.04

Introduction

Node.js is a JavaScript runtime for server-side programming. It allows developers to create scalable backend functionality using JavaScript, a language many are already familiar with from browser-based web development.

In this guide, we will show you three different ways of getting Node.js installed on an Ubuntu 20.04 server:

  • using apt to install the nodejs package from Ubuntu’s default software repository
  • using apt with an alternate PPA software repository to install specific versions of the nodejs package
  • installing nvm, the Node Version Manager, and using it to install and manage multiple versions of Node.js

For many users, using apt with the default repo will be sufficient. If you need specific newer (or legacy) versions of Node, you should use the PPA repository. If you are actively developing Node applications and need to switch between node versions frequently, choose the nvm method.

Prerequisites

This guide assumes that you are using Ubuntu 20.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 following the initial server setup tutorial for Ubuntu 20.04.

Option 1 — Installing Node.js with Apt from the Default Repositories

Ubuntu 20.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 10.19. 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 first by typing:

  • sudo apt update

Then install Node.js:

  • sudo apt install nodejs

Check that the install was successful by querying node for its version number:

  • nodejs -v
Output
v10.19.0

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 installing the npm package with apt:

  • sudo apt install npm

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

At this point you have successfully installed Node.js and npm using apt and the default Ubuntu software repositories. The next section will show how to use an alternate repository to install different versions of Node.js.

Option 2 — Installing Node.js with Apt Using a NodeSource PPA

To install a different version of Node.js, you can use a PPA (personal package archive) maintained by NodeSource. These PPAs have more versions of Node.js available than the official Ubuntu repositories. Node.js v10, v12, v13, and v14 are available as of the time of writing.

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

  • cd ~
  • curl -sL https://deb.nodesource.com/setup_14.x -o nodesource_setup.sh

Refer to the NodeSource documentation for more information on the available versions.

Inspect the contents of the downloaded script with nano (or your preferred text editor):

  • nano nodesource_setup.sh

When you are satisfied that the script is safe to run, exit your editor, then run the script with sudo:

  • sudo bash nodesource_setup.sh

The PPA will be added to your configuration and your local package cache will be updated automatically. You can now install the Node.js package in the same way you did in the previous section:

  • sudo apt install nodejs

Verify that you’ve installed the new version by running node with the -v version flag:

  • node -v
Output
v14.2.0

The NodeSource nodejs package contains both the node binary and npm, so you don’t need to install npm separately.

At this point you have successfully installed Node.js and npm using apt and the NodeSource PPA. The next section will show how to use the Node Version Manager to install and manage multiple versions of Node.js.

Option 3 — Installing Node Using the Node Version Manager

Another way of installing Node.js that is particularly flexible is to use nvm, the Node Version Manager. This piece of software allows you to install and maintain many different independent versions of Node.js, and their associated Node packages, at the same time.

To install NVM on your Ubuntu 20.04 machine, visit the project’s GitHub page. Copy the curl command from the README file that displays on the main page. This will get you the most recent version of the installation script.

Before piping the command through to bash, it is always a good idea to audit the script to make sure it isn’t doing anything you don’t agree with. You can do that by removing the | bash segment at the end of the curl command:

  • curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.35.3/install.sh

Take a look and make sure you are comfortable with the changes it is making. When you are satisfied, run the command again with | bash appended at the end. The URL you use will change depending on the latest version of nvm, but as of right now, the script can be downloaded and executed by typing:

  • curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.35.3/install.sh | bash

This will install the nvm script to your user account. To use it, you must first source your .bashrc file:

  • source ~/.bashrc

Now, you can ask NVM which versions of Node are available:

  • nvm list-remote
Output
. . . v12.13.0 (LTS: Erbium) v12.13.1 (LTS: Erbium) v12.14.0 (LTS: Erbium) v12.14.1 (LTS: Erbium) v12.15.0 (LTS: Erbium) v12.16.0 (LTS: Erbium) v12.16.1 (LTS: Erbium) v12.16.2 (LTS: Erbium) v12.16.3 (Latest LTS: Erbium) v13.0.0 v13.0.1 v13.1.0 v13.2.0 v13.3.0 v13.4.0 v13.5.0 v13.6.0 v13.7.0 v13.8.0 v13.9.0 v13.10.0 v13.10.1 v13.11.0 v13.12.0 v13.13.0 v13.14.0 v14.0.0 v14.1.0 v14.2.0

It’s a very long list! You can install a version of Node by typing any of the release versions you see. For instance, to get version v13.6.0, you can type:

  • nvm install v13.6.0

You can see the different versions you have installed by typing:

nvm list
Output
-> v13.6.0 default -> v13.6.0 node -> stable (-> v13.6.0) (default) stable -> 13.6 (-> v13.6.0) (default) . . .

This shows the currently active version on the first line (-> v13.6.0), followed by some named aliases and the versions that those aliases point to.

Note: if you also have a version of Node.js installed through apt, you may see a system entry here. You can always activate the system-installed version of Node using nvm use system.

Additionally, you’ll see aliases for the various long-term support (or LTS) releases of Node:

Output
. . . lts/* -> lts/erbium (-> N/A) lts/argon -> v4.9.1 (-> N/A) lts/boron -> v6.17.1 (-> N/A) lts/carbon -> v8.17.0 (-> N/A) lts/dubnium -> v10.20.1 (-> N/A) lts/erbium -> v12.16.3 (-> N/A)

We can install a release based on these aliases as well. For instance, to install the latest long-term support version, erbium, run the following:

  • nvm install lts/erbium
Output
Downloading and installing node v12.16.3... . . . Now using node v12.16.3 (npm v6.14.4)

You can switch between installed versions with nvm use:

nvm use v13.6.0
Now using node v13.6.0 (npm v6.13.4)

You can verify that the install was successful using the same technique from the other sections, by typing:

  • node -v
Output
v13.6.0

The correct version of Node is installed on our machine as we expected. A compatible version of npm is also available.

Conclusion

There are a quite a few ways to get up and running with Node.js on your Ubuntu 20.04 server. Your circumstances will dictate which of the above methods is best for your needs. While using the packaged version in Ubuntu’s repository is the easiest method, using nvm or a NodeSource PPA offers additional flexibility.

For more information on programming with Node.js, please refer to our tutorial series How To Code in Node.js.

Originally posted on DigitalOcean Community Tutorials
Author: Brian Boucheron

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *