Introduction
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 familiar with from browser-based web development.
In this guide, we will show you three different ways to install Node.js on Ubuntu 20.04 server:
- Using apt to install the nodejs package from Ubuntu's default software repository
- Using 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 be sufficient. If you need newer or older versions of Node, you should use a PPA repository. If you are actively developing Node applications and need to switch between Node versions, choose the nvm method.
Note
This article will guide you through installing Node.js on an Ubuntu server. If you want a one-click way to deploy a Node application to a live server, take a look at the DigitalOcean application platform.
Prerequisites
To follow this guide, you will need to set up an Ubuntu 20.04 server. Before you begin, you will need to set up a non-root user account with sudo privileges on your system. You can learn how to do this by following our tutorial on setting up an initial Ubuntu 20.04 server.
Option 1 – Install Node.js with Apt from the default repositories
Ubuntu 20.04 includes 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 available in the repositories is 10.19. This won't be the latest version, but it should be stable and sufficient for quick language testing.
You can use the apt package manager to get this version. First, refresh your local package list:
sudo apt updateThen install Node.js:
sudo apt install nodejsVerify that the installation was successful by querying for its version number:
node -vOutput
v10.19.0If the package in the repositories meets your needs, that's all you need to do to get Node.js up and running. 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:
sudo apt install npmThis 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 the default Ubuntu software repositories. The next section shows how to use an alternative repository to install different versions of Node.js.
Option 2 – Install Node.js with Apt using NodeSource PPA
To install a different version of Node.js, you can use the PPA (Personal Package Archive) maintained by NodeSource. These PPAs contain more versions of Node.js than the official Ubuntu repositories. Node.js v16 and v18 are available as of this writing.
First, install the PPA to access 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):
cd ~
curl -sL https://deb.nodesource.com/setup_16.x -o /tmp/nodesource_setup.shSee the NodeSource documentation for more information about available versions.
Check the contents of the downloaded script with nano or your favorite text editor:
nano /tmp/nodesource_setup.shWhen you are sure the script is ready to run, exit your editor. Then run the script with sudo:
sudo bash /tmp/nodesource_setup.shThe PPA will be added to your configuration and your local package cache will be automatically updated. You can now install the Node.js package in the same way you did in the previous section:
sudo apt install nodejsVerify that you have the new version installed by running Node with the -v version flag:
node -vOutput
v16.19.0The NodeSource nodejs package includes 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 shows how to use Node Version Manager to install and manage multiple versions of Node.js.
Option 3 – Install Node using Node Version Manager
Another way to install Node.js that is 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 simultaneously.
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 passing the command to bash, it's always a good idea to check the script to make sure it doesn't do anything you don't agree with. You can do this by removing the | bash part at the end of the curl command:
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.3/install.shReview the script and make sure you are comfortable with the changes it makes. When you are satisfied, run the command again with | bash added at the end. The URL you use will change depending on the latest version of nvm, but as of now, the script can be downloaded and run with:
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.3/install.sh | bashThis will install the nvm script into your user account. To use it, you must first source your bashrc file:
source ~/.bashrcNow you can ask NVM which Node version is available:
nvm list-remoteOutput
. . .
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.0This is a very long list. You can install a version of Node by typing in any of the released versions in the list. For example, to get v14.10.0, you could run:
nvm install v14.10.0You 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 (-> v14.21.2) (default)
. . .This shows the currently active version on the first line (->v14.10.0) followed by some aliases and the versions that those aliases refer to.
Additionally, there are aliases for different long-term support (or LTS) versions 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 also install a version based on these aliases. For example, to install the latest long-term support version, Hydrogen, run:
nvm install lts/hydrogenOutput
Downloading and installing node v18.13.0...
. . .
Now using node v18.13.0 (npm v8.19.3)You can switch between installed versions using nvm:
nvm use v14.10.0Output
Now using node v14.10.0 (npm v6.14.8)
```
You can verify that the install was successful using the same technique from the other sections:
```command
node -vOutput
v14.10.0The correct version of Node is installed on your machine as expected. A compatible version of npm is also available.
Uninstall Node.js
You can uninstall Node.js using apt or nvm remove, depending on how you installed it. To remove a version from the system repositories, use apt remove:
sudo apt remove nodejsBy default, apt remove preserves local configuration files created since installation. If you don't want to save the configuration files for later use, use apt purge:
sudo apt purge nodejsTo uninstall a version of Node.js that you installed using nvm, first determine whether it is the current active version:
nvm currentIf the version you want is not the current active version, you can run:
nvm uninstall node_versionOutput
Uninstalled node node_versionThis command will remove the selected Node.js version.
If the version you want to remove is the current active version, you must first disable nvm to enable your changes:
nvm deactivateYou can now uninstall the current version using the uninstall command used earlier. This will remove all files associated with the targeted Node.js version.
Result
There are many ways to get Node.js up and running on Ubuntu 20.04 Server. Your circumstances will determine which of the above methods is best for your needs. While using the version packaged in the Ubuntu repository is one way, using nvm or the NodeSource PPA offers more flexibility.









