
Introduction
Node.js is an open-source, cross-platform runtime environment designed for executing JavaScript code on the server side. By installing Node.js on Ubuntu, you gain access to its powerful capabilities, including the Node Package Manager (npm), the official registry for software packages. Widely used for developing both front-end and back-end applications, Node.js is supported by a large developer community and extensive documentation, making it an essential tool for modern developers.
Express.js is a minimal and flexible Node.js web application framework. It's the most popular framework for Node.js and provides a wide range of web and mobile application development features. There are several other frameworks based on Node.js. It has a large community with many libraries that solve most of the problems encountered during application development.
This article explains how to install Node.js and Express.js on Ubuntu 20.04. You will have a simple Express application running at the end of this guide.
Prerequisites
- Deploy a fully updated Vultr Ubuntu 20.04 Server.
- Create a non-root user with sudo access.
1. Install Node.js on Ubuntu
To install Node.js, you can either install a more stable version of Node.js from the official Ubuntu 20.04 repositories, install the latest versions using Personal Package Archive (PPA) from NodeSource, or install using Node Version Manager (nvm). This tool allows you to manage multiple versions of Node.js within the same server. The stable version is dependent on the version of the operating system you are using. Conversely, the PPA versions let you choose a range of the latest versions available: the maintenance LTS version, the active LTS version, and the current release. Choose your preferred installation method and continue.
Option 1: Install Stable Version
Install the Node.js stable version.
$ sudo apt install nodejs -yInstall npm.
$ sudo apt install npm -yVerify the installed Node.js version.
$ nodejs --versionVerify the installed npm version.
$ npm --version
Option 2: Install via PPA Version
The currently available versions of Node.js from NodeSource are v12.x, v14.x, and v16.x. To find more about the versions available, please visit the official download page. This article illustrates the installation of the latest version v16.x.
Download and install the PPA to add the NodeSource repository signing keys, Node.js, and npm binaries. Then, change
16.xwith your preferred version.$ curl -sL https://deb.nodesource.com/setup_16.x | sudo -E bash -Install Node.js 16.x, which also installs npm.
$ sudo apt install nodejsVerify the installed Node.js version.
$ node --versionVerify the installed npm version.
$ npm --version
Option 3: Install via Node Version Manager
Download and install the nvm script. To find the latest version of the installation script, please visit the nvm official GitHub page.
$ curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.0/install.sh | bashVerify the installed nvm version.
$ nvm -vList the available versions of Node.js.
$ nvm ls-remoteInstall the latest available version of Node.js.
$ nvm install nodeInstall version
v14.3.0.$ nvm install 14.3.0List the installed versions of Node.js in the system.
$ nvm lsChange to a different version.
$ nvm use 14.3.0
2. Install Express.js
Install Express globally in case you need it in other projects.
$ npm install -g expressCreate your project directory. Change
myprojectto your preferred project name.$ mkdir myprojectNavigate into the project root directory.
$ cd myprojectRun the
npm initcommand to create apackage.jsonfile for your application.$ npm init -yInstall Express.js in the working directory and save it in the dependencies list.
$ npm install expressCreate an
app.jsfile.$ sudo nano app.jsAdd the following Hello World code to the file.
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}`) })Save and close the file.
Open firewall port 3000.
$ sudo ufw allow 3000/tcp
Run the application.
$ node app.js
3. Access Express.js
By default, the Express.js application starts on HTTP port 3000. Open a browser and visit http://Server-IP:3000. For example:
http://192.0.2.10:3000Conclusion
You have installed Node.js and Express.js on your Ubuntu 20.04 server. Continue with the installation process by completing the required steps. You can then change the code to suit your needs.
More Information
To learn more about the tools in this article, visit: