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. 

how to install node.js on ubuntu

Note: Updating Node.js is a similar process to installing it, but there are minor differences. To find out more, read How to Update Node.js on Linux.


Prerequisites

3 Ways to Install NPM (Node Package Manager) on Ubuntu

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 will 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

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

download nvm installation script

3. To enable nvm:

  • Close and open the terminal or
  • Run the given command

4. Finally, check whether the installation was successful by verifying nvm version:

nvm --version

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

nvm ls-remote

This will list all available versions of nvm, 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.


Install a Specific Version

Nvm is a package manager; it can install and manage multiple Node.js versions.

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

For example for: nvm install 10.15.2

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

nvm ls

This will list 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_11.x | sudo bash -

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

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 versions 11, 8, and 6 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 gcc g++ make

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

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.