Install Node JS and NPM Latest Packages on Ubuntu 18.04/18.10

56 views

Node.js is a JavaScript open source platform. Node.js makes development to be consistent and integrated, it’s built with Chrome’s V8 JavaScript engine as open source high performance from Google.

This short tutorial is going to show you how to quickly get started with Node.js and install the Latest latest version NPM package


Step 1: Update Ubuntu Package

To get the latest version of Ubuntu package use theapt by typing the command below:

sudo apt update&&upgrade

Step 2: Install Latest Node.js and NPM

Install Node.js from the official repositories

sudo apt install nodejs

Install Latest NPM

In most cases we also want to install npm as the Node.js package manager by typing:

sudo apt install npm

After installing, check version Node.js and NPM modules that already installed, now ready to use

nodejs -v
npm -v

Once you have established which version of Node.js you have installed from the Ubuntu repositories, you can decide whether or not you would like to work with different versions, package archives, or version managers

Installing Using a PPA

To get a more recent version of Node.js you can add the PPA (personal package archive) maintained by NodeSource. This will have more up-to-date versions of Node.js than the official Ubuntu repositories, and will allow you to choose between Node.js v6.x (supported until April of 2019), Node.js v8.x (the current LTS version, supported until December of 2019), Node.js v10.x (the second current LTS version, supported until April of 2021), and Node.js v11.x (the current release, supported until June 2019).

sudo apt install curl

First, install the PPA in order to get access to its contents. From your home directory, use curl to retrieve the installation script for your preferred version, making sure to replace 10.x with your preferred version string (if different):

cd ~
curl -sL https://deb.nodesource.com/setup_10.x -o nodesource_setup.sh

You can inspect the contents of this script with nano (or your preferred text editor):

sudo nano nodesource_setup.sh

Run the script under sudo:

sudo bash nodesource_setup.sh

The PPA will be added to your configuration and your local package cache will be updated automatically. After running the setup script from Nodesource, you can install the Node.js package in the same way you did above:

sudo apt install nodejs

To check which version of Node.js you have installed after these initial steps, type:

nodejs -v

Step 3: Testing Node.js

First, create a directory for your project in your non-root user’s home directory. We will call ours test_nodejs, but you should feel free to replace this with something else:

mkdir test_nodejs

Navigate to test_nodejs directory:

cd test_nodejs

To test whether the web server is properly installed, run the commands below to create a test file with a name test_server.js

sudo nano test_server.js

Then copy and paste the content below and please attention on the line with the port number const port = 9000; you should change the port as you want

const hostname = '127.0.0.1';
const port = 9000;

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

Ctr/Cmd X and Y  to save the file and Exit and run the commands below to start the server

sudo node test_server.js

You should see an output that read server running at http://localhost:9000/ or if your running with IP http://YourIP:9000/ and open your browser you will see Hello World