How To Install Node.js on Ubuntu 20.04 – DigitalOcean

Getting Started Node

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

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

use apt to install the nodejs package from the default Ubuntu software repository use

  • apt
  • with an alternative PPA software repository to install specific versions of the nodejs package
  • install nvm, the Node Version Manager

  • , and use it to install and manage multiple versions of Node.js

For many users, using apt with the default repository will suffice. 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 To

follow this guide, you will need to set up an Ubuntu 20.04 server. Before you begin, you must have a non-root user account with sudo privileges configured on your system. You can learn how to do this by following the Ubuntu 20.04 server initial setup tutorial.

Option 1 — Installing Node.js with Apt from 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 won’t be the last 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. Update local package index first

: sudo apt update

Then install Node.js

:

  1. sudo apt
  1. install nodejs

Verify that the installation was successful by querying node for its version number:

  1. node -v

Outputv10.19.0

If the package in the repositories suits your needs, this is all you need to do to configure with Node.js. In most cases, you’ll also want to install npm, the Node.js package manager. You can do this by installing the

npm package with apt:

  1. sudo apt

install npm

This allows you to install modules and packages for use with

Node.js.

At this point, you have successfully installed Node.js and npm using apt and Ubuntu’s default 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 file) maintained by NodeSource. These PPAs have more versions of Node.js available than the official Ubuntu repositories. Node.js v16 and v18 are available at the time of writing.

First, install the PPA to gain access to your packages. From your home directory, use curl to retrieve the installation script for your preferred version, making sure to replace 16.x with your preferred version string (if different):

  1. cd ~
  2. curl -sL https://deb.nodesource.com/setup_16.x-o /tmp/nodesource_setup.sh

See the NodeSource documentation for more information on available versions.

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

/

  1. tmp/nodesource_setup.sh

When you are satisfied that the script is safe to run, exit your editor. Then run the script with

sudo:

  1. sudo bash /tmp/nodesource_setup.sh

The PPA will be added to your configuration and your local packet cache will be updated automatically. You can now

install the Node package.js the same way you did in the previous section:

  1. sudo apt

install nodejs

Verify that you have installed the new version by running

node with the flag -v version: node

  1. -v

Outputv16.19.0

The NodeSource nodejs package contains both the node binary and npm, so you do not need to install npm separately.

At this point, you have successfully installed Node.js and npm using apt and 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 to install Node.js which is particularly flexible is to use nvm, the Node Version Manager. This software allows you to install and maintain many different standalone versions of Node.js and its 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 displayed on the home page. This will give you the latest version of the installation script.

Before funneling the command to bash, it’s always a good idea to audit the script to make sure you’re not doing anything you disagree with. You can do this by deleting the | Bash segment at the end of the

curl command:

  1. curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.3/install.sh

Review the script and make sure you are comfortable with the changes you are 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 from now on, the script can be downloaded and run with the following:

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

This will install the nvm script in your user account. To use it, you must first get your

.bashrc file:

  1. source ~/.bashrc

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

  1. nvm list-remote

Output… v18.0.0 v18.1.0 v18.2.0 v18.3.0 v18.4.0 v18.5.0 v18.6.0 v18.7.0 v18.8.0 v18.9.0 v18.9.1 v18.10.0 v18.11.0 v18.12.0 (LTS: hydrogen) v18.12.1 (LTS: hydrogen) v18.13.0 (latest LTS: hydrogen) v19.0.0 v19.0.1 v19.1.0 v19.2.0 v19.3.0 v19.4.0

It’s a very long list. You can install a version of Node by typing in any of the released versions listed. For example, to get version

v14.10.0, you can run: nvm install v14.10.0

You can see the different versions you have installed by listing them:

nvm list Output-> v14.10.0 v14.21.2 default -> v14.10.0 iojs -> N/A (default) unstable -> N/A (default) node -> stable (-> v14.21.2) (default) stable -> 14.21 (->

  1. v14.21.2

) (default) . . .

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

In addition, there are aliases for the various Long-Term Support (or LTS) versions of Node:

Outputlts/* -> lts/hydrogen (-> 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.24.1 (-> N/A) lts/erbium -> v12.22.12 (-> N/A) lts/fermium -> v14.21.2 lts/gallium -> v16.19.0 (-> N/A) lts/hydrogen -> v18.13.0 (-> N/A)

You can also install a version based on these aliases. For example, to install the latest long-term support release, hydrogen, run the following:

  1. nvm install lts/hydrogen

OutputDownloading and installing node v18.13.0… . . Now using node v18.13.0 (npm v8.19.3) You can

switch between installed versions with

the use of nvm: nvm use v14.10.0 OutputNow using node v14.10.0 (npm v6.14.8) ”’ You can verify that the installation was successful using the same technique as the other sections: ”’command node -v

  1. Outputv14.10.0

The correct version of Node is installed on your machine as expected. A supported version of npm is also available.

Deletion of

Node.js

You can uninstall Node.js using apt or nvm, depending on how it was installed. To remove the version from the system repositories, use apt remove:

  1. sudo apt remove nodejs

By default, apt remove retains the local configuration files that were created since the installation. If you do not want to save the configuration files for later use, use apt purge

:

  1. sudo apt purge nodejs

To uninstall a version of Node.js that you installed with nvm, first determine if it

is the current active version: current nvm If the version you

are targeting is not the current active version, you can run:

  1. nvm uninstall

node_version OutputUninstalled node node_version

This command will uninstall the selected version of Node.js

. If the version

you want to remove is the current active version, you must first disable nvm to enable the changes:

  1. nvm deactivate

You can now uninstall the current version using the uninstall command used above. This deletes all files associated with the target version of Node.js.

Conclusion

There are 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 the Ubuntu repository is one method, using nvm or a NodeSource PPA offers additional flexibility.

For more information on programming with Node.js, check out our tutorial series How To Code in Node.js.

Contact US