Tutorials > How to install NodeJs on Ubuntu 18.04

How to install NodeJs on Ubuntu 18.04

Published on: 16 January 2020

Development NodeJs Ubuntu

Node.js is an open source framework for the execution of the server-side Javascript code. Its efficiency is guaranteed by the Javascript V8 engine created by Google. In this guide you will be shown how to install Node.js on Ubuntu 18.04 with different methods.

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.

Installing Node.js

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

$ nodejs -v

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:

$ sudo apt install npm

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:

$ nodejs -v

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:

$ npm -v

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:

$ bash install.sh

To complete the installation of NVM, login again or reload the .profile file in this way:

$ source ~/.profile

The installation is now completed.

To retrieve the list of the available versions for the installation, type the following command:

$ nvm ls-remote

A list similar to the following will be displayed :

        v8.11.1   (Latest LTS: 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 

To install node.js, all you need to do is choose the version you prefer and run the command:

$ nvm install 10.0.0

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:

$ nvm use 10.0.0

At this point, check if the current version of Node.js matches the one just selected:

$ node -v

Removing Node.js via apt

Node.js can be easily removed by using apt's remove command, as follows:

$ sudo apt remove nodejs

While remove the associated configuration files, use the purge command:

$ sudo apt purge nodejs

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:

$ nvm deactivate