Debian

Install NodeJS on Debian and Ubuntu

With the introduction of JavaScript, the development field took a new turn. It has opened various opportunities for developers to create innovative and dynamic web-based applications. You will be amazed to look at its several features and functionalities. Moreover, with various libraries and plugins, developers can make it extensible. But with the increase in tools and libraries, we need a mature solution to handle them.

What is Node.JS?

Node.js was launched as an open-source, cross-platform JavaScript run-time environment. You can run this JavaScript code as a standalone application on any device or machine, and you do not have to worry about relying on any web browser. Developers use Node.js for creating back-end, server-side applications. It is widely used and popular due to its usage as a full-stack and front-end solution for developers who want to create web-based applications.

Node.js comes with in-built support for managing the package with the help of npm (Node Package Manager). NPM is one of the important package libraries offering various features. You can use its vast downloadable libraries, and it is available free of cost. Day by day, these libraries are getting bigger, making Node.js stronger and better.

You can install Node.js on Debian/Ubuntu and many other platforms. We highlight the simple steps to install Node.js on the Debian 20.04 system; read below and follow for error-free installation!

Advantages of Using Node.JS

Below are few advantages of using Node.js:

  • It is based on JavaScript, making it easy to learn and understand if you are a JavaScript developer.
  • It comes with a npm package, providing a wide variety of features.
  • It has wide community support, actively managing Node.js.
  • It helps in streaming files of big size.
  • It is based on asynchronous event-driven IO, which helps in managing concurrent requests.

Disadvantages of using Node.js

Below are a few disadvantages of using Node.js:

  • You will not be able to scale using Node.js. Using Node.js, working with one CPU is not enough. Also, you will not be able to avail benefits of several cores present in server-class hardware.
  • If you are using callback features, you may enter a loop of callbacks.
  • Node.js is not well-suited for CPU-intensive work.

Installing NodeJS on Ubuntu 20.04

Here, we will pinpoint the three different ways for installing the Node.js on Ubuntu 20.04 server.

You can install the Node.js package with the help of apt from Ubuntu’s default software repository.

You can install a specific version of the Node.js package with the help of an apt using an alternate PPA software repository.

You can install the Node.js package via Node Version Manager and managing various versions of Node.js.

Prerequisites

Below are the basic prerequisites for installing the node.js package on Ubuntu 20.04 server.

Set up the Ubuntu 20.04 server on your system.

Users should have a non-root user account, but especially with the admin rights for running various commands.

Method 1: Using Apt from the Default Repository

The default repository of Ubuntu 20.04 comes with a Node.js version, providing a consistent experience for all systems. We have the repository version as 10.19. If you want to get this version, you can use the apt package manager. Then, refresh your local package index first by running the below command.

$ sudo apt update

Now, start installing Node.js by running the below command in the terminal.

$ sudo apt install nodejs

After running the command mentioned above successfully, run the below command for checking the version of the installed Node.js.

$ nodejs -v

Output-
V10.19.0

More packages other than the default one can be installed accordingly. Apart from the default repositories, you will require npm (Node.js package manager). You can run this command line command to start the installation of the npm package with apt.

$ sudo apt install npm

After running the above commands, you have successfully installed Node.js and npm using apt.

Method 2: Using Apt Using a NodeSource PPA

To install the various versions of Node.js, use the NodeSource PPA (personal package archive). You will get more versions available for Node.js, as you can find in the Ubuntu repository. With time, you will get a new version added to the list.

Before accessing the packages, install PPA. Use “curl” from the home directory to get the script for installing your choice of Node.js version. We are using the 14.x version in the below command, and you can replace it with whatever version you want to install.

$ cd ~
$ curl -sL https://deb.nodesource.com/setup_14.x -o nodesource_setup.sh

Use a nano text editor to check the downloaded script content. Then, simply run the below-mentioned command from the terminal to do so.

$ nano nodesource_setup.sh

Next, exit the editor after checking and verifying the script’s content and run the below sudo command to run the script.

$ sudo bash nodesource_setup.sh

After running the commands mentioned above, you will automatically add the PPA to your configuration and update the local package cache.

Now, run the below command for installing the Node.js package.

$ sudo apt install nodejs

After the successful installation of npm, you will be able to check the installed version of Node.js using the below-mentioned command.

$ node -v

The node js package from the NodeSource comes with both the node binary and npm. So, do not install the npm package explicitly.

Method 3: Using Node Version Manager

Using nvm (Node version manager), you can install and maintain several independent Node.js versions and their node packages altogether. Install the NVM using the curl command from the README file available on the main page. In addition, avail the installation script of the latest version.

Before running the command, verify its content. For example, run the below command to check the content.

$ curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.35.3/install.sh

Once you confirm the script, you can run the below command with a bash at the end to download and execute the script.

$ curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.35.3/install.sh | bash

The above command will install the nvm script to your user account. For using the script, add the source to your .bashrc file, as shown below.

$ source ~/.bashrc

To check the NVM for available Node.js versions, you can run the below command.

$ nvm list-remote

For example, if you want to install Node.js from the available versions, run the below command.

$ nvm install v13.6.0

After successfully installing the Node.js version, run the below command for checking the installed version.

$ node -v

Conclusion

We have mentioned and discussed three different ways for installing Node.js and npm on your Ubuntu 20.04 server. Of course, you can go with any of the mentioned methods depending on your requirements and preferences. However, installing the packaged version using the NodeSource repository is easier. Apart from this, you will get enhanced flexibility by using the nvm method for adding and removing different Node.js versions on a per-user basis.

Now that you’ve installed Node.js on your Ubuntu 20.04 system, go ahead and develop your application!

About the author

Simran Kaur

Simran works as a technical writer. The graduate in MS Computer Science from the well known CS hub, aka Silicon Valley, is also an editor of the website. She enjoys writing about any tech topic, including programming, algorithms, cloud, data science, and AI. Travelling, sketching, and gardening are the hobbies that interest her.