Install Node.js on Ubuntu Linux 18.04

In this article, I will show you how I install Node.js latest version (11.x) and NPM on my

Ubuntu 18.14 virtual machine hosted on Microsoft Azure.

Get Started

To get started, I will start by installing the main prerequisite which is curl using the command below.

sudo apt install curl

Once curl is installed, I will use it to download the latest version from nodesource.

curl -sL https://deb.nodesource.com/setup_11.x | sudo -E bash -

Now, I can start with node.js installation using the command below

sudo apt-get install -y nodejs

Check Version

Once completed, I will run the command below to check that I’ve installed the correct version

node -v

I will go ahead and create a test Node.js hello world application using the built-in https server module.

The first step will be creating a new .JS file.

If you look closely, you will see that I am using PowerShell Core to create the file.

Below is the “Hello World” application code

const http = require('http');

const hostname = '127.0.0.1';
const port = 3000;

const server = http.createServer((req, res) => {
res.statusCode = 200;
res.setHeader('Content-Type', 'text/plain');
res.end('Hello World\n');
});

server.listen(port, hostname, () => {
console.log(`Server running at http://${hostname}:${port}/`);
});

All I need to do now is save the file and run it.


Posted

in

,

by