NodeJS on Ubuntu: Installation and First Steps

Every time we read something about web development, the subject of NodeJS always comes up. But this is not bad, because it is quite an important technology that is the basis of many current applications. So if you want to develop web applications you probably need to install NodeJS on Ubuntu or another system.

NodeJS is a server-side implementation of javascript. It is event-driven and asynchronous so it is often used as a partial replacement for PHP.

NodeJS is cross-platform so installing it on Ubuntu is not difficult. However, it is always useful to know.

Install NodeJS on Ubuntu

Ubuntu has one of the largest package repositories in all of Linux and also includes NodeJS. So to install it, just run the following command in a terminal:

sudo apt install nodejs

Install NodeJS using an external repository

This method is quite simple but it has a detail that can become a problem and that is that the latest stable version will not be installed.

So a more flexible method that will help us to have the latest stable version is to use the NodeJS PPA for Ubuntu. In this case, I will use the PPA of the 14.x branch which is the latest one.

Add the repository in question:

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

And then, proceed to install NodeJS:

sudo apt install nodejs
Installing NodeJS on Ubuntu
Installing NodeJS on Ubuntu

If you already have it installed and want to upgrade, just update the system:

sudo apt upgrade

This method works when a new development branch of NodeJS comes out. That is, you add the repository and update the system.

Finally, you can check the version of NodeJS.

node -v

Output:

v14.3.0

First steps with NodeJS on Ubuntu

To demonstrate that NodeJS is correctly installed and ready for action, we will create and run the classic “Hello world” example.

First, create a folder where the file with the NodeJS code will be located.

mkdir project

Access the folder and create a file called hello.js

cd project
nano hello.js

And paste this content into the file:

var http = require('http');

http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/html'});
res.end('Hello World. Thanks for visit this blog');
}).listen(8080);
Creating a new Hello world file with NodeJS
Creating a new Hello world file with NodeJS

The last line refers to the port from which the file will be executed. In this case, it is 8080. You can change it but you have to open the port in the firewall.

Save your changes by pressing CTRL + O and exit the editor with CTRL + X.

Once you have saved the file, you need to make it work. To do this, run the following command:

node hello.js

Remember that the execution of this command has to be done in the same folder where the source file is.

To view, the contents of the file, open your favorite web browser and open the following address http://localhost:8080 or if you have installed NodeJS on a server http://pc-ip:8080

NodeJS running on Ubuntu
NodeJS running on Ubuntu

So as you can see, NodeJS is running smoothly. So, it is ready for work.

Go back to the terminal and to finish running the application, press the CTRL + C keys.

Conclusion

NodeJS is a vital technology in many websites today. This is why new developers are adopting it more and more and are noticing its benefits. Thanks to this post you have learned to install it in a popular system like Ubuntu 20.04

For more information, you can visit the NodeJS website.