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.
- A Debian 8 server with a sudo-capable user who is not root.
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:
Also, execute the script with sudo:
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:
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):
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:
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:
Using nano, examine the installation script:
Run bash to execute the script:
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:
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.
v6.9.0 (LTS: Boron)
v6.9.1 (LTS: Boron)
v6.9.2 (Latest LTS: Boron)
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.
The following output will appear:
Computing checksum with sha256sum
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.
Entering the following command will display the shell’s current version:
You may check which Node.js version is installed if you have several of them by typing:
You can type: to set one of the versions as the default.
When you start a new terminal session, this version is automatically chosen. Additionally, you can refer to it by its pseudonym as follows:
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 ./node modules directory of the Node.js project. For the express module, as an illustration:
You can add the -g flag to install it globally so that it is accessible to other projects using the same Node.js version:
The package will be installed in:
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:
By typing: You can find out more about the choices you have with nvm.
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.