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
- How to Run Node.js Scripts on Debian 12
- How to Access Node.js Shell on Debian 12
- How to Switch Between Node.js Versions on Debian 12
- How to Remove Node.js From Debian 12
- Final Thoughts
How to Install Node.js on Debian 12
On Debian 12, Node.js can be installed via:
- Default Debian Repository
- Installation Script
- NodeSource Repository
- Source Code
- Node Version Manager
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:
Step 2: Install Node.js on Debian 12
Then, install the Node.js package i.e. “nodejs” on your Debian 12 system as:
Step 3: Ensure Node.js Installation
Finally, check the Node.js installed version to verify its installation on Debian 12:
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:
Step 2: Verification
Check the installed Node.js version to ensure that it has been successfully installed on Debian 12:
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:
Step 2: Import NodeSource GPG Key
Then, import the NodeSource GPG key to ensure the integrity of Node.js packages as:
Step 3: Add NodeSource Repository
Next, import/add the official NodeSource repository to your Debian 12 system:
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:
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:
Step 6: Ensure Installation
Lastly, display the Node.js version to confirm its installation:
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:
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:
Step 3: Install Required Dependencies
You also have to install the following dependencies on your Debian system for compiling source code:
Step 4: Start Configuration Process
To configure Node.js on your system, you need to navigate the extracted directory through the given command:
Then, initiate the configuration process:
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:
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:
Step 7: Verification
To ensure that Node.js has been installed successfully, check its installed version:
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:
After that, type out the below-listed command or restart the terminal to save the changes:
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:
Similarly, you can install any other Node.js version, such as the latest “21.6.2” as seen below:
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:
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(process.env);
Save and close the file by pressing the “CTRL + S” and “CTRL + X” keys respectively.
Then, run the “test.js” Node.js script file as:
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:
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:
Similarly, you can enter other JavaScript code like concatenating strings as:
If you want to exit/leave the Node.js shell, run the given command:
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:
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:
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:
You also have to remove the NodeSource repository and GPG key from your system:
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:
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:
Then, deactivate the current version by utilizing the given command:
Finally, uninstall the specific Node.js version through the given command:
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.