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.
Matching infrastructure at centron
Node applications need somewhere to run: ccloud³ VMs with full root access from €3.12 per month – or as a managed server if you would rather not run it yourself. Rent a cloud 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
$ sudo apt updateDownload 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.shVisit the official NodeSource documentation to confirm which versions are available.
Run the Setup Script
$ sudo -E bash nodesource_setup.shInstall Node.js and NPM
$ sudo apt-get install -y nodejs npmCheck Node.js Version
$ node -vExpected output:
v22.14.0
Check NPM Version
$ npm -vExpected output:
10.9.2
Test the Installation
Create a New Project Directory
$ mkdir example-siteNavigate to the Project Directory
$ cd example-siteInitialize a Node.js Project
$ npm init -yInstall Express Framework
$ npm install expressCreate an Application File
$ nano index.jsAdd 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
$ sudo ufw allow 3000Reload the Firewall
$ sudo ufw reloadRun the Application
$ node index.jsAccess 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.shCheck the latest NVM version on the official documentation before proceeding.
Install NVM
$ bash install.shReload the Environment
$ source ~/.profileList Available Node.js Versions
$ nvm ls-remoteExample 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
$ nvm install 20List Installed Versions
$ nvm lsExample 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
$ nvm use 20Output:
Now using node v20.14.0 (npm v10.7.0)
Check Node.js and NPM Versions
$ node -v
$ npm -vSet a Default Node.js Version
$ nvm alias default 20Conclusion
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.
Testen Sie Ihr Setup auf ccloud³
Registrieren Sie sich in der ccloud³ und erhalten Sie 200 € Startguthaben für Ihr Projekt – z. B. für eine PostgreSQL-VM mit automatischen Backups.