Install Node.js 18 and npm on Ubuntu 22.04

Install Node.js 18 and npm on Ubuntu 22.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 18 is a long-term support (LTS) release. This release has various updates. For example, the V8 JavaScript engine has been updated to 10.1. This tutorial demonstrates how to install Node.js 18 and npm on Ubuntu 22.04.

Install Node.js and npm

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

curl -sSL | 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

Install older versions

Node.js 16 is an older LTS version that is still supported. It can be installed by adding the following NodeSource repository:

curl -sSL | sudo bash -

The 1 Comment Found

  1. Avatar
    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

Cancel reply

Your email address will not be published.