Introduction
Node.js is a free, open-source JavaScript run-time environment designed to implement JavaScript code on the server-side via asynchronous programming. It is a cross-platform platform that makes it easy for administrators to develop network applications; create dynamic page content; collect form data; and add, modify, or delete data in the database.
This tutorial will help you install Node.js on your Debian 9 system using some popular technique; using an apt package manager, from the official NodeSource repository, and using the Node.js Version Manager (NVM).
Ready? Let’s get started!
Step 1 – Installing Node.js Using An Package Manager
Debian’s default repository comes with a distro-stable Node.js version. The version is a stable option for a trial but may not be ideal for production and development uses cases. Nonetheless, if you feel this package suits your need, you can go ahead and install it.
In this section, we’ll utilize an apt package manager to install Node.js on Debian 9. First, execute the command below to update the package index:
$ sudo sudo apt update
Then, run the command to deploy the application (Node.js) from the Debian’s default repositories.
$ sudo apt install nodejs
Next, check the version of the Node.js application:
$ nodejs -v
Note: We are using Nodejs and not node to check the version of the newly installed Node.js. This prevents the conflict that may occur between the two executables from the official Debian repository.
If you are comfortable with the version you installed, then, that’s all you need to get started with Node.js on your Debian system. However, if you want to try other package archives, versions, and version managers, then, you will have to implement the most recent versions using the NVM or from the NodeSource repository.
Step 2 – Installing Node.js Using A Personal Package Archive (PPA)
In this step, we’ll help you install Node.js using the personal package archive (PPA) which is maintained in the NodeSource repository. This will give you the flexibility to select the most recent Node.js versions including Node.js v4.x, Node.js v6.x, Node.js v8.x, and Node.js v10.x.
First, execute the commands below to update your package index:
$ sudo sudo apt update
Next, install curl; which help you access the NodeSource repositories.
$ sudo sudo apt install curl
Now, run the command below from the home directory to incorporate NodeSource repository into your Debian 9 system.
$cd ~ $ curl -sL https://deb.nodesource.com/setup_10.x -o nodesource_setup.sh
Here, you are using curl to get the required installation script for Node.js version 10.x. This is an ideal version to install since it’s supported until 2021. However, if you are not comfortable with this version, feel free to replace it with another one such as Node.js v8.x which is the latest LTS version.
To scrutinize the content of this installation script, execute the command below:
$ nano nodesource_setup.sh
Next, run the setup script using sudo:
$ sudo bash nodesource_setup.sh
Once that is done, you can now install Node.js:
$ sudo apt install nodejs
Now, execute the command below to check the version you have already installed:
$ nodejs -v
This will give you the output below:
V10.9.0
Node.js package is complete and it comes with both nodejs binary and the Node.js Package Manager (NPM). For this reason, you don’t have to install NPM separately. Simply, run the command below to confirm if Node.js Package Manager is installed automatically:
$ npm -v
If npm was successfully installed, you will get the output below:
6.2.0
Next, you will have to install a build-essential application package to streamline the operation of some of the packages in npm.
$ sudo apt installbuild-essential
That’s it!
Step 3 – Installing Node.js Using The NVM
If you are not comfortable with these two methods, you can try installing Node.js using the Node.js Version Manager. The NVM functions as a stand-alone directory of the home directory, giving you the freedom to implement numerous self-contained Node.js Versions. It gives you the option to work with the most recent Node.js versions and maintain older versions without impacting your system.
Note: The Debian versions you manage using NVM are different from the one managed using apt.
First, execute the command below to get the installation script for NVM from GitHub page.
$ curl -sL https://raw.githubusercontent.com/creationix/nvm/v0.33.11/install.sh -o install_nvm.sh
You can inspect this script using any text editor:
$ nano install_nvm.sh
Next, execute the installation script using bash:
$ bash install_nvm.sh
This will install Node.js into a unique subdirectory of the directory at ~/.nvm. Besides, it will incorporate the required lines into the ~/.profile file. Now, execute the command below to find the file ~/.profile and enable the current session to recognize these changes:
$source ~/.profile
By now, the Node.js Version Manager is successfully installed and the stage is set for Node.js installation. But first, run the command below obtain a list of the available Node.js version;
$ nvm ls-remote
This will give you the output below:
... v8.11.1 (LatestLTS: Carbon) v9.0.0 v9.1.0 v9.2.0 v9.2.1 v9.3.0 v9.4.0 v9.5.0 v9.6.0 v9.6.1 v9.7.0 v9.7.1 v9.8.0 v9.9.0 v9.10.0 v9.10.1 v9.11.0 v9.11.1 v10.0.0 v10.1.0 v10.2.0 v10.2.1 v10.3.0 v10.4.0 v10.4.1 v10.5.0 v10.6.0 v10.7.0 v10.8.0 v10.9.0
Now, you can execute the command below to install v8.11.1; the current version for LTS.
$ nvm install 8.11.1
Next, run the command below to instruct nvm to utilize v8.11.1:
$ nvm use 8.11.1
You can use the command below to check the version being utilized by the current shell:
$ node -v
Note: The executable here is node and not the nodejs as used in the previous step. The command will give you the output:
v8.11.1
If you already have multiple versions of Node.js installed on your system, you can execute the command below to get the list:
$ nvm ls
To make one of these versions such as v8.11.1your default applications:
$ nvm alias default 8.11.1
Next, you can direct nvm to use the default version everytime a session is opened:
$ nvm usedefault
Each Node.js version installed via nvm manages its unique packages and comes with a Package manager to maintain these packages.
You can instruct npm to install nvm packages on the directory ./node_modules, which is the official directory for Node.js projects. First, run the command below to install an express module:
$ npm install express
If you wish to implement the express module globally and avail it to all projects using one Node.js version, simply add a -g to the command:
$ npm install -g express
The package will be installed in:
~/.nvm/versions/node/node_version/lib/node_modules/express
Next, run the command below to connect the express module to local sphere:
$ npm link express
If you need more details regarding nvm, run the command below to get help:
$ nvm help
Step 4 – Uninstalling Node.js
The method you use to remove Node.js depends on your version of interest and the technique used to install it. Here, there are two options:
To remove a Node.js version implemented from the NodeSource repository, you will use an apt utility. Run the command below to uninstall Node.js using apt:
$ sudo apt remove nodejs
Alternatively, to uninstall Node.js version installed using the nvm follow the steps below:
- First, execute the command below to check your current active Node.js version.
$ nvm current
- To remove a Node.js version that is not currently active, execute the command below:
$ nvm uninstall node_version
- Now, to remove the currently active Node.js version, first deactivate the nvm
$ nvm deactivate
- Next, run an uninstall command to remove the files linked to the version of interest.
Conclusion
Congratulation! You have learned how to install and uninstall Node.js on your Debian system. The tutorial detailed three methods that can be utilized to install this application. The ball is in your court now! Evaluate your circumstance and go for the method that best suits your needs.
Check out these top 3 Node.js hosting services:
- Want to get top recommendations about best hosting? Just click this link!