This guide will prompt you on a few possible ways to get Node.js installed on a Debian 10 server using a PPA repository, NVM (Node Version Manager), or from the default Debian repository.
Lastly, the guide will show how to uninstall these versions of Node.
How to Install the Official Debian Node.js Package
Node.js is contained in Debian default repositories. At the moment, this version is 10.15.2, and it will be supported until April 1, 2021. So, to try a long-term stable option, which is easy-to-install, use the Debian repo for Node.js installation.
Thus, use the apt package manager to retrieve Node.js from the default Debian repository. First of all, update your local package index:
$ sudo apt update
After that, install the Node.js package and npm (Node Package Manager):
$ sudo apt install nodejs npm
To check if the installation was completed successfully, type the node command with the -v flag:
$ node -v
In case you want a more recent version of Node.js, see the next two sections of our guide.
How to Install Node.js Using a PPA
For a more up-to-date version of Node.js, install from a personal package archive (PPA) that is maintained by NodeSource. This repository works with `apt, and provides more recent versions of Node.js unlike the official repositories of Debian. NodeSource offers PPAs available from 0.10 through to 12 Node versions.
Installing the PPA will add the repository to the package list and let us install the new packages using apt.
Use curl from your home directory to get the preferred Node.js installation script. However, please, make sure to replace 12.x with the version string of your choice (if different):
$ cd ~ $ curl -sL https://deb.nodesource.com/setup_12.x -o nodesource_setup.sh
Use nano or any other text editor you prefer check the contents of this script:
$ nano nodesource_setup.sh
Having inspected the content, exit the text editor and use sudo to run the script :
$ sudo bash nodesource_setup.sh
Since npm package is included in the nodejs package, there is no need to install it separately.
Run node with the -v version option to check on the installation process:
$ node -v
The configuration file, aimed to to keep track of updates, will be created in your home directory the first time you run npm npm. Run the command below to make sure that npm is successfully installed and to create the configuration file:
$ npm -v
Some npm packages require compiling code from source, so you need to install the build-essential package for them to work properly:
$ sudo apt install build-essential
At the moment, all the necessary tools for npm packages that require compiling code from source are available.
How to Install Using NVM
Except apt, Node.js can be installed using a tool called nvm. It stands for “Node Version Manager”. Its specification is that, unlike apt, which works at the operating system level, nvm operates within your user’s home directory at the level of an independent directory. It allows installing multiple self-contained versions of Node.js with no load to the entire system.
Moreover, nvm allows you to access the latest Node.js releases at the same time retaining and managing its previous versions. nvm differs from apt; thus, those Node.js versions that can be managed with nvm are distinct from those managed by apt.
Use curl to download the nvm installation script from the project’s GitHub page. Please, not the version highlighted here may be different:
curl -sL https://raw.githubusercontent.com/nvm-sh/nvm/v0.34.0/install.sh -o install_nvm.sh
Use nano to check the installation script:
$ nano install_nvm.sh
If it looks OK, leave the text editor you are using and run the script with bash:
$ bash install_nvm.sh
Due to the fact that nvm is not installed into any privileged system directories, sudo is not needed.The software will be installed into a subdirectory of your home directory at ~/.nvm. some configurations will be added to your ~/.profile file to enable the new software.
To access the nvm functionality, source the ~/.profile file or either log out and log in again to introduce the changes to your current session:
$ source ~/.profile
Having installed nvm, you can proceed to installing isolated versions of Node.js. To see the available versions of Node.js, type:
$ nvm ls-remote
Output . . . v10.16.2 (Latest LTS: Dubnium) v11.0.0 v11.1.0 v11.2.0 v11.3.0 v11.4.0 v11.5.0 v11.6.0 v11.7.0 v11.8.0 v11.9.0 v11.10.0 v11.10.1 v11.11.0 v11.12.0 v11.13.0 v11.14.0 v11.15.0 v12.0.0 v12.1.0 v12.2.0 v12.3.0 v12.3.1 v12.4.0 v12.5.0 v12.6.0 v12.7.0 v12.8.0
As seen from the example, the current LTS version is v10.16.2. To install it, type:
nvm install 10.16.2
nvm is set up to switch to use the version that is the most recent. To tell nvm to use the version that was just downloaded, type:
$ nvm use 10.16.2
To check the version of Node.js that is being used, type:
$ node -v
To see the version installed from the multiple Node.js versions (if you have many), type:
$ nvm ls
To default of the versions, type the command below:
$ nvm alias default 10.16.2
The version you set up as default will be automatically selected when a new session spawns. To reference it, type:
$ nvm use default
Every version of Node.js has npm to manage its packages and keeps track of them.
How to Remove Node.js
Use apt or nvm to uninstall Node, but it depends on the version you target. You need to use apt that works the system level to remove versions installed from the PPA or Debian repository.
In order to remove either of these versions, type the command below:
$ sudo apt remove nodejs
With this command you will remove both the configuration files and the package.
If you need to remove Node.js enabled by nvm, you have to determine if it is the current active version:
$ nvm current
In case you target a version that is not current active version, type:
$ nvm uninstall node_version
Using this command, you will uninstall the selected version of Node.js.
However, if the version to remove is the current active version, first of all, deactivate nvm to enable the changes:
$ nvm deactivate
Now, you can uninstall the current version. For that, youneed to use the uninstall command above. This command will remove all files associated with the targeted Node.js version but for the cached files ( these files can used for reinstallation).