Install and Manage Node.js and NPM on Ubuntu 20.04

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.

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:

Download the Node.js version 22.x PPA installation script:

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

Refer to the official NodeSource documentation to verify all available Node.js versions.

Run the Node.js setup script:

$ sudo -E bash nodesource_setup.sh

Install Node.js and NPM:

$ sudo apt-get install -y nodejs npm

Check the installed Node.js version:

Output: v22.14.0

Check the installed NPM version:

Output: 10.9.2

Test the Installation

Create a new project directory, for example:

Navigate into the directory:

Initialize a new Node.js project:

Install the Express module:

Create the application file index.js:

Add 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:

Reload the firewall:

Start the Node.js application:

Now, 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.sh

Check the NVM documentation to ensure you have the latest version.

Run the script to install NVM:

Reload your environment variables:

List all available Node.js versions:

Sample 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:

List installed Node.js versions and verify the default version:

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)

Activate a specific Node.js version, such as 20:

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

Check the active Node.js version:

Output: v20.14.0

Check the NPM version:

Output: 10.7.0

Set a specific Node.js version as the default in your environment:

Conclusion

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.

Source: vultr.com

Create a Free Account

Register now and get access to our Cloud Services.

Posts you might be interested in: