How to Install Node.js and npm on Ubuntu 18.04

How to Install Node.js and npm on Ubuntu 18.04

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.

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:

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

  2. 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.

  3. 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
nvm install 8.10.0

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
sudo apt install nodejs

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.

Thank you for reading !

nodejs npm nvm ubuntu

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

How To Install NVM on Ubuntu

It also provides an option to auto-select the node version using the .nvmrc configuration file. This tutorial will help you to install NVM on Ubuntu 20.04 Linux system. Also, allow you to install different node versions and other useful examples.

How to Install Microsoft Teams on Ubuntu 20.04

In this tutorial, we will show you how to install Microsoft Teams on Ubuntu 20.04 machine. we can install teams using Debian installer file or by adding microsoft repository.

How to Install NodeJS on Ubuntu 19.04

In this tutorial, you will learn how to install Node onto Ubuntu 19.04 Disco Dingo. We will cover installation from the default repositories and, for those wanting more recent releases, how to install from the NodeSource repositories.

Hire NodeJs Developer

Looking to build dynamic, extensively featured, and full-fledged web applications? **[Hire NodeJs Developer]( "Hire NodeJs Developer")** to create a real-time, faster, and scalable...

How to Install Node.js and npm on Ubuntu 20.04

In this tutorial, we will explore three different ways of installing Node.js and npm on Ubuntu 20.04. From the standard Ubuntu repositories, from the NodeSource repositories and by using the nvm script.