Debian

How to install Node Debian 12?

how to install node on debian 12

Node.js is a popular free-to-use runtime environment that is utilized to run JavaScript code on the server side (independently of a web browser). It is an efficient tool for building web applications as it can handle multiple requests at the same time without getting blocked. Moreover, it is a cross-platform tool that can be run on Windows, macOS as well as various Linux distributions including Debian 12.

This guide will demonstrate different methods for Node.js installation on Debian 12.

Overview

How to Install Node.js on Debian 12

On Debian 12, Node.js can be installed via:

Method 1: Install Node.js on Debian 12 From Default Debian Repository

The default Debian repository contains numerous software packages including Node.js for Debian users. It is the simplest way to install Node.js on Debian 12, but it provides a bit outdated version. You can install Node.js from the Debian repository by following the given steps:

Step 1: Refresh System Packages

First, update the Debian system packages list as:

sudo apt update

Step 2: Install Node.js on Debian 12

Then, install the Node.js package i.e. “nodejs” on your Debian 12 system as:

sudo apt install nodejs

Step 3: Ensure Node.js Installation

Finally, check the Node.js installed version to verify its installation on Debian 12:

node --version

The output shows the installed outdated version:

Method 2: Install Node.js on Debian 12 Using Installation Script

The official Node.js provides the installation script to automatically install Node.js along with all dependencies on Debian 12 systems. It is the handy and fastest approach to get the most up-to-date version of Node.js on Debian 12. Here are the steps:

Step 1: Download and Install Node.js on Debian 12

To get the most recent version of Node.js on your Debian 12 system, execute the given command to automatically install it from the installation script:

curl -fsSL https://deb.nodesource.com/setup_21.x | sudo -E bash - && sudo apt install nodejs -y

Step 2: Verification

Check the installed Node.js version to ensure that it has been successfully installed on Debian 12:

node -v

The output shows the Node.js latest version installed on the system:

Method 3: Install Node.js on Debian 12 From NodeSource Repository

You can also install Node.js on Debian 12 from the NodeSource repository by adding it to your system. It is the manual installation method that not only provides the Node.js latest version but also upgrades it automatically via Debian’s package manager. Check out the given steps to install Node.js on Debian 12 from the NodeSouce repository:

Step 1: Install Necessary Dependencies

First, install the given required dependencies for Node.js installation on the system:

sudo apt install ca-certificates curl gnupg -y

Step 2: Import NodeSource GPG Key

Then, import the NodeSource GPG key to ensure the integrity of Node.js packages as:

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

Step 3: Add NodeSource Repository

Next, import/add the official NodeSource repository to your Debian 12 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

Step 4: Update System’s Packages

You also need to refresh your Debian system to implement new changes:

sudo apt update

Step 5: Install Node.js on Debian 12

Now, utilize the given command the install Node.js on your Debian 12 system from the official NodeSource repository:

sudo apt install nodejs

Step 6: Ensure Installation

Lastly, display the Node.js version to confirm its installation:

node -v

Method 4: Install Node.js on Debian 12 Via Source Code

The official Node.js provides the source code for the installation of the latest Node.js version. This approach directly installs Node.js on the Debian 12 system from the source code but consumes a lot of users’ time so it is least recommended. You can try out the given instructions for Node.js installation through its source code:

Step 1: Download Node.js Source Code File

First, copy the URL of the latest source package file on the official Node.js website and download it using the “wget” command:

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

Here, we are downloading the source code file of the latest Node.js version i.e. “v21.6.2”.

Alternatively, download it directly from the official website:

Step 2: Extract Source Code File

Then, use the “tar xzf” command followed by the downloaded Node.js compressed file name to extract it:

tar xzf node-v21.6.2.tar.gz

Step 3: Install Required Dependencies

You also have to install the following dependencies on your Debian system for compiling source code:

sudo apt install zlib1g-dev libncursesw5-dev build-essential libncurses5-dev libffi-dev libbz2-dev libsqlite3-dev libssl-dev -y

Step 4: Start Configuration Process

To configure Node.js on your system, you need to navigate the extracted directory through the given command:

cd node-v21.6.2

Then, initiate the configuration process:

./configure

Step 5: Compile and Build Source Code

Now, use the below-listed command to start the build process through parallel compilation and create the necessary files for Node.js installation:

make -j 3

Note: This process will take a few hours to finish/complete.

Step 6: Install Node.js on Debian 12

Finally, you can install Node.js on your Debian 12 system via the following command:

sudo make install

Step 7: Verification

To ensure that Node.js has been installed successfully, check its installed version:

node -v

Method 5: Install Node.js on Debian 12 Through Node Version Manager

The Node Version Manager (NVM) is a utility that enables users to handle multiple Node.js versions and easily switch between them on the system. You can use the Node Version Manager to seamlessly install and use various Node.js versions without interfering with each other. Here are the steps:

Step 1: Download and Install NVM on Debian 12

First, you need to download the NVM from GitHub and install it on your Debian 12 system using the given command:

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

After that, type out the below-listed command or restart the terminal to save the changes:

source ~/.bashrc

Step 2: List Available Node.js Versions

You can see all the available Node.js versions and choose the desired version that you want to install on your Debian 12 system:

Scroll down to view more:

Step 3: Install Node.js

To install the specific Node.js version, use the “nvm install <version>” command and specify the desired version that needs to be installed. Here, we are downloading the “20.11.1” Node.js version:

nvm install 20.11.1

Similarly, you can install any other Node.js version, such as the latest “21.6.2” as seen below:

nvm install 21.6.2

How to Run Node.js Scripts on Debian 12

The Node.js scripts are the JavaScript files that are executed via the Node.js runtime environment. You can write Node.js scripts using any text editor and save them with the “.js” extension. Then, you can execute Node.js scripts via the “node” command in the terminal.

Follow the given instructions for writing and running Node.js scripts on Debian 12:

First, create and open the “test.js” Node.js script in the Nano text editor:

nano test.js

In the “test.js” file, write the desired JavaScript code. Here, we are simply adding the following code. The first line will print the “Hello! Linux Ways!” message on the console and the second line will display the environment information in which the script is running:

console.log("Hello! Linux Ways!");
console.log(process.env);

Save and close the file by pressing the “CTRL + S” and “CTRL + X” keys respectively.

Then, run the “test.jsNode.js script file as:

node test.js

How to Access Node.js Shell on Debian 12

The Node.js shell is the interactive environment where users can run the JavaScript code directly and see the output. You can access the Node.js shell and interact with it on Debian 12 by writing out the below-listed command in the terminal:

node

As you can see, the Node.js shell has started:

Now, you can write the desired JavaScript code here. For instance, we are just printing the following message:

console.log("Hello! Linux Ways!")

Similarly, you can enter other JavaScript code like concatenating strings as:

"Linux " + "Ways"

If you want to exit/leave the Node.js shell, run the given command:

.exit

How to Switch Between Node.js Versions on Debian 12

If you have installed multiple Node.js versions on your Debian 12 system and you want to use the specific version, you can easily switch to it. For this purpose, you have to list the installed Node.js versions, choose the desired version, and switch to it.

You can list all the installed Node.js versions as:

nvm list

As you can see the current Node.js version is “v21.4.0”:

To switch to and use a particular Node.js version, use the “nvm use <version>” command and specify the desired version that you want to use. Here, we are switching to the “v21.6.2” Node.js version:

nvm use v21.6.2

How to Remove Node.js From Debian 12

If you do not need Node.js on your Debian 12 system, you can simply uninstall/remove it based on the method you have installed. Following are the Node.js removal methods on Debian 12:

If Installed From Debian/NodeSource Repository or Installation Script

To uninstall Node.js that you have installed from the default Debian repository, installation script, or NodeSource repository, use the given command:

sudo apt remove nodejs -y

You also have to remove the NodeSource repository and GPG key from your system:

sudo rm -r /etc/apt/sources.list.d/nodesource.list && sudo rm -r /etc/apt/keyrings/nodesource.gpg

If Installed From Source Code

If you have installed Node.js from the source code, use the given command to remove it from the Debian 12 system:

sudo find /home/linuxway -depth -iname 'node*' -exec rm -rf {} \;

Note: Make sure to replace “/home/linuxway” with your desired directory path:

If Installed Using Node Version Manager(NVM)

If Node.js is installed through NVM on your system, you can remove its specific version via the “nvm uninstall <version>” command. However, if you want to uninstall the current version, you have to deactivate it first and then you can remove it.

You can check out the currently used version as:

nvm current

Then, deactivate the current version by utilizing the given command:

nvm deactivate

Finally, uninstall the specific Node.js version through the given command:

nvm uninstall v21.6.2

That was all about the installation and usage of Node.js on Debian 12.

Final Thoughts

You can install Node.js on Debian 12 from the default Debian repository, installation script, NodeSource repository, source code, or Node Version Manager. Among all these methods, the installation script and NodeSource repository methods provide the latest and up-to-date version of Node.js on the Debian 12 system. Other than this, the Node Version Manager method allows you to install numerous Node.js versions and also switch to the desired version. This guide has explained various methods to install, use, and remove Node.js on Debian 12.

Similar Posts