How to Install Node.js and NPM on Ubuntu 20.04

Updated on 08 May, 2025
How to Install Node.js and NPM on Ubuntu 20.04 header image

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 20.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:

Choose a Node.js Version

When selecting a Node.js version for your project, consider factors like stability, performance, and application compatibility. Below are the supported Node.js release types:

  • Latest Release: Features the newest updates, improvements, and functionalities. Ideal for applications needing the latest features.
  • LTS Release: A stable, long-term release that receives critical updates and bug fixes. Best for production environments due to its reliability and extended support.

Factors to consider when choosing a release:

  • Stability and Support: For production or reliable environments, the latest LTS release is recommended.
  • Feature Requirements: For projects requiring the newest features or performance improvements, the latest release version is a good choice.

Install Node.js

The default Ubuntu 20.04 APT repositories may not have the latest version of Node.js. To install the most recent version, use the NodeSource PPA to add the latest repository for the specific Node.js version you want to install.

  1. Update the server package index.

    console
    $ sudo apt update
    
  2. 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.

  3. Run the Node.js setup script.

    console
    $ sudo -E bash nodesource_setup.sh
    
  4. Install Node.js and NPM using the following command.

    console
    $ sudo apt-get install -y nodejs npm
    
  5. View the installed Node.js version on your server.

    console
    $ node -v
    

    Output:

    v22.14.0
  6. View the installed NPM version on your server.

    console
    $ npm -v
    

    Output:

    10.9.2

Test the Installation

  1. Create a new project directory such as example-site.

    console
    $ mkdir example-site
    
  2. Switch to the directory.

    console
    $ cd example-site
    
  3. Initialize a new Node.js project using npm.

    console
    $ npm init -y
    
  4. Install the express module using npm.

    console
    $ npm install express
    
  5. Create a new index.js application file.

    console
    $ nano index.js
    
  6. Add the following code to the file.

    js
    const 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 a Hello World! Greetings from Vultr message.

  7. Allow port 3000 through the firewall to enable network connections to the application.

    console
    $ sudo ufw allow 3000
    
  8. Reload the firewall to apply changes.

    console
    $ sudo ufw reload
    
  9. Start the Node.js application.

    console
    $ node index.js
    
  10. Access your Node.js application using your server IP and port 3000 in a web browser such as Chrome.

    http://<server-ip>:3000

    View the working Node.js application in a web browser

Install Multiple Versions with Node Version Manager (NVM)

NVM (Node Version Manager) is a handy tool for managing multiple Node.js versions on your server. You can install and switch between different Node.js versions based on your project needs. Follow the steps below to set up NVM and install the required Node.js versions.

  1. 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.

  2. Run the script to install NVM in your user environment.

    console
    $ bash install.sh
    
  3. Reload your server environment variables.

    console
    $ source ~/.profile
    
  4. 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
  5. Install a specific Node.js version. For example, Node.js 20.

    console
    $ nvm install 20
    
  6. 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)
  7. 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)
  8. View the active Node.js version in your user environment.

    console
    $ node -v
    

    Output:

    v20.14.0
  9. View the NPM version in your user environment.

    console
    $ npm -v
    

    Output:

    10.7.0
  10. 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 successfully installed Node.js and NPM on your Ubuntu 20.04 server. With the Node Version Manager (NVM), you can easily switch between different Node.js versions to meet your project's requirements.

Comments

No comments yet.