First, connect to your server via an SSH connection. If you haven’t done so yet, following our guide is recommended to connect securely with SSH. In case of a local server, go to the next step and open the terminal of your server.
Install Node.js via apt and make sure to download the latest version. To do so, update your repositories as follows:
$ sudo apt update && apt install nodejs
Then, check the correct installation of Node.js using the command
If the installation was successful, then the installed version will be displayed on the screen.
Now, let's move on to the installation of NPM, or Node Package Manager, which will be used to install additional modules for Node.js:
Install Node.js through NodeSource
A more recent version of Node.js can also be installed from the NodeSource repository. In this case, download the automatic script to add the NodeSource repository to your configuration.
Use curl to download the script. In this case, download the script related to version 10 of Node.js:
$ curl -sL https://deb.nodesource.com/setup_10.x -o nodesource_setup.sh
Then run the newly downloaded script:
$ sudo bash nodesource_setup.sh
At this point, the NodeSource repository has already been added. Proceed with the installation of Node.js:
$ sudo apt install nodejs
Then verify the successful installation of Node.js using the command:
If the installation was successful, the installed version will be displayed on the screen.
Also, check if the installation of NPM was successful by typing:
As before, if the NPM version appears on the screen, the installation was successful.
Installing Node.js through NVM
Another way to install Node.js is by using NVM or the Node.js Version Manager. NVM allows you to install and manage multiple versions of Node.js on the same system.
To install NVM, download its installation script from the official project page on GitHub. So let's proceed with the download via curl:
$ curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.34.0/install.sh
Then, run the newly downloaded script:
To complete the installation of NVM, login again or reload the .profile file in this way:
The installation is now completed.
To retrieve the list of the available versions for the installation, type the following command:
A list similar to the following will be displayed :
v8.11.1 (Latest LTS: Carbon)
To install node.js, all you need to do is choose the version you prefer and run the command:
by replacing 10.0.0 with the version number you prefer.
To use the newly downloaded version, use the nvm use command to select it, as follows:
At this point, check if the current version of Node.js matches the one just selected:
Removing Node.js via apt
Node.js can be easily removed by using apt's remove command, as follows:
While remove the associated configuration files, use the purge command:
Finally, remove all the packages installed automatically by Node.js.
Remove Node.js via NVM
To remove Node.js via NVM, use the corresponding ‘uninstal’l command followed by the version number of Node.js you want to uninstall:
$ nvm uninstall <node_version>
N.B. If the version you want to uninstall is the currently active one, disable it before uninstalling: