How To Install Latest NodeJS And NPM On Ubuntu

This comprehensive Linux guide expects that you run the following commands as root user but if you decide to run the commands as a different user then ensure that the user has sudo access and that you precede each of the privileged commands with sudo

This brief tutorial is going to show students and new users how to easily install the latest Node.js and NPM packages on Ubuntu. Although Node.js comes in Ubuntu default repositories, if you want to get the latest version, you’ll have to add its official PPA.

This tutorial will work for all Ubuntu LTS releases 14.04 LTS, 16.04LTS and 18.04 LTS and also 14.10, 15.10, 16.10, 17.10, 18.10 and 19.10 Ubuntu releases.

Add Node.js PPA

Before installing the latest version of Node.js, you must add its PPA to Ubuntu.

Latest Current Release

Before installing the latest version of Node.js, you must add its PPA to Ubuntu. At this time Node.js 12.6.0 is the current latest Node.js release available.

root@codesposts:~$ apt-get install curl python-software-properties
root@codesposts:~$ curl -sL https://deb.nodesource.com/setup_12.x | sudo -E bash -

Latest LTS Release

You have to add the PPA for the latest LTS release available. At this time, Node.js 10.16.0 is the latest LTS release.

To Add PPA for latest LTS release, run this command:

root@codesposts:~$ apt-get install curl python-software-properties
root@codesposts:~$ curl -sL https://deb.nodesource.com/setup_10.x | sudo -E bash -

Install Node.js

To install the latest version of Node.js on your system, run this command:

root@codesposts:~$ apt-get install nodejs

Verifying The Installation

After the installation of latest Node.js, you can verify the installation by the following command:

root@codesposts:~$ node -v 
v12.6.0

You can also verify the installation of the latest npm by running the following command:

root@codesposts:~$ npm -v
6.9.1

Testing The Web Server

After the installation of the Node.js and npm on your system. You can run the following commands to test whether the web server is installed correctly or not.

root@codesposts:~$ nano /tmp/http_server.js

After this, copy the following code to the file http_server.js

/tmp/http_server.js
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 Worldn');
});

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

Now, save and exit the file

After this, run the following command:

root@codesposts:~$ node http_server.js

debugger listening on port 5858
Server running at http://127.0.0.1:3000/

The post How To Install Latest NodeJS And NPM On Ubuntu appeared first on CODESPOSTS.COM.

Go to Source
Author: staff

Ubuntu Server Admin

Recent Posts

🚀 Deploy Elastic Stack on Ubuntu VPS (5 Minute Quick-Start Guide)

Here’s the guide to deploy Elastic Stack on Ubuntu VPS, with secure access, HTTPS proxying,…

2 days ago

🚀 Deploy Nagios on Ubuntu VPS

This guide walks through deploying Nagios Core on an Ubuntu VPS, from system prep to…

2 days ago

Shoryuken Has a New Maintainer, and v7.0.0 Is Almost There

After a decade under Pablo Cantero's stewardship, Shoryuken has a new maintainer - me. I'm…

5 days ago

A better way to provision NVIDIA BlueField DPUs at scale with MAAS

MAAS 3.7 has been officially released and it includes a bunch of cool new features.…

2 weeks ago

Ruby Floats: When 2.6x Faster Is Actually Slower (and Then Faster Again)

Update: This article originally concluded that Eisel-Lemire wasn't worth it for Ruby. I was wrong.…

2 weeks ago

MicroCeph: why it’s the superior MinIO alternative (and how to use it)

Recently, the team at MinIO moved the open source project into maintenance mode and will…

2 weeks ago