Cybersecurity Updates & Tools

Install Node.js and npm on Ubuntu 18.04: Three Methods Compared

Node.js is an open-source, cross-platform JavaScript runtime that lets you run JavaScript on the server side, outside of a browser. It is widely used for building backend services, REST APIs, and real-time applications. npm is Node’s built-in package manager and the world’s largest software registry.

There are three ways to install Node.js on Ubuntu 18.04. If you are deploying a Node.js application and need a stable, specific version, use the NodeSource repository. If you are a developer who needs to switch between multiple Node.js versions across different projects, NVM is the better choice. If you just need Node.js quickly and the version does not matter, the Ubuntu default repository is the simplest option.

<strong>Prerequisite:</strong>&nbsp;You need sudo access.

Method 1: Install Node.js on Ubuntu from NodeSource

NodeSource maintains up-to-date packages for specific Node.js releases. At the time of writing, it provides v10.x, v12.x, v13.x, and v14.x. This guide installs the current LTS version — Node.js 12.

Run the setup script to add the NodeSource repository to your system:

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

This script adds the NodeSource signing key, creates the apt source file, and refreshes the package cache. To install a different version, replace setup_12.x with setup_14.x or whichever version you need.

Install Node.js and npm:

bashsudo apt install nodejs

The nodejs package includes both the node binary and npm. Verify both are installed:

bashnode --versionnpm --version

Method 2: Install Node.js with NVM

NVM (Node Version Manager) lets you install and switch between multiple Node.js versions on the same machine. This is the preferred method for developers who work across projects with different runtime requirements.

Install NVM by running the official install script:

bashcurl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.35.3/install.sh | bash

Close and reopen your terminal to load NVM into your shell session, then verify it is working:

bashnvm --version

Install the latest Node.js version:

bashnvm install node

To install the current LTS release:

bashnvm install --lts

To install a specific version:

bashnvm install 10.16.3

List all installed versions:

bashnvm ls

Switch to a different version for the current session:

bashnvm use 10.16.3

To set a version as the permanent default for all new terminal sessions:

bashnvm alias default 10.16.3

Method 3: Install from the Ubuntu Repository

Ubuntu 18.04’s default repositories include Node.js, but the available version is v8.10.0 — an older release. Use this method only if you need Node.js quickly and do not require a specific version.

bashsudo apt updatesudo apt install nodejs npm

Note that the binary is called nodejs rather than node in this package, due to a naming conflict with another package.

Verify the installation:

bashnodejs --version

Install Development Tools

To compile and install native npm addons, you need the GCC compilers and build utilities:

bashsudo apt install build-essential

Uninstall Node.js

To remove Node.js and npm installed via apt:

bashsudo apt remove nodejs npm

If you installed Node.js through NVM, remove a specific version with nvm uninstall [version].

Node.js and npm are now installed on your Ubuntu 18.04 machine. NodeSource and NVM give you the most control over which version runs on your system. NVM is the most flexible option if you are actively developing across multiple projects with different requirements. Leave a comment below if you run into any issues during installation.