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:
Method 1 – 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:
- Update Packages – First update all the packages previously installed in the System by below mentioned command:
sudo apt update
- 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
- 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
Method 2 – 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:
- 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 stable version “16.x” by below mentioned command:
curl -sL https://deb.nodesource.com/setup_16.x -o nodesource_setup.sh
- 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
- 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.
- 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
- Check Version – Now verify installation by checking version number of NodeJs:
node -v
Output:v16.14.0Also check version of npm to verify its installation with NodeJs:
npm -v
Output:8.3.1 - Installing “build-essential” – To get the needy tools to work with npm package, run the below mentioned command:
sudo apt install build-essential
Method 3 – 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:
- 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/creationix/nvm/master/install.sh -o install_nvm.sh
- 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.
- Run the script – After configuring the file, run the downloaded script:
bash install_nvm.sh
- 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
- 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 16.14.0, so replace [version-number] with 16.14.0:
nvm install 16.14.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 16.14.0
- Check Version – You can check the version of NodeJs installed using:
node -v
Output:v16.14.0If you are having many node versions installed on your system, to check recently installed version, run the below mentioned command:
nvm ls
Output:-> v16.14.0 system default -> 16.14.0 (-> v16.14.0) iojs -> N/A (default) unstable -> N/A (default) node -> stable (-> v16.14.0) (default) stable -> 16.14 (-> v16.14.0) (default) lts/* -> lts/gallium (-> v16.14.0) lts/argon -> v4.9.1 (-> N/A) lts/boron -> v6.17.1 (-> N/A) lts/carbon -> v8.17.0 (-> N/A) lts/dubnium -> v10.24.1 (-> N/A) lts/erbium -> v12.22.10 (-> N/A) lts/fermium -> v14.19.0 (-> N/A) lts/gallium -> v16.14.0
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 v16.14.0 so replace [version-number] with v16.14.0:
nvm alias default 16.14.0
Test NodeJs
We can check whether our installed NodeJs is working or not; make sample JavaScript file using nano command:
nano sample.js
The file will be opened in the editor now enter the below-shown content in the file to print “Hello World” on Terminal. Press Ctrl+O to save file and press Ctrl+X to exit file:
1 2 3 4 5 6 7 8 9 10 11 12 | 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 World\n’); }); 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.