Debian

How to Install Node.js on Debian 12

Node.js is an open-source JavaScript runtime environment used to run JavaScript code outside of a Internet browser. It is built upon the Chrome V8 JavaScript engine that has become increasingly popular because of its asynchronous, event-driven and single-threaded nature. This allows Node.js to simultaneously handle a large number of requests without blocking the main thread.

As a Debian user, you may be wondering whether Node.js is a good choice for you. The answer is yes! Node.js is a tool you must install on your Debian system as it will help you develop web servers, custom tools and automate system tasks.

In this guide, you will learn:

How to Install Node.js on Debian 12

Conclusion

How to Install Node.js on Debian 12

To install Node.js on Debian 12, you can:

  • Use Apt Package Manager
  • Add Repository
  • Use tar.gz File
  • Use NVM

How to Install Node.js on Debian 12 from Apt Package Manager

The simplest way to install Node.js on Debian is to use the apt package manager that installs the application from the system repository. The current apt method won’t install the Node.js latest version on Raspberry Pi, but it can be helpful in case someone wants to install Node.js quickly.

To install Node.js from apt package manager, use the following steps:

Step 1: Update Debian Repository

First, open terminal and run the command below to update Debian repository:

sudo apt update && sudo apt upgrade -y

Step 2: Install Node.js from Apt Package Manager

After updating the repository, run the following command to directly install Node.js from the apt package manager:

sudo apt install nodejs -y

Step 3: Check Node.js Version on Debian

After installation, you can confirm Node.js version on Debian using the command given below:

node --version

How to Remove Node.js on Debian 12 from Apt Package Manager

If you don’t like using Node.js install from apt package manager on Debian, you can remove it from your system through the following command:

sudo apt remove nodejs -y

How to Install Node.js on Debian 12 By Adding Repository

You can also add Node.js repository and install the latest version of Node.js on Debian, this can be done by using the following steps:

Step 1: Install Prerequisites for Node.js on Debian

First, use the terminal command given below to install a few prerequisites required for installing Node.js on your Debian system:

sudo apt install ca-certificates curl gnupg -y

Step 2: Create a keyrings Directory

Now, you have to create a directory with the name keyrings in /etc/apt location so that the system can store public keys required for adding the external repository. You can create a keyrings directory on Debian from the following command:

sudo mkdir -p /etc/apt/keyrings

Step 3: Import GPG Key

After creating the keyrings directory, it’s now time to import GPG key to verify and authenticate the integrity of the software packages. You can add GPG key for the external repository on Debian using the following command:

curl -fsSL https://deb.nodesource.com/gpgkey/nodesource-repo.gpg.key | sudo gpg --dearmor -o /etc/apt/keyrings/nodesource.gpg

Step 4: Add Node.js Repository to Debian

You have to add Node.js repository and specify the version you want to install on Debian, the following command will add Node.js v21 repository on your Debian system:

NODE_MAJOR=21

echo "deb [signed-by=/etc/apt/keyrings/nodesource.gpg] https://deb.nodesource.com/node_$NODE_MAJOR.x nodistro main" | sudo tee /etc/apt/sources.list.d/nodesource.list

Note: For latest Node.js updates for this method, do visit here.

Step 5: Update Repository on Debian

Once you added the repository to Debian, you must update the system repository using the following command:

sudo apt update

Step 6: Install Node.js on Debian

If the above command runs without an error or warning, you can then execute the apt command to install the latest version of Node.js on Debian:

sudo apt install nodejs -y

Step 7: Check Node.js Version on Debian

After successfully install Node.js on Debian from the external repository, you can run the following command to ensure it is installed correctly:

node --version

How to Remove Node.js on Debian 12 from Repository

If you have installed Node.js on Debian from the external repository and want to remove it from the Debian system, you can simply run the following command:

sudo apt remove nodejs -y

How to Remove Node.js Repository from Debian 12

You can also remove the external repository from Debian by first navigating towards the /etc/apt/sources.list.d location from the following command:

cd /etc/apt/sources.list.d

Then use the ls command to check for the repository added to Debian:

ls

After that, run the following command to remove nodesource repository from your Debian system:

sudo rm -rf nodesource.list

How to Install Node.js on Debian 12 from tar.gz File

You can also install Node.js on Debian by downloading the tar.gz file and perform required configuration steps to complete the installation. The steps provided below will help you install Node.js on Debian from the tar.gz file:

Step 1: Download Node.js tar.gz File on Debian

First, you have to download the Node.js tar.gz file from the official website. The latest version of Node.js at the time of writing this article is 21.2.0, which can be downloaded on Debian from the following command:

sudo wget https://nodejs.org/dist/v21.2.0/node-v21.2.0.tar.gz

Step 2: Extract Node.js tar.gz File

You have to extract the content of Node.js tar.gz file on Debian, this can be done using the tar command provided below:

sudo tar -xvf node-v21.2.0.tar.gz

Step 3: Install Dependencies on Debian

You are required to install a few dependencies as well through the following command since they are required to perform further installation steps:

sudo apt install g++ cmake -y

 

Step 4: Open Node.js Directory on Debian

Navigate to Node.js source folder you just downloaded on Debian using the cd command:

cd node-v21.2.0

Note: The directory name could be different if you are installing other Node.js versions on the Debian system. You can check the source directory name using the ls command.

Step 5: Configure Node.js on Debian

You must run the following command to configure Node.js on Debian and ensure the configuration should be successful:

./configure

Step 6: Build Node.js Installation File on Debian

You have to use the make command on Debian inside the Node.js source directory to build the files that are required for installing Node.js on Debian:

make

This process will take some minutes or hours preparing the installation files.

Step 7: Install Nodejs on Debian

After the successful execution of the above step, you can install Node.js on Debian by running the following command:

sudo make install

Step 8: Check Node.js Version on Debian

You can then check Node.js version installed on Debian from the tar.gz file by running the following command:

node --version

How to Remove Node.js on Debian 12 from tar.gz File

If you want to remove Node.js on Debian installed from tar.gz file, you can simply use the following command:

sudo make uninstall

If that doesn’t work, you can try removing the Node.js source directory from Debian using the following command:

sudo rm -rf <directory_name>

How to Install Node.js on Debian 12 from NVM

Node Version Manager or NVM is a package manager that simplifies the installation of Node.js since it allows you to install any Node.js version on your system. The advantage of using NVM to install Node.js is that you can use multiple Node.js versions on your system. To install the Node.js latest version on Debian 12, you can use the following steps to install it through NVM:

Step 1: Download and Run NVM Script on Debian 12

First, you have to download NVM installation script and run it on Debian, this can be done from the following command:

wget -qO- https://raw.githubusercontent.com/nvm-sh/nvm/master/install.sh | bash

Step 2: Save the Changes

You have to save the changes you made in the above steps using the following command:

source ~/.bashrc

Note: You can also restart your terminal in case you don’t want to run the above command.

Step 3: Check List of Node.js Version on Debian

To check the list of different versions of Node.js that can be installed through NVM on Debian 12, you can use the following command:

nvm ls-remote

Scroll down to see various versions of Node.js:

Step 4: Install Node.js on Debian Through NVM

You can now then install Node.js on Debian according to your choice through the following syntax:

nvm install version_number

Replace version_number with the Node.js version you want to install. Here, I am going with installing the latest version of Node.js on Debian using the following command:

nvm install 21.2.0

Step 5: Check Node.js Version

To check Node.js is successfully installed through NVM on Debian, you can run the following command:

node --version

How to Remove Node.js on Debian 12 from NVM

If you have installed Node.js on Debian 12 from NVM, you can remove it by first deactivating the NVM on your system through the following command:

nvm deactivate

Then you can run the following command to uninstall Node.js from Debian installed through NVM:

nvm uninstall version_number

Conclusion

Node.js has emerged as a powerful JavaScript runtime environment used for executing code outside of a browser. You can install Node.js on Debian 12 through apt package manager or by adding an external repository and install it through apt command. You can use the tar.gz file or NVM to install the Node.js latest version on Debian 12. This guide has provided a detailed step-by-step guide to install Node.js on Debian 12 from the aforementioned methods. Following it will help users create Node.js applications, web servers or automate system tasks on Debian.

About the author

Awais Khan

I'm an Engineer and an academic researcher by profession. My interest for Raspberry Pi, embedded systems and blogging has brought me here to share my knowledge with others.