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

Enjoy!
You may also like the post below:
Thank you so much !!!
little correction to view the version of npm type npm -v
^^
Thanks for the tutorial. Now I have installed node in Ubuntu 18.04.
Great tutorial, very direct for the helping achieve question that comes up in search.
thank u so much!
good work appreciated. THanks for helping out.
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.
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
Great answer
Thank you
Fantastic!!! Great article.
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’
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
Thank you. You make this novice’s life so easy
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”
Thanks BTW
Thanks very much; very helpful!!
Thank you so much.
This works perfectly
Thanks .it helped me!
Thank you very much. very helpful.
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!
@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.
Thank you so much
Thanks a lot really a great help!!
Thank you very much!
Really Precise answer. Appreciate your work! Thanks!!!
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
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.
Should be node not nano
Thank you for the good tutorial.