Dayalan Saravanan


Install Node.js on Debian 10 GNU/Linux

Tuesday, August 31, 2021

Node.js is a JavaScript platform for general-purpose programming that allows users to build asynchronous network applications quickly. By leveraging JavaScript on both the front and backend, Node.js can make web application development more consistent and integrated.

Debian contains a version of Node.js in its default repositories. To get Node.js from the default Debian software repository, you can use the apt package manager. First, refresh your local package index:

 sudo apt-get update 

Then install the Node.js package, and npm the Node Package Manager:

 sudo apt-get install nodejs npm 

To verify that the install was successful, run the node command with the -v flag to get the version:

 node -v 

Output

Installing Using a PPA

To work with a more recent version of Node.js, you can install from a PPA (personal package archive) maintained by NodeSource. This is an alternate repository that still works with apt , and will have more up-to-date versions of Node.js than the official Debian repositories. NodeSource has PPAs available for Node versions from 0.10 through to 12.

Let's install the PPA now. This will add the repository to our package list and allow us to install the new packages using apt.

From your home directory, use curl to retrieve the installation script for your preferred Node.js version, making sure to replace 12.x with your preferred version string (if different):

 curl -sL https://deb.nodesource.com/setup_12.x -o nodesource_setup.sh 

You can inspect the contents of this script with vim or your preferred text editor:

 vim nodesource_setup.sh 

Run the script using sudo:

 sudo bash nodesource_setup.sh 

The PPA will be added to your configuration and your local package cache will be updated automatically. Now you can install the nodejs package in the same way you did in the previous step:

 sudo apt-get install nodejs 

We don't need to install a separate package for npm in this case, as it is included in the nodejs package.

Verify the installation by running node with the -v version option:

 node -v 

Output

npm uses a configuration file in your home directory to keep track of updates. It will be created the first time you run npm. Execute this command to verify that npm is installed and to create the configuration file:

 npm -v 

Output

In order for some npm packages to work (those that require compiling code from source, for example), you will need to install the build-essential package:

 sudo apt-get install build-essential 

You now have the necessary tools to work with npm packages that require compiling code from source.