Install Node.js 16 and npm on Ubuntu 20.04

Node.js is an open-source runtime environment that enables to execute JavaScript code on the server without a browser. It is built on the Chrome’s V8 JavaScript engine. Node.js is often used for developing web applications. NPM is a package manager for Node.js that enables to install various packages from remote registry.

Node.js 16 is a long-term support (LTS) release. This release have various updates. For example, the V8 JavaScript engine has been updated from 8.6 to 9.0. This tutorial demonstrates how to install Node.js 16 and npm on Ubuntu 20.04.

Install Node.js and npm

NodeSource repository maintains Node.js 16 binary distributions. So add this repository in your system by using the following command:

curl -sSL https://deb.nodesource.com/setup_16.x | sudo bash -

Install Node.js:

sudo apt install -y nodejs

During installation, npm has been installed too. So you don’t need to install npm separately, it comes bundled with Node.js. Verify that the installation completed successfully by checking Node.js and npm versions:

node --version
npm --version

Testing Node.js

Create a new main.js file:

nano main.js

Add the following a line of code:

console.log('Hello world');

Test a program with node command:

node main.js

Uninstall Node.js and npm

If you wish to completely remove Node.js and related dependencies, run the following command:

sudo apt purge --autoremove -y nodejs

Remove GPG key and repository:

sudo rm -rf /usr/share/keyrings/nodesource.gpg
sudo rm -rf /etc/apt/sources.list.d/nodesource.list

This article has 1 comment

  1. Jhanvi Mehta Reply

    In your article, you provide a node.js installation, testing and uninstallation process because many articles only provide an installation or uninstallation process but you provide all processes in one post. so, Thank you for sharing this information.

Leave a Comment

Your email address will not be published. Required fields are marked *