Kiến Thức Linux

How to Install Node.js on Debian

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.

How to Install Node.js on Debian

Prerequisites

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
sudo apt update terminal output

Next, upgrade packages to the latest version with:

sudo apt upgrade
sudo apt upgrade terminal output

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
sudo apt install Node.js NPM -y terminal output

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
node -v terminal output

Verify the NPM installation with:

npm -v
npm -v terminal output

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
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 with:

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

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 update PPA terminal output
sudo apt upgrade
sudo apt upgrade PPA terminal output

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
sudi apt install Node.js terminal output

Step 5: Verify the Installation

To confirm Node.js and NPM are installed correctly, check their versions with -v:

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 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
Download NVM installer terminal output

Step 2: Reload the Bash Profile

Use one of two ways to add the path to the NVM script in the current shell session:

  1. Restart the terminal.
  2. Run the source ~/.profile command.
Reload the bash profile terminal output

Step 3: List Node.js Versions

Use the following command to list all available Node.js versions:

nvm ls-remote
nvm ls remote terminal output

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
nvm install 12.1 terminal output

Once the system installs Node.js, the installed version is the one used. Verify the version currently in use with:

node -v
node -v NVM installation terminal output

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:

NVM install multiple versions terminal output

To list all installed versions, run nvm with ls command:

nvm ls
nvm ls terminal output

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

To switch to another version, use:

nvm use 18.1
nvm use terminal output

The ls output also shows that the default version is 12.1.

nvm ls default version terminal output

To change the default Node.js version, run:

nvm alias default 18.1
nvm alias default terminal output

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.

Đăng ký liền tay Nhận Ngay Bài Mới

Subscribe ngay

Cám ơn bạn đã đăng ký !

Lỗi đăng ký !

Add Comment

Click here to post a comment

Đăng ký liền tay
Nhận Ngay Bài Mới

Subscribe ngay

Cám ơn bạn đã đăng ký !

Lỗi đăng ký !