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
aptrepository. 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
sudoprivileges.
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 nodejsCheck the version:
node -vOutput
v12.22.9That is quite old. This package also does not include npm, so install it separately:
sudo apt install npmIf 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 npmInstall curl if it is missing:
sudo apt install curlDownload 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 nodejsThe NodeSource nodejs package includes npm. Check both:
node -v
npm -vOutput
v20.11.1
10.2.4Some npm packages compile native code during installation. To avoid errors, install the build toolchain:
sudo apt install build-essentialOption 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 | bashThe script clones NVM into ~/.nvm and adds initialisation lines to ~/.bashrc. To use NVM in the current session, reload .bashrc:
source ~/.bashrcVerify NVM works:
nvm --versionOutput
0.39.7List the Node.js versions available to install (the full list is long, so filter to LTS releases):
nvm ls-remote --ltsInstall the latest LTS version:
nvm install --ltsOr install specific versions:
nvm install 18
nvm install 20.11.1See the installed versions and which one is active:
nvm lsOutput
-> v20.11.1
v18.19.1
default -> lts/* (-> v20.11.1)Switch versions in the current session:
nvm use 18Set the default for new terminals:
nvm alias default 20Tip: 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 useTesting With a Small App
Whichever option you chose, create a hello.js file to confirm Node.js runs correctly:
nano hello.jsconst 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.jsOutput
Server is running at http://localhost:3000Open a second terminal and check:
curl http://localhost:3000Output
Hello from Node.js on UbuntuPress CTRL + C to stop the app.
Uninstalling Node.js
If you installed from apt (Option 1 or 2):
sudo apt remove nodejs
sudo apt autoremoveIf you used NVM, uninstall a specific version (you cannot remove the active one, so nvm use another version first):
nvm uninstall 18To remove NVM entirely, delete ~/.nvm and the NVM lines in ~/.bashrc:
rm -rf ~/.nvmConclusion
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.

