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:

29 Comments

  1. Thank you so much !!!
    little correction to view the version of npm type npm -v
    ^^

  2. Fernando López Moreno

    Thanks for the tutorial. Now I have installed node in Ubuntu 18.04.

  3. Auckland Junior Dev

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

  4. thank u so much!

  5. good work appreciated. THanks for helping out.

  6. 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

      1. Great answer

  7. Fantastic!!! Great article.

  8. 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’

  9. 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

  10. Thank you. You make this novice’s life so easy

  11. 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”

  12. Thanks BTW

  13. Thanks very much; very helpful!!

  14. Thank you so much.
    This works perfectly

  15. Thanks .it helped me!

  16. Thank you very much. very helpful.

  17. 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.

  18. Thank you so much

  19. Thanks a lot really a great help!!

  20. Thank you very much!

  21. Sandeep Ganesana

    Really Precise answer. Appreciate your work! Thanks!!!

  22. 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

  23. 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.

    1. Should be node not nano

  24. Thank you for the good tutorial.

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.