Linux

How to install Node.js on Ubuntu

feature-image-nodejs

Node.js is a server-side programming runtime based on JavaScript. It enables developers to design scalable backend functionality using JavaScript, a language that many people are already familiar with through web development in the browser. Node takes JavaScript out of the browser. As a result, developers can utilize JavaScript to perform anything that other popular server-side languages like Ruby, PHP, C#, and Python can do. So, at its most basic level, NodeJS allows you to run JavaScript code on a machine other than a web browser, such as your local computer or a server. NodeJS provides certain extra features that browser-based JavaScript doesn’t have, such as the ability to read and write local files, create HTTP connections, and listen for network requests. You will be guided on how to install Node.js on any Ubuntu server in this tutorial as the commands will work on most but for the purpose of the demos, we used version 20. Other guides can be found here: Squid Proxy: How to setup and configure a Proxy Server/, How to install MariaDB on Ubuntu/, How to Install Terraform on Linux/, How to install Apache Tomcat on Ubuntu

Event-driven

Because of its single-threaded nature, Node.js is best suited for non-blocking, event-driven servers. It was created with real-time, push-based architectures in mind and is utilized for standard websites and back-end API applications. If we look through the official website of NodeJS nodejs.org for a proper definition, we will see that NodeJS is a JavaScript runtime that is event-driven and asynchronous at the same time. In this case, asynchronous means that you don’t try to forecast the exact order in which each line will execute when you write your code. Instead, you write your code as a series of smaller functions that are invoked when certain events occur, such as a network request (event-driven).

Consider the following scenario: you’re writing a program that must read text from a file, print that text to the console, query a database for a list of users, and filter those users based on their age.

http.createServer(function (req, res) {
  res.writeHead(200, {'Content-Type': 'text/html'});
  res.end('Hello World!');
}).listen(8080);

Essentially, this code creates a server and instructs it to “perform this callback function whenever we receive a network request.” The text ‘Hello World’ is returned by this function. That text would appear on your screen if you opened a browser and navigated to the correct location and port.

Importance of Node.js

  1. Calling other services using Node.js is simple. The majority of apps require seamless communication with the database and platform API.
  2. Node.js increases efficiency and can manage a large number of queries. It’s incredibly handy, practical, and speedy on the client side because it requires consumers to make fewer clicks and have everything loaded directly.
  3. Node.js allows developers to use JavaScript on both the server and the browser.
  4. With its lightweight and diverse tools, Node.js allows developers to speed up the development of online apps.
  5. Back-end frameworks run on Nodejs, which is an environment. Express.js is one of such frameworks (or just Express) is a popular HTTP server , while Socket.IO is a popular WebSocket server that also runs on NodeJS.
  6.  As a stand-alone platform, Node.js provides numerous advantages to the tech stack; for example, it includes the JSON module library. Node.js has become a major participant in several IT ecosystems, with over 368,985,988 downloads, or an average of 1,010,921 every day.

Prerequisites

You should have a non-root user account with sudo rights set up on your system before you begin.

 Possibility No. 1: Installing with Apt from the Default Repositories

In Ubuntu 20.04’s default repositories, there is a version of Node.js that can be used to give a consistent experience across various platforms. The version in the repositories at the time of writing is 10.19. This will not be the most recent version, but it should be dependable.

The apt package manager can be used to obtain this version. To begin, refresh your local package index by typing:

$ sudo apt update 

Install NodeJS:

sudo apt install nodejs
install

to check the version installed

node -v
node-v

You should also install the Node.js package manager, npm. You can do this by using apt to install the npm package:

sudo apt install npm
npm-v

Using apt and the default Ubuntu software repositories, we were able to successfully install Node.js and npm.

 Possibility No. 2: Installing Node Using the Node Version Manager

curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.38.0/install.sh
FIRST
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.38.0/install.sh | bash
DOWNLOAD

The nvm script will now be installed in your user account. You must first source your.bashrc file in order to utilize it:

source ~/.bashrc

source the ~/.bash profile file with the command:

source ~/.bash profile

We can now ask NVM which Node versions are available:

nvm list-remote
versions

It’s a lengthy list! By typing any of the release versions you see, you can install a version of Node. For example, to obtain version 14.10.0, type:

nvm install v14.10.0
npms

you can see the various versions you have installed with the command below:

nvm list
npmlist

On the first line, the currently active version (-> v14.10.0) is shown, followed by various named aliases and the versions to which those aliases point.

Summary

Node.js is outstanding and efficient at managing several requests at once. It takes others longer and costs less than other IT giants to handle your app’s traffic. We have installed Nodejs on Ubuntu using two different methods, first, from the Default Repositories and lastly using the Node Version Manager.

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x