

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.
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:
shell
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.
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
shell
To get this version, you can use the apt package manager. Refresh your local package index first:
shell
Then install Node.js:
shell
Check that the install was successful by querying node for its version number: Then install Node.js:
shell
shell
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:
shell
This allows 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 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, making sure to replace 16.x with your preferred version string (if different):
shell
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:
shell
When you are satisfied that the script is safe to run, exit your editor. Then run the script with sudo:
shell
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:
shell
Verify that you’ve installed the new version by running node with the -v version flag:
shell
shell
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:
shell
Review the script 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 with the following:
shell
This will install the nvm script to your user account. To use it, you must first source your .bashrc file:
shell
Now, you can ask NVM which versions of Node are available:
shell
shell
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:
shell
You can view the different versions you have installed by listing them:
shell
shell
This shows the currently active version on the first line (-> v14.10.0), followed by some named aliases and the versions that those aliases point to.
shell
Additionally, there are aliases for the various long-term support (or LTS) releases of Node:
shell
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:
shell
shell
shell
shell
shell
The correct version of Node is installed on your machine as expected. A compatible version of npm is also available.
Removing 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:
shell
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:
shell
To uninstall a version of Node.js that you installed using nvm, first determine whether it is the current active version:
shell
If the version you are targeting is not the current active version, you can run:
shell
shell
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:
shell
Now you can uninstall the current version using the uninstall command used previously. This removes all files associated with the targeted version of Node.js.