How to Install Node.js and NPM on Ubuntu 24.04

Introduction

Node.js is a powerful, open-source JavaScript runtime environment for building faster and scalable network applications. Node.js allows developers to run JavaScript on the server-side to create high-performing web servers, APIs, real-time applications, and more. NPM is the default package manager for Node.js for managing dependencies, installing libraries, and sharing code efficiently.

This guide shows you how to install Node.js and NPM on Ubuntu 24.04 so that you create web-based applications faster.

Prerequisites

Before you begin:

Install Node.js and NPM

Node.js and NPM are available in the default Ubuntu repository. Follow the steps below to update your system and install the packages using APT.

  1. SSH to your server and update the package information index.

    CONSOLE
    $ sudo apt update
    
  2. Install Node.js.

    CONSOLE
    $ sudo apt install nodejs -y
    
  3. Install NPM.

    CONSOLE
    $ sudo apt install npm -y
    
  4. Ensure you have installed Node.js.

    CONSOLE
    $ node -v
    

    Output:

    v18.19.1 (or higher)
    
  5. Ensure you have installed NPM.

    CONSOLE
    $ npm -v
    

    Output:

    9.2.0 (or higher)
    

Create a Hello World! Application

A Hello World application is the simplest example of a Node.js application that demonstrates basic functionality, such as creating a server and responding to requests. Follow the steps below to create the application.

  1. Make a new project directory.

    CONSOLE
    $ mkdir project
    
  2. Navigate to the new project directory.

    CONSOLE
    $ cd project
    
  3. Initialize a Node.js project.

    CONSOLE
    $ npm init -y
    
  4. Create a new hello-world.js file using a text editor like Nano.

    CONSOLE
    $ nano hello.js
    
  5. Copy the following content and paste it into the hello-world.js file.

    JavaScript
    console.log("Hello, World!");
    
  6. Save and close the file by pressing Ctrl + X, then Y and Enter.

  7. Run the application.

    CONSOLE
    $ node hello.js
    
  8. Ensure you get the following output.

    Hello, World!
    

Conclusion

In this article, you have installed Node.js and NPM on Ubuntu 24.04 The two applications provides you the foundation to build fast and scalable JavaScript-based applications. You've also learned how to verify your installation and created a basic "Hello World!" application, demonstrating the power of Node.js in action.

  • Databases
  • Webservers
  • PHP
  • API
  • Python
  • VPS Guides
  • Network
  • AI
  • Node.js