Node.js is an open-source cross-platform JavaScript run-time  environment that allows server-side execution of JavaScript code. This  means that you can run JavaScript code on your machine as a standalone  application, free of any web browser. Node.js is mainly used to build  back-end server-side applications, but it is also very popular as a  full-stack and front-end solution.

Npm is the default package manager for Node.js and the world’s largest software registry.

In  this tutorial, we will show you several different ways of installing  Node.js and npm on Ubuntu 18.04. The same instructions apply for any  Ubuntu-based distribution, including Kubuntu, Linux Mint and Elementary  OS.

If  you need Node.js only as a local runtime for deploying Node.js  applications then the simplest option is to install Node.js from the  NodeSource repository. Developers should prefer installing Node.js using  the NVM script.

Choose the installation option that is  appropriate for your environment. It is best to consult the  documentation of the Node.js application that you use to find out which  Node.js versions are supported.

Installing Node.js and npm from NodeSource

NodeSource  is a company focused on providing enterprise-grade Node support and  they maintain a repository containing the latest versions of Node.js.

Use  this repository if you need to install a specific version of Node.js.  At the time of writing, NodeSource repository provides the following  versions - v12.x, v11.x, v10.x, and v8.x. We’ll install the current LTS  version of Node.js, version 10.x, Carbon.

To install Node.js and npm from the NodeSource repository, follow these steps:

Enable the NodeSource repository by running the following curl as a user with sudo privileges:

curl -sL | sudo -E bash -

The  command will add the NodeSource signing key to your system, create an  apt sources repository file, install all necessary packages and refresh  the apt cache.

If you need to install another version, for example 12.x, just change setup_10.x with setup_12.x

Once the NodeSource repository is enabled, install Node.js and npm by typing:

sudo apt install nodejs

The nodejs package contains both the node and npm binaries.

Verify that the Node.js and npm were successfully installed by printing their versions:

node --version
npm --version

Installing Node.js and npm using NVM

NVM  (Node Version Manager) is a bash script used to manage multiple active  Node.js versions. With NVM you can install and uninstall any specific  Node.js version you want to use or test.

To install Node.js and npm using NVM on your Ubuntu system, perform the following steps:

1. Installing NVM (Node Version Manager) script

To download and install the nvm script run:

curl -o- | bash

The command above will clone the NVM repository from Github to the ~/.nvm directory:

=> Close and reopen your terminal to start using nvm or run the following to use it now:

export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/" ] && \. "$NVM_DIR/"  # This loads nvm
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"  # This loads nvm bash_completion

As the output above says, you should either close and reopen the terminal or run the commands to add the path to nvm script to the current shell session. You can do whatever is easier for you.

Once the script is in your PATH, verify that nvm was properly installed by typing:

nvm --version

2. Installing Node.js and npm

Now that the nvm is installed you can install the latest available version of Node.js, by typing:

nvm install node

The output should look something like this:

Downloading and installing node v12.8.1...
######################################################################### 100.0%
Computing checksum with sha256sum
Checksums matched!
Now using node v12.8.1 (npm v6.10.2)
Creating default alias: default -> node (-> v12.8.1)

Once the installation is completed, verify it by printing the Node.js version:

node --version

Let’s install two more versions, the latest LTS version and version 8.10.0

nvm install --lts

To list installed Node.js versions type:

nvm ls

The output should look something like this:

->      v8.10.0
default -> node (-> v12.8.1)
node -> stable (-> v12.8.1) (default)
stable -> 12.8 (-> v12.8.1) (default)
iojs -> N/A (default)
unstable -> N/A (default)
lts/* -> lts/dubnium (-> v10.16.3)
lts/argon -> v4.9.1 (-> N/A)
lts/boron -> v6.17.1 (-> N/A)
lts/carbon -> v8.16.1 (-> N/A)
lts/dubnium -> v10.16.3

The entry with an arrow on the right (-> v8.10.0) is  the Node.js version used in the current shell session and the default  version is set to v12.8.1. Default version is the version that will be  active when opening new shells.

You can change the currently active version with:

nvm use 10.16.3
Now using node v10.16.3 (npm v6.9.0)

If you want to change the default Node.js version use the following command:

nvm alias default 10.16.3

Install Node.js and npm from the Ubuntu repository

Node.js and npm packages are available from the default Ubuntu 18.04 repositories.

At the time of writing, the version included in the Ubuntu repositories is v8.10.0 which is the previous TLS version.

To install nodejs and npm run the following commands:

sudo apt update

The Node.js executable from the Ubuntu repositories is named nodejs instead of node because of a conflict with another package.

Verify the installation by executing:

nodejs --version

Install development tools

To be able to compile and install native add-ons from npm you need to install the development tools.

The following command will install all the necessary packages including the GCC compilers:

sudo apt install build-essential

Uninstall Node.js

If for some reasons you want to uninstall Node.js and npm packages, you can use the following command:

sudo apt remove nodejs npm


We  have shown you three different ways to install Node.js and npm on your  Ubuntu 18.04 server. The method you choose depends on your requirements  and preferences. Even though installing the packaged version from the  Ubuntu or NodeSource repository is easier, the nvm method gives you more  flexibility for adding and removing different Node.js versions on a  per-user basis.

If you want to manage your npm packages with yarn, you can check this tutorial about how to install and use yarn on Ubuntu 18.04.

If you have any questions or feedback, feel free to comment below.