Install Node.js and NPM on Ubuntu 22.04

Node.js is a cross-platform, open-source JavaScript runtime environment used for building scalable server-side applications. NPM serves as the package manager that handles dependencies, libraries, and scripts, making it easier to develop and manage server-based JavaScript projects.

This guide explains how to install Node.js and NPM on Ubuntu 22.04 using the NodeSource PPA and how to manage multiple Node.js versions through NVM (Node Version Manager).

Prerequisites

Before you begin, make sure you have the following:

  • An Ubuntu 22.04 server.
  • SSH access as a non-root user with sudo privileges.
  • An updated package index on your Ubuntu server.

Choose a Node.js Version

When selecting which version of Node.js to install, consider these points:

  • Latest Release: Contains the most recent features and improvements—ideal for development or projects requiring the latest capabilities.
  • LTS (Long-Term Support): A stable version recommended for production environments.

Important factors include:

  • Stability: Use the latest LTS for reliability in production.
  • Features: Choose the latest release for cutting-edge functionality.

Install Node.js

The Ubuntu 22.04 APT repositories may not include the latest Node.js release. To install the most up-to-date version, add the NodeSource PPA for your desired Node.js branch.

Update the Package Index

Download the NodeSource Setup Script

Download the installation script for your chosen Node.js version. For example, to install Node.js 22.x:

$ curl -fsSL https://deb.nodesource.com/setup_22.x -o nodesource_setup.sh

Visit the official NodeSource documentation to confirm which versions are available.

Run the Setup Script

$ sudo -E bash nodesource_setup.sh

Install Node.js and NPM

$ sudo apt-get install -y nodejs npm

Check Node.js Version

Expected output:

v22.14.0

Check NPM Version

Expected output:

10.9.2

Test the Installation

Create a New Project Directory

Navigate to the Project Directory

Initialize a Node.js Project

Install Express Framework

Create an Application File

Add the Following Code

const express = require('express')
const app = express()
const port = 3000

app.get('/', (req, res) => {
    res.send('Hello World! Greetings from centron')
})

app.listen(port, () => {
    console.log(`Example app listening on port ${port}`)
})

This example creates an Express web server that responds with “Hello World! Greetings from centron” when accessed at port 3000.

Allow Port 3000 Through the Firewall

Reload the Firewall

Run the Application

Access your app in the browser using your server’s IP and port 3000:
http://<server-ip>:3000

Install Multiple Versions with NVM

NVM (Node Version Manager) enables you to install and manage multiple Node.js versions on your system. Follow these steps to use NVM for version control.

Download the NVM Installation Script

$ curl -O https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh

Check the latest NVM version on the official documentation before proceeding.

Install NVM

Reload the Environment

List Available Node.js Versions

Example output:

v20.13.1   (LTS: Iron)
v20.14.0   (Latest LTS: Iron)
v21.0.0
v21.6.0
v22.0.0
v22.2.0

Install a Specific Version

List Installed Versions

Example output:

v20.14.0
-> v21.7.3
default -> 20 (-> v20.14.0)
node -> stable (-> v21.7.3) (default)
lts/* -> lts/iron (-> v20.14.0)

Switch to a Specific Version

Output:

Now using node v20.14.0 (npm v10.7.0)

Check Node.js and NPM Versions

Set a Default Node.js Version

Conclusion

In this tutorial, you installed Node.js and NPM on Ubuntu 22.04 using NodeSource. You also learned how to use NVM to manage multiple Node.js versions, enabling a flexible development environment for server-side JavaScript applications.

Source: vultr.com

Create a Free Account

Register now and get access to our Cloud Services.

Posts you might be interested in: