Node.js is an open-source cross-platform that provides a runtime environment for JavaScript, primarily used in web servers and APIs. Further, its event-driven architecture and asynchronous programming give it the capability to handle a large volume of concurrent requests. Debian on the other hand is also a stable Linux distribution that offers a strong foundation for deploying, and building web and other server-side applications. This blog post will extensively discuss all the possible ways to install Node.js in Debian 12
6- Ways to Install Node.js in Debian 12
Node.js provides a faster development cycle and reduces the context switch between different languages. Further, it can be used for both back-end and front-end development, thus simplifying the development for full-stack developers. On Debian, there are 6 different ways to install Node.js which this guide will explain.
1: Through Debian apt Package Manager
Installing Node.js using the Debian apt package manager is one of the simple methods. If you are willing to use its older version, run the following command:
sudo apt install nodejs -y
To validate the Node.js Installation on Debian, check its version by executing the below command:
node -v
Here, in the image below, it can be seen that the older version of Node.js is installed through apt:
Further, you will need a node package manager to install JavaScript packages on Debian 12 so for that execute:
sudo apt install npm -y
Just to verify the node version manager(npm) installation check its version by running:
npm -v
2: Through Node.js Deb Repository
The deb repository of Node.js comes with version 20 of nodejs, so simply download and execute it in the bash shell by using the following command:
curl -fsSL https://deb.nodesource.com/setup_20.x | sudo bash -
The repository will be saved in the sources.list directory of Debian:
Next, use the Debian default repository to install Node.js in Debian 12:
sudo apt-get install -y nodejs
To validate the Node.js installation on Debian, check its version by executing the below command:
node -v
Here in the image below, you can see that the node version 20 is installed:
3: Through Node Version Manager (NVM)
If you want to install multiple versions of nodejs on Debian 12, then the best way is to use the node version manager. Using this node version manager, you can also switch between Node.js versions. First, you need to install the node version manager on Debian using the GitHub bash file for NVM:
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash
This will create a hidden NVM folder on Debian, which you can remove if you want to uninstall NVM after deactivating it:
Next, restart your terminal or execute the below command to add the NVM script in the path:
source ~/.profile
As previously mentioned NVM comes with all the versions of Node.js so to list all of them execute the below command:
nvm ls-remote
Currently, the latest version of Node.js is 22.3.0 in NVM, to install nodejs on Debian using NVM execute the below command:
nvm install [desired-nodejs-version]
Here, I have installed the latest version of nodejs on Debian:
4: Through Node.js Pre-built Binaries
Another way to install Node.js on Debian 12 is by using its pre-built binaries. This method doesn’t require any installation process. Just extract the file and execute it. First, visit the download page of Node.js and then select the Prebuilt Binaries option. Afterward, select the desired Node.js version then select the Linux operating system along with its architecture. Next, click on the download option:
Once the file is downloaded extract the contents of the file by using the below command, further edit this command as it will vary based on the version of Node.js:
sudo tar -xvf node-v22.3.0-linux-x64.tar.xz
Next, move to the bin directory of the extracted folder and execute the node file. This will launch the Node.js on Debian:
./node
5: Through Node.js Source Code
The fifth method to install Node.js on Debian is by compiling the source file, which can be downloaded from the Node.js official website. Before downloading the source file, select the Node.js version and then click on the Download option:
The next thing to do is to extract the source file of Node.js by using the tar utility along with xvzf flags:
sudo tar xvzf node-v22.3.0.tar.gz
Now move to the extracted directory of the Node.js source file on Debian, here you will to a file named configure which will be used to check all the necessary packages for installing Node.js:
Next, execute the configuration file in the source code of Node.js
./configure
Now create the make file for Node.js that specifies the dependencies required for a successful installation, and define rules that specify the commands to execute for the installation target.
make -j4
This step will take more than an hour but the estimated time is purely dependent on the system resources:
Next, install the Node.js using the created make file, again this step will also take an hour to complete roughly:
make install
6: Through Snap Package Manager
The last method for installing Node.js on Debian is using the Snap package manager by executing:
sudo snap install node --channel=22/stable --classic
Next, list all the Debian applications installed via the Snap package manager to validate the installation of Node.js on Debian 12:
sudo snap list
How to Switch Node.js Versions Using NVM in Debian 12
Using the Node Version Manager, you can not only install multiple Node.js versions but can also switch between them on Debian. Like, you can also set different versions for the default and current use. Moreover, you can also switch between Node.js versions depending on your needs. Firstly, it is better to know which Node.js versions are available, for that list all Node.js available in the node version manager:
nvm ls-remote
Next, you can list all the installed versions of nodejs on Debian 12 by executing the below command:
nvm ls
Here in the image below you can see that the current version of Node.js in use is 18 whereas the default version set for Node.js is 22.3.0:
To use a different version then use the below syntax for switching to the desired version:
nvm use [deired-nodejs-version]
Here, for instance, I have switched my current version of Node.js on Debian from 18 to 22, and in the image below you can see that version 18.20.3 is the current version in use switched to version 22:
If you want to set any desired version as default for nodejs on Debian, then use the below syntax:
nvm alias default [desired-nodejs-version]
Here, I have changed my default node.js version from 22.3.0 to 18.20.3
Note: Instead of using the version number for Node.js installation on Debian, you can also use the respective version name if installing through the node version manager.
Conclusion
To install Node.js on Debian 12 there are 6 ways which include using apt package manager, Snap app installer, Node.js Deb repository, node version manager, Node.js prebuilt binaries, and Node.js source code. The recommended method for Node.js installation on Debian 12 is using the node version manager. This is because one can select from a wide range of available versions and can also have multiple Node.js versions installed which can also be switched.