Introduction
Node.js is an open-source JavaScript (JS) runtime environment mostly used for the development of online apps. This cross-platform environment is lightweight and efficient due to its event-driven, non-blocking I/O model.
This guide will teach you how to install Node.js on Debian using three different methods.
Prerequisites
- A Debian 9, 10, or 11 system.
- Access to the terminal.
- Sudo privileges.
Method 1: Install Node.js via apt
The simplest method for installing Node.js on Debian is from the official repository.
Note: The Node.js version included in the Debian repository could be outdated. Older Node.js versions are suitable for experimentation but should not be used in production environments.
To install Node.js from Debian's repository, follow the steps below.
Step 1: Update the Repository
Before installing Node.js, ensure the repository pulls the latest version of the package containing Node.js. To do that, update system repositories with:
sudo apt update
Next, upgrade packages to the latest version with:
sudo apt upgrade
Step 2: Install Node.js
Once the repository has been updated, proceed with the installation. Use apt install
to install Node.js and NPM, the default package manager for Node.js.
Execute the following command:
sudo apt install nodejs npm -y
Step 3: Verify the Installation
Run -v
to check the version of Node.js and NPM, verifying that packages have been installed:
<code>node -v
Verify the NPM installation with:
npm -v
Method 2: Install Node.js via NodeSource PPA
The latest version of Node.js might not be available in Debian's repository. If that's the case, to get the latest version of Node.js use the PPA repository by NodeSource. PPA is an alternative repository, providing software not available in the official Debian repositories.
Step 1: Install the curl Command
Installing Node.js from PPA requires the use of curl. To install the latest version of curl
, run:
sudo apt install curl
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
Step 2: Add PPA to the System
Add PPA to the system with:
<code>curl -sL https://deb.nodesource.com/setup_18.x | sudo -E bash -
Note: NodeSource offers different Node versions. The example uses 18.x. To download another version, replace setup_18.x with the version in question.
Step 3: Update Repository
Once the system adds the PPA, update the repository and upgrade packages:
<code>sudo apt update
sudo apt upgrade
Step 4: Install Node.js
When installing from the PPA, Node.js and NPM come in the same package. Install both with:
<code>sudo apt install nodejs
Step 5: Verify the Installation
To confirm Node.js and NPM are installed correctly, check their versions with -v
:
node -v
npm -v
Method 3: Install Node.js via NVM
Another way to install Node.js is with the Node Version Manager (NVM).
NVM manages active Node.js versions. With NVM, users are able to download and switch between multiple Node.js versions using the terminal.
Step 1: Get the Installer Script
To install NVM, download the installer from GitHub first. Run the following command:
curl https://raw.githubusercontent.com/creationix/nvm/master/install.sh | bash
Step 2: Reload the Bash Profile
Use one of two ways to add the path to the NVM script in the current shell session:
- Restart the terminal.
- Run the source
~/.profile
command.
Step 3: List Node.js Versions
Use the following command to list all available Node.js versions:
nvm ls-remote
The command prints all available versions, allowing users to choose and install more than one.
Step 4: Install Node.js
Use nvm install
to download and install any specific version. For instance, to install version 12.1, run:
nvm install 12.1
Once the system installs Node.js, the installed version is the one used. Verify the version currently in use with:
node -v
Step 5: Switch Versions
NVM allows users to download and install multiple versions. For instance, download and install versions 18.1 and 16.5 as well:
To list all installed versions, run nvm
with ls command:
nvm ls
The command shows three versions, with the currently active one printed in green.
To switch to another version, use:
nvm use 18.1
The ls
output also shows that the default version is 12.1.
To change the default Node.js version, run:
nvm alias default 18.1
Conclusion
After reading this tutorial, you know how to install Node.js and NVM on Debian. Next, learn how to update Node.js to the latest version or how to build a Node.js app with Docker.