How to Install Express.js and Write a Hello World App on Ubuntu

In this tutorial, we’ll show you how to set up your Ubuntu with Node.js and Express.js and how to write your first Hello World Express.js app on Ubuntu.

Express is a minimal and flexible Node.js web application framework that provides a robust set of features for web and mobile applications.

Before we begin

For this tutorial, you’ll only need access to an Ubuntu system. We wrote this tutorial for an Ubuntu 20.04 server, but the instructions will work on the desktop version too, or any other Ubuntu-based distro. You can get a cheap Ubuntu server at Vultr.

You’ll need the root user for this tutorial, or run sudo where necessary.

Step 1: Update Ubuntu and install nano, curl

Your first step should always be to update your Ubuntu system. Run the following commands:

apt-get update && apt-get upgrade -y

For this tutorial, we’ll be using nano and curl, so if you don’t have them installed already, run the following command:

apt-get install nano curl -y

Step 2: Install Node.js

Since Express is a Node.js framework, you’ll need to install Node.js first.

To install the current version (v15 as of writing) of Node.js, run the following nodesource commands:

curl -fsSL https://deb.nodesource.com/setup_current.x | sudo -E bash -
apt-get install nodejs -y

Or if you want to install the latest LTS release (v14 as of writing) of Node.js, run the following commands:

curl -fsSL https://deb.nodesource.com/setup_lts.x | sudo -E bash -
apt-get install nodejs -y

To check what version of Node.js you have installed on your system (if any), run

node -v

Which should return the version of Node.js you’re currently using.

Step 3: Create a directory and the needed js files for your Express app

The next step is to first create a directory and navigate to it:

mkdir linuxstanjs && cd linuxstanjs

Next, use the following command to create a package.json file:

npm init

The command will give you a few prompts which are all self-explanatory. You can leave everything to the default values and just hit enter for everything.

Step 4: Install Express

Now for the main part of our tutorial. While you’re still in the directory you previously created, run the following command to install Express.js on Ubuntu:

npm install express --save

And that’s it! Express is installed. Now we’ll write our first Hello World app.

Step 5: Create a Hello World Express app

To create the simples Express Hello World app, create a file in your new directory (linuxstanjs in our case):

nano expressapp.js

You can name it whatever you want to.

And paste the following code:

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

app.get('/', (req, res) => {
  res.send('Hello Linux Stans!')
})

app.listen(port, () => {
  console.log(`Your first Express app is successfully running! You can view the output of this app at http://localhost:${port}`)
})

This app will output a simple “Hello Linux Stans!” message. To run the app, use the following command:

node expressapp.js

Which should start the app. You can now view the output in a browser at http://localhost:3000 or http://your-server-ip:3000.

If you’re using a firewall, you can allow the port:

ufw allow 3000

And that’s it! That’s your first Express Hello World app. Subscribe for more Express.js beginner-friendly tutorials like this one.

Leave a comment

Your email address will not be published. Required fields are marked *