How to Install Node.js on Debian

June 27, 2024

Introduction

Node.js is an open-source JavaScript (JS) runtime environment for developing web applications. It is lightweight and efficient, thanks to its event-driven, non-blocking I/O model and cross-platform support.

There are several ways to install Node.js on Debian. The best approach depends on the specific Node.js version.

This guide explains how to install Node.js on Debian using three different methods.

How to Install Node.js on Debian

Prerequisites

  • A system running Debian OS (we used Debian 12).
  • Access to the terminal.
  • Sudo privileges.

Method 1: Install Node.js via apt

The simplest way to install Node.js on Debian is from the official repository. This method is simple and sufficient for development purposes.

Note: The Node.js version included in the repositories may be outdated. While older Node.js versions are suitable for development environments, they are not recommended for production environments.

Follow the steps below to install Node.js using the APT package manager.

Step 1: Update the Repository

Before installing Node.js, make sure the repository is up to date. To update system repositories, run the following apt command:

sudo apt update
sudo apt update terminal output

Then, upgrade packages to the latest version:

sudo apt upgrade
sudo apt upgrade terminal output

After updating and upgrading the repository, proceed to the next step.

Step 2: Install Node.js

Use apt install to install Node.js and the Node Package Manager (NPM), the default package manager for Node.js.

Run the following command:

sudo apt install nodejs npm -y
sudo apt install Node.js NPM -y terminal output

The command also installs any required dependencies for both packages.

Step 3: Verify the Installation

Run the node command with -v to check the Node.js version and verify the package installation:

node -v
node -v terminal output

Confirm NPM is installed with:

npm -v
npm -v terminal output

Both commands show the program version.

Method 2: Install Node.js via NodeSource PPA

The latest version of Node.js may not be available in the APT repository. To get a newer version, use the PPA repository by NodeSource. This method enables you to choose a specific version that is unavailable through the repositories.

Step 1: Install the curl Command

Installing Node.js from PPA requires the curl command. To install the latest version of curl, run:

sudo apt install curl
sudo apt install curl terminal output

The output shows that curl is up to date. In case it is not, the system updates the command. To verify the installation, run:

curl --version
curl --version terminal output

Step 2: Add PPA to the System

Add PPA to the system and install it with:

curl -sL https://deb.nodesource.com/setup_18.x | sudo -E bash -
add PPA with curl terminal output

The command downloads and runs the Bash script to add the package to the repository listing.

Note: NodeSource offers different Node versions. The example above uses 18.x. To download another version, replace 18.x in the previous command with another version number.

Step 3: Update the Repository

Once the system adds the PPA, update the repository:

sudo apt update
sudo apt update PPA terminal output

Check for upgrades with:

sudo apt upgrade
sudo apt upgrade PPA terminal output

Step 4: Install Node.js and NPM

When installing from the PPA, Node.js and NPM come in the same package. Install both with:

sudo apt install nodejs
sudi apt install Node.js terminal output

Step 5: Verify the Installation

To confirm Node.js and NPM are installed, check their versions:

node -v
node -v PPA installation terminal output
npm -v
NPM -v PPA installation terminal output

Method 3: Install Node.js via NVM

Another way to install Node.js on Debian is with the Node Version Manager (NVM). NVM manages installed Node.js versions and enables switching between different versions using the terminal. Use this method to work with multiple Node.js versions on the same system.

Note: Learn about the differences between NVM vs. NPM.

Step 1: Get the Installer Script

To install NVM, download the installer from GitHub. Run the following command:

curl https://raw.githubusercontent.com/creationix/nvm/master/install.sh | bash
Download NVM installer terminal output

The command downloads and runs the Bash script to install NVM.

Step 2: Reload the Bash Profile

To add the path to the NVM script in the current shell session, restart the terminal or source the .profile file:

source ~/.profile
Reload the bash profile terminal output

Both methods apply the file contents to the shell session and enable using the nvm command.

Step 3: List Node.js Versions

Run the following to list all available Node.js versions:

nvm ls-remote
nvm ls remote terminal output

The command prints all available Node.js version numbers.

Step 4: Install Node.js

Use nvm install to download and install your preferred Node.js version. For instance, to install version 12.1, run:

nvm install 12.1
nvm install 12.1 terminal output

The installed version becomes the system's default Node.js. To confirm, run:

node -v
node -v NVM installation terminal output

The newly installed version is shown in the output.

Step 5: Switch Versions

NVM allows users to download and install multiple Node.js versions on the same system. For instance, you can download and install versions 18.1 and 16.5:

NVM install multiple versions terminal output

List all installed versions with the following command:

nvm ls
nvm ls terminal output

The command shows three versions, with the currently active one shown with a pointer printed in green.

To switch to another installed version, use:

nvm use 18.1
nvm use terminal output

The nvm ls output also shows the current default version as 12.1.

nvm ls default version terminal output

To change the default Node.js version, use:

nvm alias default 18.1
nvm alias default terminal output

Node.js now defaults to the selected version.

Conclusion

This guide demonstrated various methods for installing Node.js on Debian. It also showed how to use NPM to manage Node.js packages and handle different Node.js versions.

Next, learn how to update Node.js to the latest version or how to build a Node.js app with Docker.

Was this article helpful?
YesNo
Sara Zivanov
Sara Zivanov is a technical writer at phoenixNAP who is passionate about making high-tech concepts accessible to everyone. Her experience as a content writer and her background in Engineering and Project Management allows her to streamline complex processes and make them user-friendly through her content.
Next you should read
MEAN Vs. LAMP: Which is Better?
January 16, 2024

LAMP and MEAN are software bundles used to develop web applications. Learn the difference between the two stacks, see their pros and cons and their use cases in our comparison article...
Read more
How to Install Node.js and NPM on CentOS 7
June 29, 2019

Node Package Manager (npm) is Node’s official package manager, used for installing and managing package dependencies. In this guide...
Read more
How To Install Node.js & NPM on Ubuntu 18.04 and 20.04
March 18, 2019

Node.js is an open-source cross-platform JavaScript (JS) runtime environment. It is used for building fast and scalable network applications. Learn three different...
Read more
How to Install & Setup MEAN Stack on Ubuntu (MongoDB, Express.JS, Angular.JS, Node.JS)
March 28, 2024

The MEAN stack is an open-source JavaScript (JS) framework used for developing robust web applications...
Read more