How to Install Nodejs on Debian 12?


Node.js is a server-side run time environment, based on Javascript. The services implemented via Node.js power client applications like Mobile and Web applications running on cell phones and web browsers respectively. Node.js is used to build real-time, fast, scalable, and data-intensive backend services/Application Programming Interfaces (APIs).

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

 

How to Install Node.js on Debian 12?

Node.js can be installed via the default repositories of Debian 12, built via the source, NodeSource, and Node Version Manager (NVM) as demonstrated below:

1. Using Default Repositories of Debian 12

The apt (Advance Packaging Tool) manages packages in Debian 12. To install Node.js via apt, first update the system packages:

sudo apt update

Then, install Node.js:

sudo apt install nodejs -y

To test node.js installation, launch node.js in the terminal by:

node

Execute a basic script, for example:

console.log("Linux Genie!")

The script will print Linux Genie! Which verifies that node.js is working properly. To exit node.js, type .exit:

2. Using Source Code

Building Node.js via the source requires downloading and compiling the source code. Execute the below-listed steps to build Node.js via the source:

Prerequisites: Install Dependencies

First update system packages followed by installing dependencies essential for compiling compressed archives:

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

Step 1: Download Source Code

The following command downloads the compressed archive containing the source code from Node.js official website:

wget https://nodejs.org/dist/v20.10.0/node-v20.10.0.tar.gz

Note: All the previous releases of Node.js can be found on the Node.js Official website.

Step 2: Extract the Compressed Archive of Node.js

Extract the node-v20.10.0.tar.gz:

tar xzf node-v20.10.0.tar.gz

The compresses archive node-v20.10.0.tar.gz is extracted to node-v20.10.0.

Step 3: Compile and Build the Source Code

Navigate to the extracted directory node-v20.10.0. The following command prepares the source code for the building process:

./configure

The make command compiles and builds the source code. The build process takes time to complete. To increase the speed of the build process, we can assign multiple CPU processors via the j option:

make -j 2

Note: Check the number of CPU processors by the nproc command.

Step 4: Install Node.js

The below-listed command installs Node.js:

sudo make install

To verify installation, check Node.js version:

nodejs -v

3. Using NodeSource Repository

NodeSource is a repository that contains different versions of Node.js. Execute the following steps to install Node.js via NodeSource:

Prerequisites: Install Dependencies

First, update the system repositories. Next, install the following tools:

  • ca-certificates: Used for verification/identification of websites to communicate with them securely.
  • curl: Downloads/Transfers data.
  • gnupg: Authenticates the sources (of the packages/repositories) to get the verified package only.
sudo apt install -y ca-certificates curl gnupg

Step 1: Import GPG Key

First, create a directory keyrings:

sudo mkdir -p /etc/apt/keyrings

Then, import the NodeSource repository’s gpg key in the directory /etc/apt/keyrings:

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

Step 2: Create a deb Repository

The command below creates a deb repository with Node.js version 20:

NODE_MAJOR=20
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: To create a deb repository with a different version of Node.js, change the value of NODE_MAJOR to the desired version, i.e., 16, 18, 21, etc.

Step 3: Update and Install Node.js

Update system packages to add the updated version to the system repositories:

sudo apt update

Next, install Node.js:

sudo apt install nodejs -y

4. Using Node Version Manager (NVM)

Node Version Manager manages different versions of Node.js and enables to install, uninstall, and switch between different Node.js versions. The command below downloads NVM from GitHub followed by installing NVM via bash:

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

Alternatively, NVM can be installed by:

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

After successful installation, either execute the bashrc file or restart the terminal to add the path to the NVM script:

source ~/.bashrc

How to Manage Node.js via Node Version Manager on Debian 12?

The following sections will demonstrate how to Manage Nodejs via the Node version manager:

List all Versions of Node.js

To list all available versions of Node.js, execute:

nvm ls-remote

Install Node.js

Node Version Manager enables a user to install multiple versions of Node.js from the source. For example, install Node.js 20.10.0 version:

nvm install 20.10.0

Similarly, install Node.js 18.19.0 version:

nvm install 18.19.0

Check Installed Node.js Versions

To return a list of active Nodejs versions along with the current version, run the command below:

nvm ls

Switch Versions

To switch from the current version 18.19.0 to 20.10.0 version, execute:

nvm use 20.10.0

 

How to Uninstall Node.js?

To uninstall Node.js along with all the data and configurations, execute:

sudo apt purge nodejs -y

To uninstall Node.js versions installed via NVM, enter the desired version in the syntax below:

nvm uninstall <version>

The current version cannot be deleted. To check the current version, execute:

nvm current

In case, the current version is to be uninstalled, first deactivate it:

nvm deactivate

Then uninstall:

nvm uninstall 20.10.0

Conclusion

The most preferable way to install Node.js on Debian 12 is via Debian’s default repositories, i.e., apt. However, the latest version of Node.js is not available via apt. To install the latest version, install Node.js via NodeSource, Node Version Manager (NVM), or build Node.js via source. Among these, NVM not only installs but allows you to manage (switch between versions/uninstall) the node on your Debian. This guide demonstrated four methods to install Node.js on Debian 12.

 

Print Friendly, PDF & Email