How To Install Node.js on Ubuntu 20.04
In this guide, we will show you three different ways of getting Node.js installed on an Ubuntu 20.04 server:
aptto install the
nodejspackage from Ubuntu’s default software repository
aptwith an alternate PPA software repository to install specific versions of the
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
Prerequisites for installing NodeJS on Ubuntu
This tutorial will assume the following: errorhat
- Ubuntu 20.04 server (although desktop should still work)
- Non-root user
- General understanding of Command-Line tools
Let’s Get Started
Install Node.js and npm from the Ubuntu repository
At the time of writing, the Node.js version included in the Ubuntu 20.04 repositories is
10.19.0 which is the previous TLS version.
The installation is pretty straightforward. Run the following commands to update the package index and install Node.js and npm:
$ sudo apt update -y ; sudo apt upgrade -y $ sudo apt install nodejs npm
The command above will install a number of packages, including the tools necessary to compile and install native addons from npm.
Once done, verify the installation by running:
$ nodejs --version
Installing using Node Version Manager
NodeSource is a company focused on providing enterprise-grade Node support. It maintains an APT repository containing multiple Node.js versions. Use this repository if your application requires a specific version of Node.js.
At the time of writing, NodeSource repository provides the following versions:
- v14.x – The latest stable version.
- v12.x – The latest LTS version.
- v10.x – The previous LTS version.
We’ll install Node.js version 14.x:
1. Run the following command as a user with sudo privileges to download and execute the NodeSource installation script:
$ curl -sL https://deb.nodesource.com/setup_14.x | sudo -E bash -
The script will add the NodeSource signing key to your system, create an apt repository file, install all necessary packages, and refresh the apt cache.
If you need another Node.js version, for example
12.x, change the
2. Once the NodeSource repository is enabled, install Node.js and npm:
$ sudo apt install nodejs
The nodejs package contains both the
3. Verify that the Node.js and npm were successfully installed by printing their versions:
$ nodejs --version
$ npm --version
To be able to compile native addons from npm you’ll need to install the development tools:
$ sudo apt install build-essential
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.
We hope the “How to Install Node.js” and “npm” on “Ubuntu 20.04” help you. If you have any query regarding “How to Install Node.js” and “npm” on “Ubuntu 20.04” drop a comment below and we will get back to you at the earliest.