Debian

Install Node.js 22 LTS on Ubuntu 24.04 / Debian 13

Node.js is a cross-platform JavaScript runtime built on Chrome’s V8 engine that lets you run JavaScript outside the browser. It powers everything from REST APIs and microservices to full-stack web applications and CLI tools. Node.js 22 LTS (codename “Jod”) is the current long-term support release, maintained through April 2027.

This guide covers three methods to install Node.js 22 LTS on Ubuntu 24.04 and Debian 13 – the NodeSource repository (recommended for production), default apt packages, and nvm for managing multiple versions. We also cover npm and yarn package management plus firewall configuration for Node.js applications.

Prerequisites

  • A server or workstation running Ubuntu 24.04 or Debian 13
  • A user account with sudo privileges
  • Internet connectivity to download packages
  • Ports 3000, 8080, or whichever port your Node.js app uses (if serving traffic)

The NodeSource repository provides the latest Node.js 22 LTS builds packaged for Debian-based systems. This is the recommended method for production servers since you get timely security updates through apt.

First, update your package index and install the required dependencies:

sudo apt update
sudo apt install -y curl gnupg

Download and run the NodeSource setup script for Node.js 22:

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

The script adds the NodeSource GPG signing key and configures the apt repository. You should see confirmation that the repository was added successfully:

## Installing the NodeSource Node.js 22.x repo...
## Populating apt-get cache...
## Confirming "noble" is supported...
## Adding the NodeSource signing key to your keyring...
## Creating apt sources list file for the NodeSource Node.js 22.x repo...
## Running `apt-get update` for you...

Now install Node.js 22 LTS:

sudo apt install -y nodejs

Verify the installation by checking the Node.js and npm versions:

node --version

The output confirms Node.js 22 LTS is installed:

v22.22.1

Check the bundled npm version:

npm --version

npm ships bundled with Node.js – you should see version 10.x or later:

10.9.2

Step 2: Install Node.js from Default APT Repositories

Ubuntu 24.04 and Debian 13 include Node.js in their default repositories. The version may be older than the latest Node.js 22 LTS, but this method requires no external repositories.

Install Node.js and npm from the default repos:

sudo apt update
sudo apt install -y nodejs npm

Check the installed version:

nodejs --version

The default repository version is typically older than NodeSource. If you need the latest Node.js 22 LTS features, use Method 1 (NodeSource) or Method 3 (nvm) instead.

Step 3: Install Node.js 22 with nvm (Node Version Manager)

nvm lets you install and switch between multiple Node.js versions on the same machine. This is ideal for development environments where different projects need different Node.js versions. If you need to run multiple versions of Node.js side by side, nvm is the right tool.

Download and run the nvm install script (v0.40.4 at the time of writing):

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

The script clones the nvm repository to ~/.nvm and adds the initialization lines to your shell profile. Reload your shell configuration to make nvm available:

source ~/.bashrc

Verify nvm is working:

nvm --version

You should see the nvm version number confirmed:

0.40.4

Install Node.js 22 LTS using nvm:

nvm install 22

nvm downloads and installs the latest Node.js 22 release along with npm:

Downloading and installing node v22.22.1...
Downloading https://nodejs.org/dist/v22.22.1/node-v22.22.1-linux-x64.tar.xz...
######################################################################### 100.0%
Computing checksum with sha256sum
Checksums matched!
Now using node v22.22.1 (npm v10.9.2)

Set Node.js 22 as the default version for new shell sessions:

nvm alias default 22

Confirm the active version:

node --version
npm --version

Working with npm – Node Package Manager

npm is the default package manager for Node.js. It handles dependency installation, script execution, and package publishing. Here are the essential npm operations you need to know.

Initialize a New Node.js Project

Create a new project directory and initialize it with a package.json file:

mkdir my-app && cd my-app
npm init -y

The -y flag accepts all defaults. This creates a package.json that tracks your project dependencies and scripts:

{
  "name": "my-app",
  "version": "1.0.0",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "description": ""
}

Install Packages Locally

Local packages are installed in the project’s node_modules directory and listed in package.json. This is the standard approach for project dependencies:

npm install express

For development-only dependencies like testing frameworks or linters:

npm install --save-dev jest eslint

Install Packages Globally

Global packages provide CLI tools available system-wide. Use global installs sparingly – only for tools you run from the command line, not project dependencies:

sudo npm install -g pm2 nodemon

List all globally installed packages:

npm list -g --depth=0

The output shows the globally installed packages and their versions:

/usr/lib
├── [email protected]
├── [email protected]
├── [email protected]
└── [email protected]

Other Useful npm Commands

Update all packages in a project to their latest compatible versions:

npm update

Remove a package:

npm uninstall express

Check for outdated packages:

npm outdated

Audit your dependencies for known security vulnerabilities:

npm audit

Install Yarn Package Manager

Yarn is an alternative package manager that some projects prefer for its speed and deterministic installs. Node.js 22 ships with Corepack, which manages Yarn without a separate install step.

Enable Corepack to activate Yarn:

sudo corepack enable

Verify Yarn is available:

yarn --version

The version output confirms Yarn is ready to use:

1.22.22

To use Yarn 4 (Berry) in a project, set the version inside the project directory:

yarn set version stable

Basic Yarn commands mirror npm closely:

yarn init -y
yarn add express
yarn add --dev jest
yarn remove express

Configure Firewall for Node.js Applications

If your Node.js application serves traffic, you need to open the appropriate port in the firewall. Ubuntu 24.04 and Debian 13 use UFW (Uncomplicated Firewall).

For a typical Node.js app running on port 3000:

sudo ufw allow 3000/tcp

If your app uses port 8080:

sudo ufw allow 8080/tcp

Enable UFW if it is not already active:

sudo ufw enable

Verify the firewall rules are in place:

sudo ufw status

The output shows the allowed ports and their status:

Status: active

To                         Action      From
--                         ------      ----
22/tcp                     ALLOW       Anywhere
3000/tcp                   ALLOW       Anywhere
8080/tcp                   ALLOW       Anywhere
22/tcp (v6)                ALLOW       Anywhere (v6)
3000/tcp (v6)              ALLOW       Anywhere (v6)
8080/tcp (v6)              ALLOW       Anywhere (v6)

For production deployments, run your Node.js app behind a reverse proxy like Nginx rather than exposing the Node.js port directly. The reverse proxy handles SSL termination, load balancing, and static file serving. You can pair it with a process manager like PM2 to keep your app running across restarts.

Create a Test Node.js Application

To confirm everything works end to end, create a simple HTTP server. Create a file called server.js:

vi server.js

Add the following code:

const http = require('http');

const hostname = '0.0.0.0';
const port = 3000;

const server = http.createServer((req, res) => {
  res.statusCode = 200;
  res.setHeader('Content-Type', 'text/plain');
  res.end('Node.js 22 LTS is running on Ubuntu/Debian\n');
});

server.listen(port, hostname, () => {
  console.log(`Server running at http://${hostname}:${port}/`);
});

Start the server:

node server.js

The server starts listening on port 3000:

Server running at http://0.0.0.0:3000/

Open a second terminal and test with curl:

curl http://localhost:3000

You should get back the response confirming Node.js is working:

Node.js 22 LTS is running on Ubuntu/Debian

Press Ctrl+C to stop the server. For a proper development workflow, consider setting up Visual Studio Code on Ubuntu as your editor with the Node.js extension pack.

Uninstall Node.js

If you installed Node.js via NodeSource or the default repos and need to remove it:

sudo apt remove --purge nodejs
sudo rm -f /etc/apt/sources.list.d/nodesource.list
sudo rm -f /etc/apt/keyrings/nodesource.gpg
sudo apt autoremove

If you used nvm, uninstall the Node.js version and optionally remove nvm itself:

nvm deactivate
nvm uninstall 22
rm -rf ~/.nvm

Conclusion

Node.js 22 LTS is now installed on your Ubuntu 24.04 or Debian 13 system. The NodeSource repository gives you the latest stable releases with standard apt updates, while nvm is better suited for development machines where you juggle multiple Node.js versions. You can also manage containers for your Node.js apps – see how to install Docker on Ubuntu for container-based deployments.

For production deployments, put your Node.js app behind Nginx as a reverse proxy, use PM2 as a process manager, and enable SSL with Let’s Encrypt. Monitor your application logs and set up SSH access for secure remote management.

Related Articles

Ubuntu How To Install XAMPP On Ubuntu 24.04|22.04 Containers Install LXC and Incus on Ubuntu 24.04/22.04 with Web UI mariadb How to Setup MariaDB Galera Cluster on Ubuntu 24.04/22.04 with HAProxy Load Balancer Git Install Gogs Git service on Ubuntu 22.04|20.04|18.04

Press ESC to close