UbuntuUbuntu Based

How To Install Express.JS on Ubuntu 24.04 LTS

Install Express.JS on Ubuntu 24.04

Express.js has become an indispensable tool in the world of web development, offering a minimalist and flexible Node.js web application framework. As developers seek robust and efficient solutions for building web applications and APIs, Express.js stands out for its simplicity and powerful features. This guide will walk you through the process of installing Express.js on Ubuntu 24.04 LTS, the latest long-term support release of one of the most popular Linux distributions.

Ubuntu 24.04 LTS provides a stable and secure foundation for your development environment. By combining the reliability of Ubuntu with the versatility of Express.js, you’ll create a powerful platform for your web development projects. Whether you’re a seasoned developer or just starting your journey in web development, this tutorial will equip you with the knowledge to set up Express.js on your Ubuntu system.

In the following sections, we’ll cover everything from preparing your system to creating your first Express.js application. We’ll explore multiple installation methods, troubleshoot common issues, and provide you with best practices to ensure a smooth setup process. Let’s dive in and unlock the potential of Express.js on your Ubuntu 24.04 LTS system.

Prerequisites

Before we begin the installation process, it’s crucial to ensure that your system meets the necessary requirements. Here’s what you’ll need:

  • A system running Ubuntu 24.04 LTS (Lunar Lobster)
  • Access to a non-root user account with sudo privileges
  • A stable internet connection for downloading packages
  • Basic familiarity with the command line interface

To prepare your system, open a terminal and update your package index:

sudo apt update

Then, upgrade your existing packages to their latest versions:

sudo apt upgrade -y

These steps ensure that your system is up-to-date and ready for the Express.js installation process.

Step 1: Install Node.js and NPM

Express.js is a framework built on Node.js, so our first task is to install Node.js and its package manager, NPM (Node Package Manager). Ubuntu 24.04 LTS offers several methods to install Node.js, each with its own advantages. We’ll explore three popular options: using APT, the NodeSource PPA, and NVM (Node Version Manager).

Option 1: Using APT

The simplest method to install Node.js and NPM is through Ubuntu’s default package manager, APT. This method is straightforward but may not always provide the latest version of Node.js.

First, update your package index:

sudo apt update

Then, install Node.js and NPM:

sudo apt install nodejs npm -y

After the installation completes, verify the installed versions:

node -v
npm -v

These commands will display the version numbers of Node.js and NPM respectively.

Option 2: Using NodeSource PPA

For those who prefer to work with the latest stable version of Node.js, the NodeSource PPA (Personal Package Archive) is an excellent option. This method allows you to install a more recent version of Node.js than what’s available in the default Ubuntu repositories.

First, add the NodeSource repository to your system:

curl -fsSL https://deb.nodesource.com/setup_22.x | sudo -E bash -

This command downloads and executes a script that adds the NodeSource repository to your system. After the script completes, install Node.js and NPM:

sudo apt-get install -y nodejs npm

Verify the installation by checking the versions:

node -v
npm -v

Option 3: Using NVM (Node Version Manager)

NVM offers the most flexibility for managing Node.js versions. It allows you to install and switch between multiple versions of Node.js on the same system, which is particularly useful for developers working on projects with different version requirements.

To install NVM, run the following command:

curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash

After the installation script completes, close and reopen your terminal or run the following command to load NVM:

source ~/.bashrc

Now, you can install the latest version of Node.js:

nvm install node

Set the default Node.js version:

nvm alias default $(nvm current)

Verify the installation:

node -v
npm -v

With Node.js and NPM successfully installed, you’re ready to move on to installing Express.js.

Step 2: Install Express.js

Now that we have Node.js and NPM set up on our Ubuntu 24.04 LTS system, we can proceed with installing Express.js. Express.js is typically installed as a dependency for individual projects rather than globally on your system. This approach allows you to manage different versions of Express.js for different projects.

Let’s create a new project directory and set up Express.js:

  1. Create and navigate to a new project directory:
    mkdir myexpressapp && cd myexpressapp
  2. Initialize a new Node.js project:
    npm init -y

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

  3. Install Express.js in the project directory:
    npm install express

After the installation completes, you can verify that Express.js has been added to your project by checking the package.json file:

cat package.json

You should see Express listed under the “dependencies” section of the file.

Step 3: Create a Simple Express Application

With Express.js installed, let’s create a basic application to ensure everything is working correctly. We’ll create a simple server that responds with “Hello World!” when accessed.

  1. Create a new file named app.js in your project directory:
    touch app.js
  2. Open app.js in your preferred text editor. For example, using nano:
    nano app.js
  3. Add the following code to app.js:
    
    const express = require('express');
    const app = express();
    const port = 3000;
    
    app.get('/', (req, res) => {
      res.send('Hello World!');
    });
    
    app.listen(port, () => {
      console.log(`Example app listening at http://localhost:${port}`);
    });
        
  4. Save the file and exit the text editor. In nano, you can do this by pressing Ctrl+X, then Y, then Enter.
  5. Run the application:
    node app.js

If everything is set up correctly, you should see the message “Example app listening at http://localhost:3000” in your terminal.

To test the server, open a web browser and navigate to http://localhost:3000. You should see the message “Hello World!” displayed.

Step 4: Configure Firewall

Ubuntu 24.04 LTS comes with a built-in firewall called UFW (Uncomplicated Firewall). If you’re planning to access your Express.js application from other devices on your network or the internet, you’ll need to configure UFW to allow traffic on the port your application is using.

By default, our Express.js application is running on port 3000. Let’s configure UFW to allow traffic on this port:

  1. Allow traffic on port 3000:
    sudo ufw allow 3000
  2. Enable UFW if it’s not already active:
    sudo ufw enable
  3. Verify the UFW status and ensure the new rule is active:
    sudo ufw status

You should see a rule allowing traffic on port 3000 in the output.

Remember, opening ports can potentially expose your system to security risks. Always be cautious about which ports you open and consider using a reverse proxy like Nginx for production environments.

Step 5: Troubleshooting Common Issues

Even with careful installation, you might encounter some issues when setting up Express.js on Ubuntu 24.04 LTS. Here are some common problems and their solutions:

Node.js or NPM Not Found

If you receive a “command not found” error when trying to run node or npm, it’s likely that Node.js wasn’t installed correctly or the system PATH wasn’t updated. Try the following:

  1. Verify the installation:
    which node
    which npm

    If these commands don’t return a path, reinstall Node.js using one of the methods described earlier.

  2. If you used NVM, ensure it’s properly loaded:
    source ~/.bashrc

Permission Errors

If you encounter permission errors when installing packages globally with NPM, avoid using sudo with npm. Instead, configure NPM to install global packages in your home directory:

mkdir ~/.npm-global
npm config set prefix '~/.npm-global'
echo 'export PATH=~/.npm-global/bin:$PATH' >> ~/.profile
source ~/.profile

Express.js Module Not Found

If Node.js can’t find the Express module when you run your application, ensure you’ve installed Express.js in your project directory:

npm install express

Also, check that the require statement in your app.js file matches the installed module name exactly.

Port Already in Use

If you see an error message saying the port is already in use when starting your Express.js application, another process might be using that port. You can either:

  1. Choose a different port in your app.js file, or
  2. Find and stop the process using the current port:
    sudo lsof -i :3000
    sudo kill -9 PID

    Replace PID with the process ID from the lsof command output.

Continue experimenting, learning, and building with Express.js, and you’ll soon discover why it’s become such a popular choice among developers worldwide. Happy coding!

Congratulations! You have successfully installed ExpressJS. Thanks for using this tutorial for installing the ExpressJS web application framework on Ubuntu 24.04 LTS system. For additional help or useful information, we recommend you check the official ExpressJS website.

VPS Manage Service Offer
If you don’t have time to do all of this stuff, or if this is not your area of expertise, we offer a service to do “VPS Manage Service Offer”, starting from $10 (Paypal payment). Please contact us to get the best deal!

r00t

r00t is a seasoned Linux system administrator with a wealth of experience in the field. Known for his contributions to idroot.us, r00t has authored numerous tutorials and guides, helping users navigate the complexities of Linux systems. His expertise spans across various Linux distributions, including Ubuntu, CentOS, and Debian. r00t's work is characterized by his ability to simplify complex concepts, making Linux more accessible to users of all skill levels. His dedication to the Linux community and his commitment to sharing knowledge makes him a respected figure in the field.
Back to top button