How to Install Express.js on Ubuntu 22.04 or 20.04

For those working with web development on Ubuntu systems, installing Express.js can be a pivotal step. This server-side framework, built on Node.js, is renowned for its efficiency, versatility, and robust feature set. Whether you’re using Ubuntu 22.04 Jammy Jellyfish or the older but stable 20.04 Focal Fossa, this guide will help you install Express.js and get started with its rich functionalities.

Key Features of Express.js

  • Speed and Efficiency: Express.js optimizes performance, making it an ideal companion for Node.js’ event-driven architecture.
  • Middleware Support: Middleware functions in Express.js can manipulate request and response objects, offering a layer of control during the HTTP cycle.
  • Flexible Routing: The framework provides an organized way to manage routes, a feature particularly useful in large-scale applications.
  • Template Engine Support: With multiple template engines like Pug and EJS support, Express.js enables dynamic server-side HTML rendering.
  • Debugging Ease: The framework offers straightforward options, simplifying the development process.
  • Community and Ecosystem: A strong community and many middleware and libraries extend Express.js functionalities and encourage code reusability.

This guide will focus on how to install Express.js on Ubuntu 22.04 or 20.04, but the methods are also applicable to other Ubuntu releases that are still supported. Stay tuned for detailed installation steps.

Section 1: Setup Node.js Environment for Express.js on Ubuntu 22.04 or 20.04

The backbone of any Express.js project is Node.js, as it allows the execution of JavaScript code on the server side. This section aims to provide a step-by-step guide to setting up Node.js on your Ubuntu system.

Step 1: Refreshing Your Ubuntu System

Initiating any new installation process on an Ubuntu system calls for an updated software environment. By updating your system’s existing packages, you’re ensuring the most recent versions of all installed software are up-to-date, mitigating the potential for software conflicts.

Execute the following command to update your system:

sudo apt update && sudo apt upgrade

The above command has two components: sudo apt update updates the package lists for upgrades, and sudo apt upgrade upgrades all packages that are updatable.

Step 2: Install Initial Packages

As part of the Node.js setup, we’ll need to install several additional packages that facilitate a smooth installation process. These include curl for data transfer via URLs, git for version control, and wget for network downlinking.

Run the command below to install these packages:

sudo apt install curl git wget -y

The -y flag is added at the end to automatically answer ‘yes’ to any prompts, thereby allowing the installation to proceed without requiring manual intervention.

Step 3: Import the NodeSource Repository on Ubuntu

Our choice for installing Node.js is the NodeSource repository. Known for providing the latest versions of Node.js, it’s an ideal choice for our setup.

We first need to import the repository by executing a script provided by NodeSource. The following command fetches the script and runs it:

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

In this command, curl -fsSL https://deb.nodesource.com/setup_lts.x fetches the script from the provided URL, and | sudo bash - pipes it to bash, which executes the script with root permissions.

Next, let’s update the system’s package lists to include the newly added packages from the NodeSource repository:

sudo apt update

Step 4: Install Node.js for Express.js on Ubuntu 22.04 or 20.04

With the preliminary setup complete, it’s time to install Node.js. Use the following command to initiate the installation from the NodeSource repository:

sudo apt install nodejs

Step 5: Verifying the Node.js Installation on Ubuntu

After the installation, it’s prudent to validate the Node.js installation to ensure it’s ready for use. This validation confirms that the installation process was successful.

To verify your Node.js installation, use the following command:

node --version

The command will output the installed version of Node.js, thereby confirming a successful installation.

Section 2: Install Express.js on Ubuntu 22.04 or 20.04

Following the installation of Node.js, the next step is deploying Express.js. Express.js is a potent element of our Node.js landscape, being a minimalist yet versatile web application framework that supplies a comprehensive range of features for web and mobile applications.

Step 1: Update NPM to the Most Recent Version on Ubuntu

As a preliminary action, before the Express.js setup, we must guarantee that npm (Node Package Manager) is current. Npm acts as the default package manager for Node.js and is indispensable for administering Node.js packages. Regularly updating npm ensures we can access the latest functionalities and security updates.

To bring npm up to the most recent version, we execute the following command:

sudo npm install npm@latest -g

In this command, npm install npm@latest retrieves the most recent version of npm, and -g installs it globally, making it available across the entire system.

Step 2: Initiation of Node.js Project (Express.js) on Ubuntu

As we begin installing Express.js, our initial task is to set up a new Node.js project. This can be achieved by employing npm, which is automatically installed along with Node.js.

We navigate to the desired project directory, and for the sake of demonstration, we will create a new one:

mkdir expressjs-test && cd expressjs-test

Next, execute the following command to initiate a new Node.js project:

npm init -y

In this command, the -y flag is used to automatically fill in the default information in the setup configuration. The npm init command generates a package.json file in our project directory, which supplies the necessary metadata about our project.

Step 3: Install Express.js on Ubuntu 22.04 or 20.04

With a newly established Node.js project in place, the subsequent step is implementing Express.js. Once again, we will employ npm to install the Express.js package.

In our project directory, we execute the command below to install Express.js:

npm install express --save

In this command, npm install express obtains the Express.js package from the npm registry and installs it in our project. The --save flag ensures that the Express.js package is appended to the list of dependencies in the package.json file, highlighting its importance for our project.

While npm install express --save is generally the preferred method, you may occasionally find the npm install express --save-dev command useful in certain situations where you want Express.js as a development dependency.

Step 4: Verifying the Express.js Installation on Ubuntu

Following the Express.js installation, it’s always recommended to validate the operation. The aim is to ensure that Express.js is correctly installed and ready for use in your Node.js project.

The package.json file should now list Express.js in the dependencies section. You can verify this by viewing the package.json file with the following command:

cat package.json

The successful installation of Express.js should be reflected in the dependencies section of the package.json file.

Screenshot showing successfully installed Express.js dependencies on a test project in Ubuntu 22.04 or 20.04 Linux.Pin
Screenshot of a terminal window displaying the list of Express.js dependencies, confirming successful installation on a test project running Ubuntu 22.04 or 20.04 Linux.

Section 3: Create a Simple Express.js Application on Ubuntu 22.04 or 20.04

Finally, test our Node Express.js environment by creating a simple Express.js application. Create a new file called app.js in your project directory. You can use your preferred text editor for this purpose.

For instance, if you’re using nano, you’d use:

nano app.js

In this file, insert the following Express.js code:

const express = require('express');
const app = express();
const port = 3000;

app.get('/', (req, res) => {
  res.send('Hello World! from https://www.linuxcapable.com');
});

app.listen(port, () => {
  console.log(`Example app listening at http://localhost:${port}`);
});

This code sets up a basic Express.js application that listens on port 3000 and responds with “Hello World!” when you access the root URL (http://localhost:3000).

To start the Express.js application, use the following command:

node app.js

The command node app.js launches the Express.js application. You will see the message “Example app listening at http://localhost:3000” in your console if everything is configured correctly.

Navigate to http://localhost:3000 in your web browser, you should be greeted with the “Hello World!”. This indicates a successful setup of the Node Express.js environment.

Screenshot of a working Express.js browser app running on Ubuntu 22.04 or 20.04 Linux.Pin
Screenshot of a web browser displaying a working Express.js application hosted on a server running Ubuntu 22.04 or 20.04 Linux.

Conclusion

Reflecting on our journey, we have comprehensively examined installing and using Express.js with the most recent LTS version of Node.js sourced from NodeSource on an Ubuntu Linux distribution. The techniques and procedures we’ve covered equip you with the knowledge to create Express.js applications on a Node.js environment and effectively manage your project’s dependencies.

Given the prominence of Node.js and Express.js in web development, understanding their operation and application is of utmost importance. These tools are pivotal for creating efficient and scalable network applications. Utilizing the current LTS version of Node.js ensures stability and support. At the same time, Express.js, as a minimalist yet powerful web application framework, offers a vast array of functionalities necessary for web and mobile applications.

Leave a Comment


Your Mastodon Instance
Share to...