Node.js is an open-source, cross-platform JavaScript runtime environment designed for building scalable server-side applications. The Node Package Manager (NPM) is a crucial utility for handling libraries, dependencies, and scripts within a Node.js project. By installing Node.js and NPM on your server, you can leverage JavaScript as a server-side language.
This guide details how to set up Node.js and NPM on Ubuntu 20.04. You will install both Node.js and NPM using the NodeSource PPA and learn how to run multiple versions with NVM (Node Version Manager).
Prerequisites
Before you start, ensure you have the following:
- An Ubuntu 20.04 server.
- SSH access as a non-root user with sudo privileges.
- A fully updated server.
Matching infrastructure at centron
Ubuntu servers without your own hardware: ccloud³ VMs with full root access from €3.12 per month, billed by the hour and ready in seconds. Rent a cloud server →
Choose a Node.js Version
When selecting a Node.js version, consider aspects such as stability, performance, and application compatibility. The supported Node.js release types are:
- Latest Release: Contains the newest updates, improvements, and features. Best for applications requiring the latest capabilities.
- LTS Release: A long-term support version that receives critical updates and bug fixes. Recommended for production environments due to reliability and extended maintenance.
Factors to keep in mind:
- Stability and Support: For production or dependable environments, the latest LTS release is recommended.
- Feature Requirements: If your project needs the newest features or performance enhancements, opt for the latest release version.
Install Node.js
The default APT repositories in Ubuntu 20.04 might not offer the most recent Node.js version. Use the NodeSource PPA to add the latest repository for your preferred Node.js version.
Update the server package index:
$ sudo apt updateDownload the Node.js version 22.x PPA installation script:
$ curl -fsSL https://deb.nodesource.com/setup_22.x -o nodesource_setup.shRefer to the official NodeSource documentation to verify all available Node.js versions.
Run the Node.js setup script:
$ sudo -E bash nodesource_setup.shInstall Node.js and NPM:
$ sudo apt-get install -y nodejs npmCheck the installed Node.js version:
$ node -vOutput: v22.14.0
Check the installed NPM version:
$ npm -vOutput: 10.9.2
Test the Installation
Create a new project directory, for example:
$ mkdir example-siteNavigate into the directory:
$ cd example-siteInitialize a new Node.js project:
$ npm init -yInstall the Express module:
$ npm install expressCreate the application file index.js:
$ nano index.jsAdd the following code to the file:
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 Node.js script creates an Express application that listens on port 3000 and returns a "Hello World! Greetings from centron" message.
Allow port 3000 through the firewall:
$ sudo ufw allow 3000Reload the firewall:
$ sudo ufw reloadStart the Node.js application:
$ node index.jsNow, open a browser and access your application using:
http://<server-ip>:3000
Install Multiple Versions with Node Version Manager (NVM)
NVM (Node Version Manager) is a convenient tool for managing multiple Node.js versions. You can install and switch between different versions as needed. Follow these steps to set up NVM and install the desired Node.js versions.
Download the latest NVM installation script:
$ curl -O https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.shCheck the NVM documentation to ensure you have the latest version.
Run the script to install NVM:
$ bash install.shReload your environment variables:
$ source ~/.profileList all available Node.js versions:
$ nvm ls-remoteSample output:
...
v20.13.1 (LTS: Iron)
v20.14.0 (Latest LTS: Iron)
v21.0.0
v21.6.0
v21.6.1
v21.6.2
v21.7.0
v21.7.1
v21.7.2
v21.7.3
v22.0.0
v22.1.0
v22.2.0
Install Node.js version 20:
$ nvm install 20List installed Node.js versions and verify the default version:
$ nvm lsOutput:
v20.14.0
-> v21.7.3
default -> 20 (-> v20.14.0)
iojs -> N/A (default)
unstable -> N/A (default)
node -> stable (-> v21.7.3) (default)
stable -> 21.7 (-> v21.7.3) (default)
lts/* -> lts/iron (-> v20.14.0)
Activate a specific Node.js version, such as 20:
$ nvm use 20Output: Now using node v20.14.0 (npm v10.7.0)
Check the active Node.js version:
$ node -vOutput: v20.14.0
Check the NPM version:
$ npm -vOutput: 10.7.0
Set a specific Node.js version as the default in your environment:
$ nvm alias default 20Conclusion
You have successfully installed Node.js and NPM on Ubuntu 20.04. With Node Version Manager (NVM), you can seamlessly switch between different Node.js versions to suit your project requirements.
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.