Node.js is a popular JavaScript runtime built on Chrome’s V8 JavaScript engine, allowing developers to run JavaScript on the server-side. This guide will walk you through the process of installing Node.js on Ubuntu 22.04 and setting up a simple Node.js application.

Step 1: Deploying a Cloud Instance on Shape.host

  • Log in to Shape.host Dashboard:
  • Navigate to the Shape.host website and log in to your account.
  • Create a New Instance:
  • Click on the “Create” button located at the top right corner of the dashboard.
  • From the dropdown menu, select “Instances”.
Shape.host Resources dashboard showing the Create menu and instance rebuild controls.
  • Select Instance Location:
  • Choose the desired location for your server. For this tutorial, we’ll select “New York, USA”.
Shape.host Create New Instance form with the available data center locations.
  • Choose a Plan:
  • Select a plan that fits your requirements. For example, you might choose a plan with 2 cores CPU, 2 GB Memory, and 50 GB SSD disk space.
  • Select an Operating System:
  • Scroll down to the “Choose an image” section and select “Ubuntu 22.04”.
Shape.host instance form with a server plan and Ubuntu image selected.
  • Configure Additional Options:
  • (Optional) You can configure additional options like User Data Configuration and IPv6 Networking.
  • Enter a hostname for your instance, e.g., “Tutorial Ubuntu”.
  • Click on the “Create instance” button to deploy the instance.
Shape.host instance form showing hostname, instance count and the Create Instance button.

Step 2: Connecting to Your Instance

  • Retrieve SSH Credentials:
  • Note the IP address of your newly created instance from the Shape.host dashboard.
  • Connect via SSH:
  • Open a terminal on your local machine.
  • Use the following command to connect to your instance:
    ssh root@your_instance_ip
  • Replace your_instance_ip with the actual IP address of your instance.

Step 3: Update and Upgrade Your System

Ensure your system is up-to-date and all packages are upgraded to the latest versions:

apt update
apt upgrade -y
Terminal showing APT package management commands and output.

Method 1: Install Node.js from Ubuntu Repositories

The simplest way to install Node.js is to use the Ubuntu package repository. However, the version available might not be the latest.

  1. Install Node.js:
   apt install nodejs -y
Terminal showing APT package management commands and output.
  1. Install npm (Node Package Manager):
   apt install npm -y
Terminal showing APT package management commands and output.
  1. Verify the Installation: Check the versions to ensure the installation was successful:
   node -v
   npm -v
Terminal showing Node.js or npm version output.

Method 2: Install Node.js Using NodeSource PPA

To install the latest version of Node.js, you can use the NodeSource PPA.

Resolve Potential Conflicts

First, remove any conflicting packages that might interfere with the Node.js installation:

dpkg --remove --force-remove-reinstreq libnode72
apt clean
apt autoremove -y
  1. Add NodeSource PPA: Add the NodeSource repository. Replace setup_18.x with the version you wish to install (e.g., setup_16.x for Node.js 16).
   curl -fsSL https://deb.nodesource.com/setup_18.x | bash -
  1. Install Node.js:
   apt install nodejs -y
  1. Verify the Installation:
   node -v
   npm -v

Method 3: Install Node.js Using nvm (Node Version Manager)

Using nvm allows you to manage multiple versions of Node.js on the same system.

  1. Install nvm: Download and install nvm:
   curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.3/install.sh | bash

Load nvm:

   source ~/.bashrc
  1. Install Node.js with nvm: List available Node.js versions:
   nvm ls-remote

Install a specific version, for example, the latest LTS version:

   nvm install --lts
  1. Verify the Installation:
   node -v
   npm -v

Setting Up a Simple Node.js Application

To ensure that Node.js is working correctly, you can create a simple application.

  1. Create a Project Directory:
   mkdir -p /var/www/nodeapp
   cd /var/www/nodeapp
Terminal showing file and directory setup commands.
  1. Initialize a Node.js Project:
   npm init -y
Terminal showing file and directory setup commands.
Terminal showing echo commands or output during Setting Up a Simple Node.js Application.

This command creates a package.json file with default settings.

  1. Create a Simple Application: Create an index.js file:
   nano index.js
Terminal showing nano commands or output during Setting Up a Simple Node.js Application.

Add the following content:

   const http = require('http');

   const hostname = '0.0.0.0';
   const port = 3000;

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

   server.listen(port, hostname, () => {
       console.log(`Server running at http://${hostname}:${port}/`);
   });
Terminal showing hostname configuration or system identification details.
  1. DNS Configuration: Ensure that tutorials.shape.host is correctly pointed to your server’s IP address. This can typically be done in your domain registrar’s DNS settings by adding an A record.
  2. Firewall Configuration: Ensure that your server’s firewall is configured to allow traffic on port 3000. If you are using ufw, you can allow traffic to port 3000 with the following command:
   ufw allow 3000
Terminal showing firewall configuration commands and rules.
  1. Run the Application:
   node index.js
Terminal showing the command to edit a Node.js application file.

You should see output indicating that the server is running:

   Server running at http://0.0.0.0:3000/
  1. Test the Application: Open your web browser and navigate to http://tutorials.shape.host:3000/. You should see “Hello World” displayed.
Browser displaying the Node.js Hello World response.

For those seeking reliable and scalable hosting solutions, Shape.host offers a variety of Linux SSD VPS services. Whether you’re a developer needing a robust environment for your applications or a business looking for scalable solutions, Shape.host provides the infrastructure and support to meet your needs.