Here at LinuxAPT, we shall look into the different ways of installing Node.JS & NPM on Debian 11.
Node.js and npm are available in the default debian repositories. They can simply be installed with the commands:
$ sudo apt update
Using the APT package manager, install NodeJS and NPM:
$ sudo apt install nodejs npm
This install Node.JS alongside a bunch of libraries and dependencies.
You can build and compile Node.js from source. This is the best option if you wish to install a specific version of Node.js. Here, we are going to install Node.js from source. First, add the repository to Debian 11 with the command:
$ curl -fsSL https://deb.nodesource.com/setup_17.x | sudo -E bash -
Here's an snippet of the output. The setup script add the GPG signing key and proceeds to create a sources list file for the Node.JS repository in the /etc/apt/sources.list file.
Then it updates the package index to sync with the newly appended Node.JS repository.
At the tail-end of the output, you are provided with the next steps to take – which is to install Node.JS and NPM.
So, go ahead and install Node.JS 17 and npm with the command:
$ sudo apt install nodejs
As before, this installs NodeJS alongside a bucketload of other libraries and dependencies. To confirm NodeJS is installed, run:
$ node —-version
Also, you can verify npm version:
$ npm —-version
If you are interested in installing Yarn package manager, first add the GPG key:
$ curl -sL https://dl.yarnpkg.com/debian/pubkey.gpg | gpg --dearmor | sudo tee /usr/share/keyrings/yarnkey.gpg >/dev/null
Then add the Yarn repository to the sources list file:
$ echo "deb [signed-by=/usr/share/keyrings/yarnkey.gpg] https://dl.yarnpkg.com/debian stable main" | sudo tee /etc/apt/sources.list.d/yarn.list
Finally, update the package index to refresh the repositories:
$ sudo apt-get update
Thereafter, as instructed, install Yarn using APT package manager:
$ sudo apt install yarn
To remove Node.JS 17, use the following command:
$ sudo apt remove nodejs -y
Next, remove the repository for complete removal.
$ sudo rm /etc/apt/sources.list.d/nodesource.list