hieuvnlabs logo
Hiusvu

How To Install Node.js on Ubuntu

Published on September 3, 2026

How To Install Node.js on Ubuntu

Introduction

Node.js is a JavaScript runtime for the server, used to build backends, command-line tools and to bundle frontend frameworks such as Next.js, React and Vue. Almost every modern web project needs Node.js at some stage.

In this guide we will look at three ways to install Node.js on Ubuntu, each suited to a different situation:

  • Option 1: install from Ubuntu's default apt repository. Fastest, but usually an old version.
  • Option 2: install from the NodeSource repository. Lets you choose a specific LTS line and stay current.
  • Option 3: install with NVM (Node Version Manager). Ideal for a development machine that needs to switch between versions.

Prerequisites

  • A server or workstation running Ubuntu 20.04 or 22.04.
  • A regular user with sudo privileges.

Option 1 — Installing Node.js from the Default apt Repository

Ubuntu 22.04 includes Node.js in its default repository. This is the simplest route if you just need a Node.js environment to experiment with.

Update the package index and install:

sudo apt update
sudo apt install nodejs

Check the version:

node -v
Output
v12.22.9

That is quite old. This package also does not include npm, so install it separately:

sudo apt install npm

If your project needs a newer Node.js (most frameworks now require at least Node 18), move on to Option 2 or 3.

Option 2 — Installing Node.js from the NodeSource Repository

NodeSource maintains a repository with recent Node.js builds packaged for Ubuntu. This is the best fit for a production server where you want a fixed LTS version that gets updated through apt like any other package.

If you installed Node.js from the default repository in Option 1, remove it first:

sudo apt remove nodejs npm

Install curl if it is missing:

sudo apt install curl

Download and run the NodeSource setup script. This example picks Node.js 20 (LTS). Replace 20 with 18 or 22 as needed:

curl -fsSL https://deb.nodesource.com/setup_20.x | sudo -E bash -

The script adds the GPG key and the NodeSource repository to apt. Then install Node.js:

sudo apt install nodejs

The NodeSource nodejs package includes npm. Check both:

node -v
npm -v
Output
v20.11.1
10.2.4

Some npm packages compile native code during installation. To avoid errors, install the build toolchain:

sudo apt install build-essential

Option 3 — Installing Node.js with NVM

NVM (Node Version Manager) is a bash script that lets you install and switch between multiple Node.js versions per user, without sudo. It is the ideal choice on a development machine where different projects need different versions.

Download and run the NVM install script from GitHub. Check the nvm-sh GitHub page for the latest release before running:

curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash

The script clones NVM into ~/.nvm and adds initialisation lines to ~/.bashrc. To use NVM in the current session, reload .bashrc:

source ~/.bashrc

Verify NVM works:

nvm --version
Output
0.39.7

List the Node.js versions available to install (the full list is long, so filter to LTS releases):

nvm ls-remote --lts

Install the latest LTS version:

nvm install --lts

Or install specific versions:

nvm install 18
nvm install 20.11.1

See the installed versions and which one is active:

nvm ls
Output
->     v20.11.1
       v18.19.1
default -> lts/* (-> v20.11.1)

Switch versions in the current session:

nvm use 18

Set the default for new terminals:

nvm alias default 20

Tip: Create a .nvmrc file in your project directory containing the Node.js version it needs (for example 20). Then simply run nvm use in that directory and NVM reads the file and switches automatically.

echo "20" > .nvmrc
nvm use

Testing With a Small App

Whichever option you chose, create a hello.js file to confirm Node.js runs correctly:

nano hello.js
const http = require("http");

const server = http.createServer((req, res) => {
  res.writeHead(200, { "Content-Type": "text/plain" });
  res.end("Hello from Node.js on Ubuntu\n");
});

server.listen(3000, () => {
  console.log("Server is running at http://localhost:3000");
});

Run it:

node hello.js
Output
Server is running at http://localhost:3000

Open a second terminal and check:

curl http://localhost:3000
Output
Hello from Node.js on Ubuntu

Press CTRL + C to stop the app.

Uninstalling Node.js

If you installed from apt (Option 1 or 2):

sudo apt remove nodejs
sudo apt autoremove

If you used NVM, uninstall a specific version (you cannot remove the active one, so nvm use another version first):

nvm uninstall 18

To remove NVM entirely, delete ~/.nvm and the NVM lines in ~/.bashrc:

rm -rf ~/.nvm

Conclusion

You now know three ways to install Node.js on Ubuntu and when to use each. For a production server, use NodeSource for a stable LTS release. For a development machine, NVM gives you far more flexibility. In the next guide we use Node.js to deploy a Next.js app on a VPS with PM2 and Nginx: How To Deploy a Next.js App on an Ubuntu VPS with PM2 and Nginx.