Knowledgebase

Installing Node.js and Express on Ubuntu 20.04 Print

  • 24

Introduction

Node.js is an open-source, cross-platform runtime environment for server-side JavaScript code execution. It uses Node Package Manager (npm) as its official software packages registry. It's used to develop general-purpose applications both on the front-end and back-end. With a large community of contributors and good documentation, it's a popular tool for many 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

1. Install Node.js

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

  1. Install the Node.js stable version.

     $ sudo apt install nodejs -y
  2. Install npm.

     $ sudo apt install npm -y
  3. Verify the installed Node.js version.

     $ nodejs --version
  4. Verify 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.

  1. Download and install the PPA to add the NodeSource repository signing keys, Node.js, and npm binaries. Then, change 16.x with your preferred version.

     $ curl -sL https://deb.nodesource.com/setup_16.x | sudo -E bash -
  2. Install Node.js 16.x, which also installs npm.

     $ sudo apt install nodejs
  3. Verify the installed Node.js version.

     $ node --version
  4. Verify the installed npm version.

     $ npm --version

Option 3: Install via Node Version Manager

  1. 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 | bash
  2. Verify the installed nvm version.

     $ nvm -v
  3. List the available versions of Node.js.

     $ nvm ls-remote
  4. Install the latest available version of Node.js.

     $ nvm install node
  5. Install version v14.3.0.

     $ nvm install 14.3.0
  6. List the installed versions of Node.js in the system.

     $ nvm ls
  7. Change to a different version.

     $ nvm use 14.3.0

2. Install Express.js

  1. Install Express globally in case you need it in other projects.

     $ npm install -g express
  2. Create your project directory. Change myproject to your preferred project name.

     $ mkdir myproject
  3. Navigate into the project root directory.

     $ cd myproject
  4. Run the npm init command to create a package.json file for your application.

     $ npm init -y
  5. Install Express.js in the working directory and save it in the dependencies list.

     $ npm install express
  6. Create an app.js file.

     $ sudo nano app.js
  7. Add 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}`)
     })
  8. Save and close the file.

  9. Open firewall port 3000.

    $ sudo ufw allow 3000/tcp

  10. 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:3000

Conclusion

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:

Introduction Node.js is an open-source, cross-platform runtime environment for server-side JavaScript code execution. It uses Node Package Manager (npm) as its official software packages registry. It's used to develop general-purpose applications both on the front-end and back-end. With a large community of contributors and good documentation, it's a popular tool for many 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 Rcs Ubuntu 20.04 Server. Create a non-root user with sudo access. 1. Install Node.js 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 -y Install npm. $ sudo apt install npm -y Verify the installed Node.js version. $ nodejs --version Verify 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.x with 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 nodejs Verify the installed Node.js version. $ node --version Verify 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 | bash Verify the installed nvm version. $ nvm -v List the available versions of Node.js. $ nvm ls-remote Install the latest available version of Node.js. $ nvm install node Install version v14.3.0. $ nvm install 14.3.0 List the installed versions of Node.js in the system. $ nvm ls Change 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 express Create your project directory. Change myproject to your preferred project name. $ mkdir myproject Navigate into the project root directory. $ cd myproject Run the npm init command to create a package.json file for your application. $ npm init -y Install Express.js in the working directory and save it in the dependencies list. $ npm install express Create an app.js file. $ sudo nano app.js Add 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:3000 Conclusion 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: Node.js documentation page. Express.js documentation page. NodeSource documentation page. Node Version Manager documentation page. Node Package Manager documentation page.

Was this answer helpful?
Back

Powered by WHMCompleteSolution