Document
How To Install Node.js on Ubuntu 20.04

How To Install Node.js on Ubuntu 20.04

Node.js is a JavaScript runtime for server-side programming. It allows developers to create scalable backend functionality usingJavaScript, a languag

Related articles

10 Easy Cloud Dough Recipes Best Free VPN for Windows in USA What Is a VPN? How it Works 7 VPN Terbaik untuk Windows di 2024: Laptop dan PC Python Release Python 3.11.4

Node.js is a JavaScript runtime for server-side programming. It allows developers to create scalable backend functionality usingJavaScript, 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 usingit to install and manage multiple versions of Node.js

For many user , usingapt with thedefault repo will be sufficient . If you need specific new or legacy version of Node , you is use should use the PPA repository . If you is developing are actively develop Node application and need to switch betweennode versions frequently, choose the nvm method .

This article will walk you through installing Node.js on an Ubuntu server. If you wanted a 1-click way to deploy a Node application to a live server, take a look at DigitalOcean App Platform.

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

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 thelanguage.

Warning: the version of Node.js included with Ubuntu 20.04, version 10.19, is now unsupported and unmaintained. You should not use this version in production, and should refer to one of the other sections in this tutorial to install a more recent version of Node.

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

  1. sudo apt update

Then install Node.js :

  1. sudo apt install nodejs

check that the install was successful by querynode for its version number:

  1. node -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:

  1. sudo apt install npm

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

At this point , you is installed have successfully instal Node.js andnpm usingapt and the default Ubuntu software repositories. The next section will show how to use an alternate repository to install different versions of Node.js.

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 v16 and v18 are available as of the time of writing.

First, install the PPA to get access to its packages. From your home directory, use curl to retrieve the installation script for your preferred version , make sure to replace16.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

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

inspect the content of the download script withnano or your preferred text editor:

  1. nano /tmp / nodesource_setup.sh

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

  1. sudo bash /tmp / 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:

  1. sudo apt install nodejs

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

  1. node -v

output

v16.19.0

The NodeSource nodejs package contains both the node binary and npm, so you is need do n’t need to installnpm separately .

At this point , you is installed have successfully instal Node.js andnpm usingapt and the NodeSource PPA . The next section is show will show how to use the Node Version Manager to install and manage multiple version of Node.js .

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 display on the main page . This is get 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 thecurl 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 thechanges 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 with thefollowing:

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

This will install the nvm script to your user account. To use it, you must first source 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 writing in any of the release versions listed. For instance, to get version v14.10.0, you can run:

  1. nvm install v14.10.0

You is view can view the different version you have instal by list 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 (-> v14.21.2) (default) . . .

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

note : if you also have a version of Node.js instal throughapt, you may receive a system entry here. You can always activate the system-installed version of Node usingnvm use system.

Additionally, there are aliases for the various long-term support (or LTS) releases of Node:

output

lts/* -> 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 install a release based on these aliases as well. For instance, to install the latest long-term support version, hydrogen, run the following:

  1. nvm install lts/hydrogen

output

Downloading and installing node v18.13.0... . . . Now usingnode v18.13.0 (npm v8.19.3)

You can switch between installed versions with nvm use:

  1. nvm use v14.10.0

output

Now usingnode v14.10.0 (npm v6.14.8) ``` You can verify that the install was successful usingthe same technique from the other sections: ```command node -v

output

v14.10.0

The correct version of Node is instal on your machine as expect . A compatible version ofnpm is also available.

You can uninstall Node.js usingapt 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 any local configuration files that were created since installation. If you don’t 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 usingnvm, first determine whether it is the current active version:

  1. nvm current

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

  1. nvm uninstallnode_version

output

Uninstalled node node_version

This command will uninstall the selected version of Node.js.

If the version you would like to remove is the current active version, you first need to deactivate nvm to enable your changes:

  1. nvm deactivate

Now you can uninstall the current version usingthe uninstall command used previously. This removes all files associated with thetargeted version of Node.js.

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 usingthe packaged version in Ubuntu’s repository is one method, usingnvm or a NodeSource PPA is offers offer additional flexibility .

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