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> You need sudo access.
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
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
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
To compile and install native npm addons, you need the GCC compilers and build utilities:
bashsudo apt install build-essential
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.
SSH (Secure Shell) is a cryptographic network protocol that creates a secure, encrypted connection between your…
DNS (Domain Name System) is what translates domain names into IP addresses. When you type a…
Docker is a containerization platform that lets you package applications and their dependencies into lightweight, portable…
Yarn is a fast, reliable JavaScript package manager that works alongside npm. It was built to fix…
Webmin is an open-source, browser-based control panel for Linux server administration. Instead of managing your server…
Go (also called Golang) is a modern, statically typed programming language created at Google. It is…