How To Ubuntu

Install Node.JS And NPM On Ubuntu 16.04/18.04

install Nodejs On Ubuntu
Written by itsubuntu

Install: Install NodeJs On Ubuntu

Node.js is an open source cross-platform JavaScript runtime environment built on Chrome’s V8 JavaScript engine. It allows server-side execution of JavaScript code. Due to the node, You can run any JavaScript code on your machine as a standalone application without need of any web browser.

In this brief tutorial, We are going to show you the method to install the latest Node.js and NPM packages on Ubuntu 16.04 / 18.04 LTS.

Tutorial To Install NodeJS On Ubuntu

Install Node.JS And NPM On Ubuntu 16.04/18.04

At first, We need to add the PPA to install latest Node.js and NPM. You might know that node is already available in the Ubuntu default repositories but it might not be updated version. So, if you want to get the latest version, you’ll have to add its official PPA in your Ubuntu.

Tutorial To Add Node.js PPA

Run the following command to instll curl at first.

sudo apt install curl

Right now, There are two Node.JS repositories available. One of them contain the latest Node.js packages and the other repo contain the LTS or (Long Term Support) packages.

If you want to install the latest release, Then add the following PPA.

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

If you want to install the LTS release, add this PPA.

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

Note: If you add the both repositories, the latest version of Node.js will be installed.

Tutorial To Install Node.js And NPM On Ubuntu

To install, run the commands below

sudo apt install nodejs

sudo apt install npm

You will have both installed in your Ubuntu operating system. You can verify your installtion by running the following command.

node -v
npm -v

Create A Test File On Node

Run the commands below to create a test file called http_server.js.

cd ~/
nano http_server.js

Copy and paste the content below into the test file and save it.

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}/`);
});

 

Now, Start the node server by running the following command.

node http_server.js

You should see something like below.

Server running at http://127.0.0.1:3000/

About the author

itsubuntu