For the complete documentation index, see llms.txt. This page is also available as Markdown.

How to install Node.js on Ubuntu

Node.js lets you run JavaScript on the server side. It is the foundation for countless modern tools and applications. Whether you are running a web app, a Discord bot, or an API server, you will probably need Node.js at some point.

This guide covers two ways to install it. Pick the one that fits your needs.

Method 1: Using the Ubuntu repository (easiest)

This is the quickest way, but the version might be a bit old:

sudo apt update
sudo apt install nodejs npm -y

Check the version:

node --version
npm --version

If the version looks acceptable for what you need, you are done. Move on to building your project.

NodeSource maintains up-to-date repositories with more current Node.js versions. This is the better choice if you need a recent release.

First, install the NodeSource setup script. Replace 22.x with the version you want (20.x, 22.x, or 23.x):

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

The script adds the NodeSource repository and updates your package list automatically. Then install:

sudo apt install nodejs -y

That is it. Node.js and npm are both included in this package:

Method 3: Using NVM (best for developers who switch versions)

NVM (Node Version Manager) lets you install and switch between multiple Node.js versions on the same machine. This is the way to go if you work on several projects that require different Node versions.

Install NVM:

Reload your shell:

Now you can install any Node.js version:

Check the active version:

Verifying npm works

Create a quick test to make sure everything is functional:

If you see a cow saying something, installation is complete.

Installing Yarn (optional)

Some projects use Yarn instead of npm. Install it globally:

Setting up a simple project

Here is a minimal web server to confirm Node.js can serve traffic:

Create index.js:

Run it:

Visit http://your-server-ip:3000 in your browser. You should see the message.

Managing Node.js processes

For production, you will want something that keeps your app running even if it crashes. PM2 is the most popular choice:

This keeps your application alive through crashes and restarts.

Common issues

"node: command not found" after NVM install. Log out and back in, or run source ~/.bashrc.

Permission errors with global npm installs. If you installed Node.js from Ubuntu repos, use npm install -g without sudo. If you get EACCES errors, switch to NVM instead.

Port 3000 is not accessible. Make sure UFW allows the port:

Summary

Method
Best for

Ubuntu repos

Quick setup, don't need latest version

NodeSource

Stable recent version, one install

NVM

Switching versions, multiple projects

Last updated