Home Ubuntu How to Install Node.js in Ubuntu 20.04/22.04

How to Install Node.js in Ubuntu 20.04/22.04

Node.JS is an open-source, backend Javascript runtime environment built on Google’s V8 engine. It’s an asynchronous event-driven Javascript runtime environment tailored to build fast and scalable network applications and backend API services.

There are three ways that you can leverage to install Node.JS on Ubuntu and we will explore each of them.

Method 1: Install Node.JS and NPM from Ubuntu Repository

To install Node.JS and NPM from the Ubuntu repository, start off by updating the package lists:

$ sudo apt update

Then use the apt package manager to install Node.JS and NPM from the Ubuntu repository as follows.

$ sudo apt install nodejs npm -y
Install NodeJS from Ubuntu Repository
Install NodeJS from Ubuntu Repository

At the time of publishing this guide, the latest version of Node.JS is v10.19.0 from the default Ubuntu repository. To confirm this, run the command:

$ nodejs -v
OR
$ nodejs --version
Check NodeJS Version
Check NodeJS Version

Method 2: Install Node.JS and NPM from Nodesource Repository

The alternative way of installing Node.JS and NPM is to install from NodeSource, which is an organization that is geared towards helping companies run mission-critical Node.JS applications with an emphasis on performance and resource utilization.

To start off, download and run the Nodesource installation script as shown.

$ curl -sL https://deb.nodesource.com/setup_14.x | sudo -E bash -

The script adds a Nodesource signing key, creates a repository of Nodesource in the sources list file, and updates the apt cache.

Download Nodesource Repository
Download Nodesource Repository

The output will also provide you will additional instructions on how to proceed.

Nodesource Installation Script
Nodesource Installation Script

For example, to install NodeJS, run the command:

$ sudo apt install nodejs -y

The NodeJS package provides both nodejs and npm binaries.

Install NodeJS from Nodesource
Install NodeJS from Nodesource

To confirm that Node.JS was installed, run the command:

$ node --version

For npm, run the command:

$ npm --version
Confirm NodeJS Version
Confirm NodeJS Version

Method 2: Install Node.JS and NPM Using NVM

Node Version Manager, abbreviated as NVM, is a shell script that helps in the management of multiple Node.JS versions on a per-user basis. Using NVM, you can install, uninstall and set a specific Node.JS version as the default version.

To download the NVM script, run the command:

$ curl https://raw.githubusercontent.com/creationix/nvm/master/install.sh | bash 

Towards the end, the output will inform you that you need to reopen your terminal to start using nvm. So, close your terminal and launch it once more to begin running nvm commands.

Download NVM Script
Download NVM Script

You can verify the version of nvm installed by executing the command:

$ nvm --version
Check NVM Version
Check NVM Version

To retrieve a list of all the Node.JS versions that can be installed, run the command:

$ nvm list-remote
List NodeJS Versions
List NodeJS Versions

The output provides all the possible versions that can be installed including the LTS versions on Node.JS. At the time of publishing this guide, the latest LTS version is v16.14.2.

NodeJS LTS Versions
NodeJS LTS Versions

To install the latest LTS version of Node.JS, execute:

$ nvm install --lts

Just to confirm the latest LTS version has been installed run the command:

$ node --version
Install NodeJS Using NVM
Install NodeJS Using NVM

To install a specific version of Node.JS, for example, 17.7.2, run the command:

$ nvm install 17.7.2
Install NodeJS Specific Version
Install NodeJS Specific Version

To list all the versions of Node.JS installed on your system, run the command:

$ nvm ls
List NodeJS Installed Versions
List NodeJS Installed Versions

To change to another version of Node.JS, for example, 16.14.2, run the command:

$ nvm use 16.14.2
Change NodeJS Version
Change NodeJS Version

To set the default version of Node.JS, run:

$ nvm alias 16.14.2

And there you have it. We have explored three ways that you can use to install Node.JS and npm on your system. Installing from the Ubuntu repository does not give you the latest version of Node.JS.

To get the latest version, installing from Nodesource and NVM is recommended. The NVM installation method also gives you room for flexibility in terms of installing your preferred version of Node.JS and choosing which version to set as default for your environment.

Ravi Saive
I am an Experienced GNU/Linux expert and a full-stack software developer with over a decade in the field of Linux and Open Source technologies. Founder of TecMint.com, LinuxShellTips.com, and Fossmint.com. Over 150+ million people visited my websites.

Each tutorial at UbuntuMint is created by a team of experienced writers so that it meets our high-quality standards.

Was this article helpful? Please add a comment to show your appreciation and support.

Got something to say? Join the discussion.

Thanks for choosing to leave a comment. Please keep in mind that all comments are moderated according to our comment policy, and your email address will NOT be published or shared. Please Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.