How to install Node.js & NPM on Debian 11?
Node.js is open-source and cross-platform JavaScript runtime environment which is built on Chrome’s V8 JavaScript engine. It features non-blocking, event-driven servers which are mainly used for back-end API services and traditional websites.
This blog explains three ways to install Node.js and npm on a Debian 11 server.
Pre-requisites
Before we begin to install, make sure these requirements are met:
- Debian 11 VPS
- Access to the root user account (or access to an admin account with root privileges)
Installation of NodeJS with apt
1. To install NodeJS and NPM on Debian 11, run the apt install command below.
sudo apt install nodejs npm -y
2. To verify if NodeJS and NPM are installed correctly, run the below commands.
node -v v12.22.5 npm -v 7.5.2
Installation of NodeJS with PPA
PPA is another repository for software packages. It offers software that is not available in the official Debian 11 repositories.
1. Add the PPA repository to the system by using the below command. and replace setup_17.x with the updated version of NodeJS.
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 the source lists, to add the new repository that was recently added with the below command.
sudo apt update -y
3. Then, install NodeJS and NPM using the following command.
sudo apt install nodejs -y
4. Check if NodeJS and NPM are installed correctly using the below commands.
node -v v17.4.0 $ npm -v 8.3.1
Installation of NodeJS with NVM
Node Version Manager(NVM) is a bash script. It is used to maintain multiple active NodeJS versions on the same machine. Through this method multiple versions of NodeJS can be used.
1. 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. To reload the environment variables in the current session, run the below command
source ~/.profile
3. List the available versions of NodeJS.
nvm ls-remote
4. Once the version is finalized, run the below command to download and install nvm install <version>:
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. Run the below command to list the installed NodeJS versions. NNM indicates the version that is the default.
Installation of NodeJS is successful. You can use the node command to run NodeJS apps within any of these installed versions and switch between the NodeJS versions using NVM.
For more latest information and updates refer to these official pages: NodeJS documentation, NodeJS GitHub page, NodeSource documentation page, NPM documentation.