DEV Community

Cover image for How to Install Node.js and npm on Ubuntu Server 22.04
Ersin KOÇ
Ersin KOÇ

Posted on

How to Install Node.js and npm on Ubuntu Server 22.04

Introduction

Node.js is a powerful JavaScript runtime built on Chrome's V8 engine, enabling developers to build fast and scalable network applications. npm, the Node.js package manager, helps in managing dependencies for your projects. In this tutorial, we will guide you through the installation of Node.js and npm on Ubuntu Server 22.04. With EcoStack Cloud VPS, you can quickly set up and run your Node.js applications in a robust environment.

Requirements

  • A VPS with Ubuntu Server 22.04. If you need a VPS, consider using EcoStack Cloud, which offers reliable and scalable VPS solutions with SSH access.
  • Basic familiarity with SSH and terminal commands.

Steps to Install Node.js and npm on Ubuntu Server 22.04

Step 1: Connect to Your VPS

Start by connecting to your EcoStack Cloud VPS via SSH. Replace your-username and your-vps-ip with your actual username and VPS IP address.

   ssh your-username@your-vps-ip
Enter fullscreen mode Exit fullscreen mode

Step 2: Update the Package Index

Before installing Node.js, update your package index to ensure you get the latest versions of software packages.

   sudo apt update
Enter fullscreen mode Exit fullscreen mode

Step 3: Install Node.js

There are several ways to install Node.js on Ubuntu. The easiest method is to use the NodeSource binary distributions. First, download and run the NodeSource setup script for Node.js 16 (the LTS version as of this guide):

   curl -fsSL https://deb.nodesource.com/setup_16.x | sudo -E bash -
Enter fullscreen mode Exit fullscreen mode

After adding the NodeSource repository, install Node.js and npm with:

   sudo apt install -y nodejs
Enter fullscreen mode Exit fullscreen mode

This command installs both Node.js and npm.

Step 4: Verify Installation

To ensure Node.js and npm are installed correctly, check their versions:

  • For Node.js:

     node -v
    

    This should output the version of Node.js installed (e.g., v16.x.x).

  • For npm:

     npm -v
    

    This should display the npm version (e.g., 8.x.x).

Step 5: Manage Packages with npm

npm allows you to manage packages for your Node.js applications. You can install packages locally for a specific project or globally for system-wide use.

  • To install a package locally:

     npm install package-name
    

    This command installs the package in the node_modules directory within your project.

  • To install a package globally:

     sudo npm install -g package-name
    

    This makes the package available for use in any project.

Step 6: Update npm (Optional)

To update npm to the latest version, use the following command:

   sudo npm install -g npm@latest
Enter fullscreen mode Exit fullscreen mode

Step 7: Create a Simple Node.js Application (Optional)

To test your Node.js setup, you can create a simple application. Create a new file named app.js:

   nano app.js
Enter fullscreen mode Exit fullscreen mode

Add the following code to app.js:

   // Load HTTP module
   const http = require("http");
   const hostname = "127.0.0.1";
   const port = 3000;

   // Create HTTP server
   const server = http.createServer((req, res) => {
     res.statusCode = 200;
     res.setHeader("Content-Type", "text/plain");
     res.end("Hello, World!\n");
   });

   // Listen on port 3000
   server.listen(port, hostname, () => {
     console.log(`Server running at http://${hostname}:${port}/`);
   });
Enter fullscreen mode Exit fullscreen mode

Save and close the file. Run your Node.js application with:

   node app.js
Enter fullscreen mode Exit fullscreen mode

Visit http://your-vps-ip:3000 in your browser. You should see "Hello, World!" displayed on the page, confirming that your Node.js server is running.

Conclusion

You have successfully installed Node.js and npm on your Ubuntu Server 22.04. With EcoStack Cloud VPS, you now have a solid platform to develop and deploy your JavaScript applications. Continue exploring Node.js and npm to leverage their full potential in your projects.

Additional Resources

Call-to-Action

Start developing your Node.js applications today with EcoStack Cloud VPS. Enjoy a seamless, powerful, and scalable environment that supports your growth.


Top comments (0)