How To Install Node.js & NPM on Ubuntu 18.04 and 20.04

March 18, 2019

Introduction

Node.js is an open-source cross-platform JavaScript (JS) runtime environment. It is used for building fast and scalable network applications. In addition to offering various JS modules, it is also lightweight, efficient and supports consistent and integrated development.

Node Package Manager (NPM) is Node’s official package manager, used for installing and managing package dependencies.

In this tutorial, learn three (3) different options for installing Node.js and NPM on Ubuntu 18.04 or Ubuntu 20.04. 

How to install Node.js on Ubuntu.

Note: The process of updating Node.js is similar to installing, but with a few minor differences. To find out more, read How to Update Node.js on Linux.

Prerequisites

3 Ways to Install Node.js and NPM on Ubuntu

Below you will find instructions on how to install Node.js and NPM on Ubuntu using the Ubuntu repository, NVM, and NodeSource repository.

Option 1: Install Node.js and NPM from Ubuntu Repository

The easiest way to install Node.js and NPM is from the Ubuntu repository.

First, update the cache repository to ensure you install the latest versions of Node.js and NPM.

1. Type the command:

sudo apt update

2. Then, install Node.js with the command:

sudo apt install nodejs

3. Confirm that the installation was successful by checking the available version:

nodejs -v

4. Install the Node.js package manager (npm):

sudo apt install npm 

5. Verify the installed version:

npm -v

Option 2: Install Node.js and NPM with NVM

Another way to install Node.js and NPM is with the Node Version Manager (NVM). NVM is a tool practical for managing multiple Node.js versions.

1. To install NVM, download the installation script from GitHub. For that, you use the curl command line.

If you do not have curl, install it by running:

sudo apt install curl

Press y to confirm the installation and hit Enter.

2. Now, download the NVM installation script with the command:

curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.11/install.sh | bash

Note: This command downloads the script for NVM version 0.33.11. Check out the NVM GitHub page to find the latest stable version and replace the section v0.33.11 in the command with the wanted release.

After it automatically clones the NVM repository and adds the NVM path to ZSH or Bash, you receive the following output:

download nvm installation script

3. Then, source the bash file by running:

source ~/.bashrc

4. Check whether the installation was successful by verifying nvm version:

nvm --version

5. Once you have installed nvm, you can find a list of all the available Node.js versions with the command:

nvm ls-remote

This lists all available versions of Node.js, including the latest LTS version, like in the image below:

list available nvm versions

Note: LTS stands for long-term support and is used for software that is maintained for an extended period and is recommended as being the most stable.

6. To install a particular version, use the command nvm install and add the number of the version. For example:

nvm install 10.15.2

To view all installed versions on your manager, use the command:

nvm ls

This lists all installed Node.js versions as well as the default and stable versions.

list installed nodejs versions

To check which version you are currently using, run the command:

node -v 

To switch versions of Node.js (if you have already installed it on the NVM) enter the command:

nvm use 8.11.1

Option 3: Install Node.js from NodeSource Repository

Alternatively, you may want to install Node.js and NPM from the NodeSource repository by adding its PPA (Personal Package Archive) for Ubuntu.

To enable the NodeSource repository, you have to use a curl command.

1. If you do not have curl on your system, run the following command to install it:

sudo apt install curl

Press y to confirm the installation and hit Enter.

2. Now use the following sudo bash script to enable NodeSource:

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

To install another version, replace setup_12.x with the wanted version.

3. Next, install both Node.js and NPM with one command:

sudo apt install nodejs

4. Verify the installation by checking the versions of Node.js and NPM.

Run the commands:

nodejs --version
npm –version
verify nodejs and npm version

If npm is not recognized or not installed properly, the terminal will print npm: command not found.

Note: When installing NodeSource, it is necessary to specify which version of Node.js you will install. In the example above, the Node.js version is 10, but other versions are also available.

Installing Development Tools

Once you have Node.js and NPM setup, install the development tools. This automation tool kit allows compiling and installing native add-ons from the NPM.

To install development tools run the command:

sudo apt install build-essential

Remove Or Uninstall Node.js on Ubuntu

To remove a specific version of Node.js, run the nvm uninstall command appended by the version number.

For example, to uninstall version 8.11.1 run the command:

nvm uninstall 8.11.1

To uninstall Node.js, but keep its configuration files, type:

sudo apt remove nodejs 

To uninstall Node.js and remove any of its configuration files, use the command:

sudo apt purge nodejs

Note: Learn how to build a Node.js app with Docker.

Conclusion

In this tutorial, you learned how to install Node.js and NPM on Ubuntu. We also covered how to remove or uninstall Node.js, and how to install development tools.

Was this article helpful?
YesNo
Sofija Simic
Sofija Simic is an experienced Technical Writer. Alongside her educational background in teaching and writing, she has had a lifelong passion for information technology. She is committed to unscrambling confusing IT concepts and streamlining intricate software installations.
Next you should read
How to Install Node.js and NPM on CentOS 7
June 29, 2019

Node Package Manager (npm) is Node's official package manager, used for installing and managing package...
Read more
How to Install Pip on Ubuntu 18.04
August 20, 2024

Pip is a software utility that downloads packages from PyPI: the Python Package Index. Using PIP is like...
Read more
How to use apt Package Manager on Ubuntu Linux
March 18, 2019

In Linux, special tools were developed for managing applications. Application software for Linux typically...
Read more
How to Install Docker On Ubuntu 18.04
October 22, 2018

Docker is an increasingly popular software package that creates a container for application development.
Read more