Install the Latest Node.js and NPM Packages on Ubuntu 16.04 | 18.04 LTS

Node.js is a JavaScript runtime built on Chrome’s V8 JavaScript engine…..  Chrome V8 engine is a Google’s open source high-performance JavaScript engine written in C++ and used in Google Chrome, the open source projects….

V8 engine works on Windows and Linux systems… and can be used to build fast and scalable applications…

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

For more about Node.js, please visit the project’s homepage

To get started with install Node.js and NPM, follow the steps below:

Step 1: Add Node.js PPA

Before installing the latest version of Node.js, you must add its PPA to Ubuntu… This repository is provided by the official package mainterner… To add the repository, run the commands below….

sudo apt install curl

There are two repositories you can install… one repository contains the latest Node.js packages and the other has the LTS or (Long Term Support) packages…. if you need the latest and greatest, then install the first repository…

On the other hand, if you need a more stable and tested Node.js packages, then install the LTS repository….

Then for the Latest release, add this PPA..

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

To install the LTS release, use this PPA

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

After that, you can now install latest version of Node.js from the particular repository you choose… If you add both repositories, the latest version of Node.js will be installed and not the LTS…

Step 2: Install Node.js and NPM

To install, run the commands below

sudo apt install nodejs

After installing, both Node.js and NPM modules should be installed and ready to use….

You can use the commands below to view the version number installed….

node -v
npm -v

To test whether the web server is properly installed, run the commands below to create a test file called http_server.js in your home folder…

cd ~/
nano http_server.js

Then copy and paste the content below into the file and save….

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

After that save the file and run the commands below to start the server….

node http_server.js

You should see an output that reads:

Server running at http://127.0.0.1:3000/

Now open your browser and browse to the server hostname or IP address followed by port 3000… and you should see a default page with Hello World

http://localhost:3000

Node.js ubuntu install

Enjoy!

You may also like the post below:

Install Habari CMS on Ubuntu 16.04 / 18.04 with Apache2, MariaDB and PHP 7.2

28 Replies to “Install the Latest Node.js and NPM Packages on Ubuntu 16.04 | 18.04 LTS

  1. Great tutorial, very direct for the helping achieve question that comes up in search.

  2. Thanks! Awesome write-up. This is probably a newbie question: what is the difference between “apt install” and “apt-get install”? I couldn’t do “sudo apt install curl” but “sudo apt-get install curl” worked ok.

    1. The former is a Ubuntu 18 inclusion whereas the latter should still work for both Ubuntu 18.04 and earlier versions i.e 16.04 LTS

  3. It was a great article, just adding something, when node is installed this is not installing by default npm then I had to install it
    ‘sudo apt install npm’

  4. You don’t need the “sudo” in “sudo nano http_server.js” and “sudo node http_server.js”.
    Just:
    nano http_server.js
    node http_server.js

    Also, with Ubuntu 18.04, you don’t need to “Step 1: Add Node.Js PPA”; just do this:
    sudo apt-get install nodejs npm

  5. adding to PPA sections didn’t worked, also installed node bt node was still not found as command, so executed “sudo apt install nodejs-legacy” & “sudo apt install npm” then it worked for both “node -v” & “npm v”

  6. Hi. I got stuck at the point ; copy and paste into file” the following instructions -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}/`);
    });
    What file, where and how?
    I am NOT used to go in to terminal! So I prefer to ask even questions that more advanced users may consider completely stupid!

    1. @Kjell When you do the “sudo nano http_server.js” it should have created a file and put you in a “Vim” like editor if you then copy what you need to put into the file, and hit CTRL+X to exit, it’ll ask you if you want to save it Hit Y and ENTER and it should take you back to the regular terminal.

  7. Thanks, great article.
    If you installing in a server to expose the port 3000, you must change your code from: const hostname = ‘127.0.0.1’;
    to: hostname = ‘0.0.0.0’;
    Cheers

  8. It says that “bash: nano: command not found” when I try to do the “nano http_server.js” and I’m not sure what I did wrong.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.