In this article we will be going over the general process of how to install Node.js and NPM on an Ubuntu 18.04 machine.
Node.js, which is a popularly known as “Node” is an open source cross-platform JavaScript run-time environment which is built on Google Chrome’s V8 engine that allows server-side execution of JavaScript code.
This simply means that you can run JavaScript code on your machine as a standalone application, free of any web browser which has traditionally been the only place that JavaScript code could run.
Node.js does not have a rendering engine for CSS nor does it have a DOM parser but it does have things you need to run a server like an HTTP library and a filesystem API.
It is mainly used to build back-end server-side applications, but it is also very popular as a full-stack and front-end solution.
NPM, which is short for Node Package Manager, is the default package manager for Node.js and it is currently the world’s largest software registry.
If you need Node.js only for deploying Node.js applications then the simplest option is to install the Node.js packages using apt from the Default Ubuntu repository or from the NodeSource repository in case you need the latest Node.js and npm versions.
If you are using Node.js for development purposes then your best option is to install Node.js using the NVM script.
Although this tutorial is written for Ubuntu the same instructions apply for any Ubuntu-based distribution, including Linux Mint and Elementary OS using their respective package managers.
Step 1. Add the PPA Packages
Node.js package is available in all the LTS and current releases of Ubuntu, however, in some minimal installations, you might not have it installed. As such, we want to install the package’s PPA. You can use the URL below to specify the version of NodeJS you want to install if the PPA package is not already installed on your machine
sudo apt-get install curl python-software-properties curl -sL https://deb.nodesource.com/setup_10.x | sudo -E bash - sudo apt-get update sudo apt-get upgrade
Step 2 – Install Node.js on Ubuntu
Now that you have successfully installed the Node.js PPA package, you can now execute the below command install Node on and Ubuntu using apt-get. This will also install NPM with Node.js. This command also installs all other required packages on your system.
sudo apt-get install nodejs
Step 3 – Verify Installation
If the last command was run successfully, it means you have been able to install NPM and Node.js on your computer. At this point, we will want to check and make sure that the packages have been successfully installed. In the commands below we are going to run the node.js
and npm
on the command line to make sure that they are accessible to everyone on the machine. We will be using the command that checks the version of Node or NPM that you are running.
Run the command below to check the version of Node.js you are running.
sudo nodejs --version
After this runs, you should see the version of Node.js installed on the computer. For the demo used for this tutorial we are running node 10.0, so you would see the version like what we have below.
v10.0.0
Just like we did for Node.js we are going to do the same for NPM. We just run the npm
command in the console and we can see the version.
npm -v 6.0.0
At this point, we have successfully install Node.js and NPM on our Ubuntu machine. As the last step, we want to test some of the good features of node.js by running a test server. We will start by creating a test file called test-server.js
in that test file enter the code snippet below:
//initialize the http server for Node.js const http = require('http'); //create a server to accept a request and return a response http.createServer(function (req, res) { //sets the response headers to plain text or html res.writeHead(200, {'Content-Type': 'text/plain'}); //send a message to the client res.end('Hello World from Dotlayer\n'); //finally specify the port to listen for connections on }).listen(3000, "127.0.0.1"); // add a debug message to know when the code is executing console.log('Server running at http://127.0.0.1:3000/');
Once you have successfully added this code to the test file, you can run the command below to start your server.
node test-server.js
You can now point your web browser, either Chrome/Firefox to http://127.0.0.1
and you should see the message on your screen.
In conclusion
In this post, we have shown a basic example of how we can install Node.js and NPM on Ubuntu as well as running a quick web-server using Node.js. As we mentioned before, Node.js can be used for writing complex server-side and serverless applications. If you have any questions or feedback, feel free to comment below.