How to Install Node.js and npm on Debian 10

Choose a different version or distribution

Introduction

Before we begin talking about how to install Node.js and npm on Debian 10, first let's understand – What is Node.js and npm?

Node.js is a popular open-source JavaScript runtime environment that allows developers to run JavaScript code outside a web browser. It's used to build fast and scalable network applications.

npm, or Node Package Manager, is a powerful tool that comes with Node.js and helps developers manage and share reusable code packages. With npm, developers can easily install, update, and manage dependencies for their projects.

Together, Node.js and npm have revolutionized the way JavaScript is used on the server-side, enabling developers to build efficient and robust applications.

In this tutorial, you will install Node.js on Debian 10 Linux. We will also address a few FAQs on how to install Node.js and npm on Debian 10 Linux.

Advantages of Node.js and npm

  1. High-performance: Node.js is known for its event-driven architecture, which allows for high scalability and handling of concurrent requests efficiently.
  2. Large ecosystem: npm offers a vast collection of reusable code packages, making it easy to find and integrate existing solutions into projects.
  3. Speedy development: The combination of Node.js and npm enables rapid development with its streamlined package management and efficient workflow.
  4. Full-stack JavaScript: Developers can use a single language, JavaScript, for both front-end and back-end development, simplifying the development process.
  5. Community support: Node.js and npm have a thriving community that provides extensive documentation, tutorials, and support, ensuring developers have resources to rely on.

Install Node.js and npm from the Debian repositories

Installing Node.js and npm is possible using default Debian repositories. The version in the repositories is v10.x, which was the latest LTS version at the time of writing this tutorial.

Use the following commands to install Node.js and npm on your Debian system:

sudo apt update
sudo apt install nodejs npm

Once the installation is done, you can verify it by entering:

nodejs --version

The output will display the Node.js version:

Output

v10.15.2

The above method is the simplest way to install Node.js and npm on Debian and is sufficient for users in most cases.

Install Node.js and npm from the NodeSource repository

NodeSource is a firm dedicated to delivering enterprise-grade Node support. Several Node.js versions can be found in the APT repository of NodeSource.

If you want to install a specific Node.js version, use this repository. At the time of writing this tutorial, the NodeSource repository included the following versions:

  • v14.x - The most recent stable version.
  • v13.x
  • v12.x - The latest LTS version.
  • v10.x - The previous LTS version.

We will install Node.js 12.x in this tutorial.

Start by running the following curl command to add the NodeSource repository to your system:

curl -sL https://deb.nodesource.com/setup_12.x | sudo bash -

Once the repository has been added, enter the following command to install Node.js and npm:

sudo apt install nodejs

Check that Node.js is successfully installed by typing:

node --version
Output

v12.8.1

Install Node.js and npm using NVM

Multiple Node.js versions can be managed using the bash script NVM (Node Version Manager). You can install and remove any version of Node.js that you want to use or test using NVM.

If you want to install Node.js for each user individually, use this method.

Write the following command into your system to install NVM. Avoid using sudo since doing so will enable the script for the root user.

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

The installation script adds the nvm path to your Bash or ZSH profile and clones the nvm repository from GitHub to the ~/.nvm directory.

...
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"  # This loads nvm
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"  # This loads nvm bash_completion

Start a new shell session or enter the commands displayed on your screen to use the nvm script. Do whatever is most convenient for you.

Once the nvm script has been set up on your Debian system, use the following instructions to install the most recent stable version of Node.js:

nvm install node
Output

...
Computing checksum with sha256sum
Checksums matched!
Now using node v12.8.1 (npm v6.10.2)
Creating default alias: default -> node (-> v12.8.1)

Let us install version 8.16.0 and the most recent LTS version.

nvm install --lts
nvm install 8.16.0

After completion, type the following to list all installed Node.js versions:

nvm ls
Output

Copy
->      v8.16.0
       v10.16.2
        v12.8.1
default -> node (-> v12.8.1)
node -> stable (-> v12.8.1) (default)
stable -> 12.8 (-> v12.8.1) (default)
iojs -> N/A (default)
unstable -> N/A (default)
lts/* -> lts/dubnium (-> v10.16.2)
lts/argon -> v4.9.1 (-> N/A)
lts/boron -> v6.17.1 (-> N/A)
lts/carbon -> v8.16.0
lts/dubnium -> v10.16.2

The version used in the current shell session is indicated by the entry with an arrow to the right (-> v8.16.0), and the default version is set to v12.8.1. The default version is the one that is used when new shell sessions are opened.

If you want to switch to, say, v10.16.2 from the one that is currently in use, you would run:

nvm use 8.11.3

For instance, to switch to v10.16.2 from the default Node.js, use:

nvm alias default 8.11.3

Install Development Tools

The development tools are required for compiling and installing native add-ons from the npm registry. Execute the below command to install the package:

sudo apt install build-essential

Uninstall Node.js

If you need to uninstall Node.js and the npm packages for some reason, run the following command:

sudo apt remove nodejs npm

FAQs to Install Node.js and npm on Debian 10 Linux

What is the recommended version of Node.js for Debian 10?

It is recommended to install the LTS (Long-Term Support) version of Node.js for stability and long-term maintenance.

Can I install multiple versions of Node.js on Debian 10?

Yes, you can manage multiple Node.js versions using tools like nvm (Node Version Manager) or n. They allow you to switch between different Node.js versions easily.

How do I update Node.js and npm to the latest versions?

To update Node.js, you can use the package manager with the command sudo apt-get update && sudo apt-get upgrade. For npm, use npm install -g npm@latest to update.

How do I uninstall Node.js and npm from Debian 10?

To uninstall Node.js and npm, use the command sudo apt-get remove nodejs npm in the terminal.

Where can I find additional packages and modules for Node.js?

You can find a wide range of packages and modules on the npm website (https://www.npmjs.com) by searching for specific packages or browsing through different categories.

Can I use a different package manager instead of npm with Node.js on Debian 10?

While npm is the default package manager for Node.js, you can use alternatives like Yarn or pnpm if you prefer. They offer similar functionality and can be installed separately.

How can I fix permission issues when installing global npm packages on Debian 10?

You can avoid permission issues by using the --global flag with sudo when installing global npm packages. Alternatively, you can configure npm to install global packages locally.

Conclusion

In this tutorial, you installed Node.js and npm in three separate ways on your Debian 10 system. It is time to deploy your application now that Node.js has been set up on your Debian 10 system.

If you have any queries, please leave a comment below, and we’ll be happy to respond to them.