NodeJS and npm, installation in Ubuntu 20.04 | 18.04

about nodejs

In the next article we are going to take a look at how we can install Node.js and npm on Ubuntu 20.04 | 18.04. This is an open source, cross-platform runtime environment for JavaScript already discussed in this page and that uses an event-oriented I / O operations model, which makes it lightweight and efficient.

NodeJS is a JavaScript-based open source server framework which is primarily used for building backend server applications with JavaScript runtime. It is based on Chrome's V8 JavaScript engine. Npm is the default package manager for NodeJS.

It is mainly used for asynchronous programming and is a very light frame, which makes it faster than others. It is also compatible with most popular operating systems. Different types of applications, such as web applications, command line applications, etc., can be developed with this framework using Ubuntu.

Add NodeJS repository from NodeSource

NodeSource is the company's own enterprise-level node repository that maintains and contains the latest versions of NodeJS. From NodeSource we will be able to install a specific version of NodeJS.

To install NodeJS from NodeSource, simply run one of the following commands to add the specific version that interests us. To do it we will have to have curl installed. If you don't have this tool yet, you can install it with the command:

sudo apt install curl

Now for install the latest version (version 14), in a terminal (Ctrl + Alt + T) we will add this PPA:

add repo nodejs 14

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

For, install version 12, you just have to run the command:

add repo nodeJS 12

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

For, install LTS version (version 10), the PPA to be used will be:

add repo nodejs 10

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

After adding any of these PPAs, we will be able to install the latest version of NodeJS from the repository of our choice. If we add several repositories, the latest version of NodeJS will be installed and not the LTS.

Install NodeJS and npm

For the installation, in a terminal (Ctrl + Alt + T) we will have to execute the command:

install nodejs with apt

sudo apt install nodejs

After the installation is complete, the NodeJS and npm modules should be installed and ready to use. We can use the following commands to see the installed version number.

node --version

npm --version

The commands will list the installed version of node and npm:

installed nodejs and npm version

May see all installation instructions available from project GitHub page.

Install NodeJS and npm via Snap

Another way to installing NodeJS is through admin snap packages. This might be the easiest way to do it.

Snaps are easy to create and install software packages. These are packaged applications with all their dependencies to run on all popular Gnu / Linux distributions from a single build.

For, install the latest version (version 14) in a terminal (Ctrl + Alt + T) run:

NodeJs 14 snap installation

sudo snap install node --channel=14/stable --classic

We can install version 13 running the command:

nodeJs 13 snap installation

sudo snap install node --channel=13/stable --classic

For, install version 10, the command to use will be the following:

NodeJS 10 snap installation

sudo snap install node --channel=10/stable --classic

Testing the server

To test if the web server is installed correctly, let's create a test file called http_server.js in our home folder using our favorite editor:

cd ~/

vim http_server.js

Then we will copy and paste the following content in the file:

server test file

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('Prueba de Nodejs para Ubunlog');
});

server.listen(port, hostname, () => {
  console.log(`Servidor funcionando en http://${hostname}:${port}/`);
});

After this, we save the file. Now we execute it with the following command to start the server:

node http_server.js

We should see an output that looks like the following in the terminal:

Example console output

Now if we open our favorite browser and we go to the hostname of the server or the IP address followed by port 3000, we should see a sample page like the following:

test in firefox

http://localhost:3000

For, learn more about NodeJS, users can visit the project page.


The content of the article adheres to our principles of editorial ethics. To report an error click here!.

A comment, leave yours

Leave a Comment

Your email address will not be published.

*

*

  1. Responsible for the data: Miguel Ángel Gatón
  2. Purpose of the data: Control SPAM, comment management.
  3. Legitimation: Your consent
  4. Communication of the data: The data will not be communicated to third parties except by legal obligation.
  5. Data storage: Database hosted by Occentus Networks (EU)
  6. Rights: At any time you can limit, recover and delete your information.

  1.   Mateo said

    I really liked how well explained it is.
    Thank you!!