69 views0October 26, 2022Updated on February 1, 2023host_know_user
Introduction
Node.js is a general-purpose JavaScript platform that enables users to create network applications fast. The development process can be more streamlined and created within the same system by utilising JavaScript on both the front-end and the back-end.
You’ll set up Node.js on a Debian 8 server using this tutorial. You’ll look at two approaches to install the most recent version of Node.js on your machine because Debian 8 comes with an older version of Node.js in its default repository.
Prerequisites
A Debian 8 server with a sudo-capable user who is not root.
Installing Using a PPA
The PPA (personal package archive) run by NodeSource should be added to your server in order to acquire the most recent version of Node.js. When compared to the official Debian repositories, this will have more recent versions of Node.js. Additionally, it allows you to select between Node.js v4.x (the earlier long-term support version, supported until April 2017), v6.x (the more recent LTS version), and v7.x (the most recent LTS version, supported until April 2018). (the current actively developed version).
To access the PPA’s contents, you must first install it. Ascertain that you are in your home directory, then use curl to download the installation script for your desired version, making care to replace 6.x with the appropriate version string:
You can use nano (or your chosen text editor) to browse through the script’s contents:
# nano nodesource_setup.sh
Also, execute the script with sudo:
# sudo bash nodesource_setup.sh
Your configuration will include the PPA, and your local package cache will be immediately updated. You can install the Node.js package in the same way you did before after running the setup script from nodesource:
# sudo apt-get install nodejs
Npm is already installed as part of the nodejs package, thus there is no need to install it separately. However, the build-essential package must be installed in order for some npm packages to function (such as those that need code to be compiled from source):
# sudo apt-get install build-essential
How To Install Using nvm
Node.js version manager, or nvm, is a specially made utility that can be used as an alternative to installing Node.js via apt. You can install numerous, independent versions of Node.js using nvm, making it simpler for you to manage your environment. You will have on-demand access to the most recent Node.js releases, but you can also target earlier versions that your app might need.
We must first download the software packages from our Debian repository so that we can create source packages. These resources will be used by the nvm command to create the following components:
# sudo apt-get update
# sudo apt-get install build-essential libssl-dev
You can get the nvm installation script from the project’s GitHub page after installing the needed packages. Although the version number may vary, generally speaking you can download it using curl:
The software will be installed into a subdirectory located at ~/.nvm in your home directory. Additionally, it will include the lines in your ~/.profile file that are required to enable the nvm command.
You’ll need to log out and log back in again to utilise the nvm command and its features, or you can source the ~/.profile file so that your active session is aware of the changes:
# source ~/.profile
You can now install separate Node.js versions after installing nvm.
You can type: to get a list of the Node.js versions that are installable.
As you can see, the most recent long-term support release is v6.9.2, although the most recent version at the time of writing is v7.2.0. By typing: you can install that.
# nvm install 6.9.2
The following output will appear:
Output : Computing checksum with sha256sum Checksums matched! Now using node v6.9.2 (npm v3.10.9) Creating default alias: default -> 6.9.2 (-> v6.9.2)
Normally, nvm switches to the most recent installation. By typing: you can specifically instruct nvm to utilise the copy we just downloaded.
# nvm use 6.9.2
Entering the following command will display the shell’s current version:
# node –v
Output :
v6.9.2
You may check which Node.js version is installed if you have several of them by typing:
# nvm ls
You can type: to set one of the versions as the default.
# nvm alias default 6.9.2
When you start a new terminal session, this version is automatically chosen. Additionally, you can refer to it by its pseudonym as follows:
# nvm use default
Each Node.js version will maintain a list of its own packages, and npm is available to manage these.
Using the standard approach, you may instruct npm to install packages in the ./nodemodules directory of the Node.js project. For the express module, as an illustration:
# npm install express
You can add the -g flag to install it globally so that it is accessible to other projects using the same Node.js version:
# npm install -g express
The package will be installed in:
~/.nvm/node_version/lib/node_modules/package_name
If you install it locally, you can perform the commands from the command line; but, if you want to require it from within a programme, you must link the package into your local sphere:
# npm link express
By typing: You can find out more about the choices you have with nvm.
# nvm help
Conclusion
As you can see, there are numerous ways to install Node.js on your Debian 8 server and get it operating. Which of the aforementioned approaches is ideal for your situation will depend on your circumstances. The packaged version in the Ubuntu repository is the most straightforward, but the nvm approach is unquestionably far more flexible.