How To Install Node.Js on Debian 11

NodeJs is a JavaScript framework that allows users to easily develop autonomous network applications for general-purpose programming. NodeJs is free to make web application development more uniform and integrated through the use of JavaScript on both the front and back ends.

It is available for all Operating Systems; in this article, you will learn how to install NodeJs on your Debian system (Linux OS) so that you can build amazing applications using NodeJs.

Given below are three methods to install NodeJs on Debian 11, you can follow any of these you find easier for successful installation:

Installing Nodejs from Debian Repository

At the time of writing this tutorial, the Node.js 12.22.5 version is available under default repositories. To get this version of NodeJs on your Debian system follow the steps mentioned below:

Step 1: Update Packages

First update all the packages previously installed in the System by below mentioned command:

sudo apt update 

Step 2: Install Nodejs and NPM

The “npm” is the package manager of NodeJs, run the below mentioned command to install NodeJs and npm on Debian 11:

sudo apt install nodejs npm 

Step 3: Check Version

To verify the correct version installation of NodeJs, run the below mentioned command to check version number of recently installed NodeJs:

node -v 
Output:
v12.22.5

How to install NodeJs using NodeSource PPA

You can use a PPA (Personal Package Archive) provided by NodeSource to operate with the latest version of NodeJs. This is an alternative repository containing ‘Apt’ and contains current versions than the official Debian repositories for NodeJs.

Follow the steps below for successful installation of NodeJs using PPA:

Step 1: Install PPA

To install NodeJs Package using “Apt”, add the repository to Package list using below-mentioned syntax:

curl -sL https://deb.nodesource.com/setup_[version_number] -o nodesource_setup.sh

You can replace “version number” with the version you want to install, here I am installing version “16.x” by below mentioned command:

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

Step2: Configure NodeSource setup

Run the below mentioned to inquire newly downloaded script, it will open a file and after inspecting the file press Ctrl+X to exit the file and return to terminal:

nano nodesource_setup.sh 

Step 3: Run the Script

After configuring the script, run the script using below mentioned command:

sudo bash nodesource_setup.sh 

The PPA is added to your settings and the local package cache is instantly updated.

Step 4: Install NodeJs

Now after adding PPA, install NodeJs using below mentioned command, we don’t need to install npm separately here as it is already included in package:

sudo apt install nodejs 

Step 5: Check Version

Now verify installation by checking version number of NodeJs:

node -v 

Also check version of npm to verify its installation with NodeJs:

npm -v 

Step 6: Install “build-essential”

To get the needy tools to work with npm package, run the below mentioned command:

sudo apt install build-essential 

Installing NodeJs using NVM on Debian 11

Node Version Manager, abbreviated as NVM, can also be used to install NodeJs on Debian. Instead of functioning in the operating system, NVM operates in the home directory of your user at the level of an independent directory. In other words, without impacting the overall system, you may install numerous autonomous NodeJs versions.
You may use NVM to control your environment while maintaining and handling prior releases in the latest NodeJs versions.

Follow the steps given below to install NodeJs using NVM:

Step1: Download NVM Installation Script

Firstly, from the GitHub link download the nvm installation script by the below-mentioned command:

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

Step 2: Configure Script

Using nano command, inquire the downloaded script by below mentioned command:

nano install_nvm.sh 

After checking the file, if everything seems good then exit the editor by pressing Ctrl+X.

Step3: Run the Script

After configuring the file, run the downloaded script:

bash install_nvm.sh 

Step 4: Gain Access to NVM Functionality

Running NVM script will add additional setup to “~/ .profile” , allowing the new program, you will either log out or log in back; reload “~/ .profile” file using:

source ~/.profile 

Step 5: Install NodeJs from Available versions on NVM

First, we can check which versions of NodeJs are available on NVM by below mentioned command:

nvm ls-remote 

Now choose the version number you want to install from list, Syntax: nvm install [version-number]

I am going to install version 11.7.0, so replace [version-number] with v11.7.0:

nvm install v11.7.0 

In general, the latest version is used by nvm, you need to tell nvm to use the version you downloaded by below mentioned command:

nvm use 11.7.0 

Step 6: Check Version

You can check the version of NodeJs installed using:

node -v 
Output:
v11.7.0

If you are having many node versions installed on your system, to check recently installed version, run the below mentioned command:

nvm ls 

Set Default Version of NodeJs using NVM

If you want to set any version as default, type the below-mentioned syntax: nvm alias default [version-number]

I am going to default version v12.1.0 so replace [version-number] with v12.1.0:

nvm alias default 12.1.0 

Test NodeJs

We can check whether our installed NodeJs is working or not; make sample JavaScript file using nano command:

nano sample.js 

File will be opened in the editor now enter below shown content in file to print “Hello World” on Terminal. Press Ctrl+O to save file and press Ctrl+X to exit file:

const http = require(‘http’);
const hostname = ‘localhost’;
const port = 3000;

const server = http.createServer((req, res) => {
  res.statusCode = 200;
  res.setHeader(‘Content-Type’, ‘text/plain’);
  res.end(‘Hello Worldn’);
});
  server.listen(port, hostname, () => {
  console.log(‘Server running at http://${hostname}:${port}/’);
});

Now to start application run below mentioned command:

node sample.js 
Output:
Server running at http://localhost:3000

Run below mentioned command to test application on another terminal:

curl http://localhost:3000 

How to uninstall NodeJs from Debian 11 Bullseye

Depending on the version you wish to target, you can remove NodeJs with apt or NVM You will need to deal with the apt program on the system level to uninstall versions installed from the Debian repository or from PPA.
To uninstall any of version, run the below mentioned command:

sudo apt remove nodejs 

If you wanted to uninstall version of NodeJs installed from NVM, for that first check the current version of NodeJs installed by below mentioned command:

nvm current 

Then run the below-mentioned syntax to uninstall any specific version of NodeJs installed using NVM on your system:

nvm uninstall [version-number]

I am uninstalling current version of NodeJs, so first I need to deactivate NVM:

nvm deactivate 

Now run the command:

nvm uninstall 12.1.0 

Conclusion

NodeJs is a server side framework to build JavaScript apps. It is used for both back-end and front-end programming. In this article, we discuss its installation on Debian 11 using three methods which are using Debian’s official Repository, through PPA Repository, and also through NVM and also discuss its testing and uninstallation from the system.

The post How To Install Node.Js on Debian 11 appeared first on TecAdmin.

Debian Debian 11 General Articles Nodejs nvm TecAdmin