How to Install Node.js On Ubuntu 20.04

How to Install Node.js On Ubuntu 20.04

 

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 package 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.
 
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 Ubuntu 20.04 server.
  • Create a non-root user with Sudo access.

1   Install Node.js   

To install Node.js, you can try multiple methods:
  • 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, the PPA versions let you choose a range of the latest versions: the maintenance LTS version, the active LTS version, and the current release.
  • Install a version using Node Version Manager (NVM). This tool allows you to manage multiple versions of Node.js within the same server. The stable version varies depending on the version of the operating system you are using.
Choose your preferred installation method and continue.

1.1  Install a 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.
$ node -v
Verify the installed npm version.
$ npm -v

1.2  Install via PPA

The currently available versions of Node.js from NodeSource are v12.x, v14.x, and v16.x. To find out more about the available versions, 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 -v
Verify the installed npm version.
$ npm -v

1.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.1/install.sh | bash
Verify the installed nvm version.
$ nvm -v
List the available versions of Node.js.
$ nvm ls-remote | more
Install the latest available version of Node.js.
$ nvm install node
Install version v16.15.0.
$ nvm install 16.15.0
List the installed versions of Node.js in the system.
$ nvm ls
Change to a different version.
$ nvm use 18.2.0

2   Install Express.js

Install Express globally in case you need it for other projects.
$ npm install -g express
Create your project directory. Replace myproject with your preferred project name.
$ mkdir myproject
Navigate into the project's 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. 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. You will see the following output:

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.
 
The tutorial is tested on VPS Mart, so it works fine on Ubuntu VPS Hosting.

Add Feedback