How to Install Node.js and NPM on Ubuntu 24.04
Introduction
Node.js is an open-source, cross-platform JavaScript runtime environment for developing server-side scalable applications. The Node Package Manager (NPM) is an essential tool for managing libraries, dependencies, and scripts in a Node.js project. By installing Node.js and NPM on your server, you can use JavaScript as a server-side language.
This article explains how to install Node.js and NPM on Ubuntu 24.04. You will install both Node.js and NPM using the NodeSource PPA and run multiple versions using NVM (Node Version Manager) on your server.
Prerequisites
Before you begin:
Access the server using SSH.
Create a non-root user with sudo privileges and switch to the user.
Choose a Node.js Version
Consider the stability, performance, and application compatibility when selecting a Node.js version to use in your project. Below are the supported Node.js release types to consider when selecting a version.
- Latest Release: Includes the latest Node.js updates, improvements, and features. It's ideal for applications that require the latest feature updates.
- LTS Release: A long-term release (LTS) is the latest stable Node.js version supported for a long period from the release date. LTS releases are suitable for production environments as they receive critical bug fixes, security updates, and performance improvements.
Consider the following factors when choosing a Node.js release:
- Stability and Support: If your project is in production or a stable and reliable environment, use the latest LTS release.
- Feature Requirements: If your project relies on the latest Node.js features or performance enhancements, use the latest release version.
Install Node.js
The Ubuntu 24.04 APT repositories contain a pre-built Node.js package that may not be the latest version when installed. To install the latest version of Node.js, use the NodeSource PPA to install the latest repository information for a specific Node.js version.
Download your desired Node.js version PPA installation script. For example, run the following command to use the Node.js version
22.x
.console$ curl -fsSL https://deb.nodesource.com/setup_22.x -o nodesource_setup.sh
Visit the official NodeSource documentation to verify all available Node.js versions you can install on your server.
Run the Node.js setup script.
console$ sudo -E bash nodesource_setup.sh
Install Node.js and NPM using the following command.
console$ sudo apt-get install -y nodejs npm
View the installed Node.js version on your server.
console$ node -v
Output:
v22.2.0
View the installed NPM version on your server.
console$ npm -v
Output:
10.7.0
Test the Installation
Create a new project directory such as
example-site
.console$ mkdir example-site
Switch to the directory.
console$ cd example-site
Initialize a new Node.js project using
npm
.console$ npm init -y
Install the
express
module usingnpm
.console$ npm install express
Create a new
index.js
application file.console$ nano index.js
Add the following code to the file.
jsconst express = require('express') const app = express() const port = 3000 app.get('/', (req, res) => { res.send('Hello World! Greetings from Vultr') }) app.listen(port, () => { console.log(`Example app listening on port ${port}`) })
Save and close the file.
The above Node.js code creates a new express application that listens for connections on the host port
3000
and outputs aHello World! Greetings from Vultr
message.Allow port
3000
through the firewall to enable network connections to the application.console$ sudo ufw allow 3000
Reload the firewall to apply changes.
console$ sudo ufw reload
Start the Node.js application.
console$ node index.js
Access your Node.js application using your server IP and port
3000
in a web browser such as Chrome.http://<server-ip>:3000
Install Multiple Versions with Node Version Manager (NVM)
NVM (Node Version Manager) is a useful tool that enables you to install and switch between multiple Node.js versions on your server. Follow the steps below to install multiple Node.js versions on your server using NVM depending on your project requirements.
Download the latest NVM installation script on your server.
console$ curl -O https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh
Please, refer to the NVM documentation to get the latest version at the time of your installation.
Run the script to install NVM in your user environment.
console$ bash install.sh
Reload your server environment variables.
console$ source ~/.profile
List all available Node.js versions you can install on your server.
console$ nvm ls-remote
Your output should look like the one below.
... 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 a specific Node.js version. For example, Node.js 20.
console$ nvm install 20
List all installed Node.js versions and verify the default version on your server.
console$ nvm ls
Output:
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)
Run the following command to install a specific Node.js version such as
20
using NVM.console$ nvm use 20
Output:
Now using node v20.14.0 (npm v10.7.0)
View the active Node.js version in your user environment.
console$ node -v
Output:
v20.14.0
View the NPM version in your user environment.
console$ npm -v
Output:
10.7.0
Run the following command to a specific version as the default Node.js version in your environment.
console$ nvm alias default 20
Conclusion
You have installed Node.js and NPM on your Ubuntu 24.04 server. You can develop applications and switch between different Node.js versions on your server using the Node Version Manager (NVM). For more information, please visit the Node.js documentation.