How to install Node.js & NPM on Debian 11

Updated on November 21, 2023
How to install Node.js & NPM on Debian 11 header image

Node.js & NPM are now available on Debian 11 Bullseye. Node.js is a JavaScript runtime built on Chrome's V8 JavaScript engine. It uses an event-driven, non-blocking I/O model that makes it lightweight and efficient. Node.js applications are written in JavaScript and run on macOS, Windows, and Linux operating systems.

Node.js is the foundation for creating fast, scalable network applications. With NPM (Node Package Manager), you have access to over 600,000 packages of reusable code that can help you build amazing things with Node.js.

Prerequisites

Installing NodeJS with apt

The Debian 11 "Bullseye" repo contains a pre-built NodeJS package, and this is an easy way to get NodeJS up and running without any fuss. However, the version of NodeJS provided is not the latest version, and there is no guarantee that it will continue to be updated.

  1. Run the apt install command below to install NodeJS and NPM on your Debian 11 system.

     $ sudo apt install nodejs npm -y
  2. Run the following commands to verify that NodeJS and NPM are installed properly.

     $ node -v
     v12.22.5
     $ npm -v
     7.5.2

Installing NodeJS with PPA

You can also install the latest version of NodeJS using the PPA maintained by NodeSource, a company that builds and maintains various packages for different Linux distributions. PPA is an alternative repository for software packages. It provides software that is not available in the official Debian 11 repositories.

  1. Add the PPA repository to your system using the following command. As if this writing, NodeJS 17.x is the latest stable version, which is very recent than the version in Debian 11 repo (12.x). Replace setup_17.x with thelatest versionof NodeJS available at the time of your installation.

     $ curl -fsSL https://deb.nodesource.com/setup_17.x | sudo -E bash -
     ## Installing the NodeSource Node.js 17.x repo...
     ## Populating apt-get cache...
     ## Creating apt sources list file for the NodeSource Node.js 17.x repo...
  2. Update your source lists to include the new repository just added with the following command.

     $ sudo apt update -y
  3. Finally, install NodeJS and NPM using the following command. You don't need to specify the NPM package here since the package is already part of the NodeJS package.

     $ sudo apt install nodejs -y
  4. Verify that NodeJS and NPM are installed properly using the following commands. You will get the following output, indicating the version number of the packages installed(v17.4 and 8.3.1).

     $ node -v
     v17.4.0
     $ npm -v
     8.3.1

Installing NodeJS with NVM

The Node Version Manager(NVM) is a bash script used to manage multiple active NodeJS versions on the same machine. You can easily switch between different NodeJS versions. Using this method, you will be able to use multiple versions of NodeJS without worrying about compatibility issues.

  1. First, download the installer script from GitHub.

     $ curl https://raw.githubusercontent.com/creationix/nvm/master/install.sh | bash
     => Downloading nvm as script to '/root/.nvm'
     => Appending nvm source string to /root/.bashrc
     => Appending bash_completion source string to /root/.bashrc
  2. Run the source ~/.profile command to reload the environment variables into your current session.

     $ source ~/.profile
  3. List the available versions of NodeJS.

     $ nvm ls-remote
  4. Once you have decided on the version, run nvm install <version> command to download and install it. For example, to install NodeJS 11.6, runs:

     $ nvm install 11.6
     Downloading and installing node v11.6.0...
     Downloading https://nodejs.org/dist/v11.6.0/node-v11.6.0-linux-x64.tar.xz...
     Computing checksum with sha256sum
     Checksums matched!
     Now using node v11.6.0 (npm v6.5.0-next.0)
  5. The latest version will be used if you don't explicitly specify a version number. You will need to tell NVM which version of NodeJS to use. For example, to use NodeJS 11, run:

     $ nvm use 11.6
     Now using node v11.6.0 (npm v6.5.0-next.0)
  6. Run the nvm ls command to list the installed NodeJS versions. NNM will also indicate which version is the default.

      $ nvm ls
     v11.6.0
     v11.7.0
     default -> 11.6 (-> v11.6.0)
  7. You can also set a specific version as the default NodeJS version. For example, to set version 11.7 as your default NodeJS version, runs:

     $ nvm alias default 11.7
     default -> 11.7 (-> v11.7.0)

Conclusion

You have installed NodeJS using several methods. At this point, you can use the node command to run your NodeJS application within any of these installed versions. You can also switch between NodeJS versions using NVM.

More Information

To learn more about the tools in this article, visit: