install Node.js on Ubuntu

How to install Node.js on Ubuntu 18.04

Node.js is an open-source platform that allows programmers to build dynamic web pages and applications. It is used widely for server-side scripting and allows users to use JavaScript for both client and server-side scripts. In this tutorial, you will learn how to install Node.js on Ubuntu 18.04 and get started with it.

Prerequisites

First, you have to create a machine. You can do that easily on CloudSigma WebApp’s compute section:
Create a Linux Server setup java

For the purposes of this tutorial, you can use the following resources:
CPU: 8 GHz
RAM: 8 GB
SSD: 50 GB

You can mount the disk with Ubuntu 18.04 LTS image available in CloudSigma’s library:
Ubuntu 18.04: Pre-installed 64bit with VirtIO drivers, superuser, Python 2.7.17, Pip 20.0.2 OpenSSL 1.1.1d, Cloud-init and latest updates until 2019-03-03.

After starting the machine, you can update all the existing repos and packages on the machine by running the following commands:

Installing the Stable Version for Ubuntu

In this section, you will install Node.js on Ubuntu 18.04 using ‘apt’ command. Ubuntu has a stable version of Node.js in its repositories. The stable version provides a more consistent experience across multiple systems. To install the stable version from Ubuntu’s repositories, you can run the following command:

After the installation, you should also install ‘npm’. NPM – Node Package Manager. This is a package manager for the javascript programming languages. It would allow you to manage and install the node.js packages and the modules. To install NPM, go ahead and run the following command:

You can check the version of Node.js installed on your server using the following command:

node js version check

You can also install it in another more robust way. You will learn how to do that in the following section.

Installing using PPA – Personal Package Archive

NodeSource maintains PPA – Personal Package Archive which allows you to get a more recent version of node.js. It will give you more freedom to choose the version of node.js you want to work on.

For this tutorial, I will use the commands for Node.js v14. First, you can install PPA using the following command:

To install any other version, just change the version number in the command. For example, if you wish to install v10, you can edit the version similar to this:

The above commands add PPA to the configuration and update the local package cache. The second command installs Node.js. To check the version, run the following command:

install Node.js on Ubuntu Node js Version Check

You don’t need to install NPM separately as the above-installed package already contains it. NPM keeps track of updates using a configuration file in your home directory. To check if NPM has been installed, you can run the following command:

npm version check

Some NPM packages require build-essentials to compile code from source. You can install them using the following command:

Installing Using NVM  – Node.js Version Manager

Another way to install Node.js is to use NVM – “Node.js Version Manager”. NVM works on the directory level instead of the system level which means you can have various versions installed in various directories without affecting the entire system.

NVM allows you to use the new versions while maintaining the old releases of Node.js. Node.js installed by apt and NPM are managed differently.

To install NVM, go to the Github page of the project and check the version you require. In this tutorial, I am using version vo.35

The above command installs NVM in ~/.nvm. Moreover, it adds the required lines of script to ~/.bashrc

You can log out and log in, or source the file “~/.bashrc” to load the changes to the current session.

You can install the various versions of Node.js using NVM. To check the available versions of Node.js, run the following command:

You can install the desired version using the following command:

install Node.js on Ubuntu nvm install

You can check the version using:

NVM will generally use the last installed version but you can also specify it using the ‘ncm use’ command:

You can manage multiple Nodejs versions. To see the versions installed, use the following command:

To make a version the default one, you can alias it. Whenever a new session is created, the default version of node.js will be used.

You can also reference it as per below:

Each version of Node.js keeps track of its packages. While installing packages through NPM, you can specify whether to install them for the particular version of Node.js or globally.

To install the package for the particular version in use, run this command:

To install it globally, add the ‘-g’ flag,

After installing the package globally, you’ll be able to run commands but for your program to use it, you will have to link the package into your local sphere.

You can find more options available with NVM by executing this command:

 

Removing Node.js

You can either uninstall node.js by using ‘apt’ or by using NVM.

To remove the stable version you installed with ‘apt’, use this command:

The above command uninstalls the node.js, but keeps the configuration files. If you want to uninstall node.js and remove the configuration files as well, use the command:

Moreover, to remove the packages that were automatically installed while installing some of the removed packages, use the command:

To uninstall the versions you installed using NVM, use this command:

If the version you want to uninstall is the current version, deactivate it first and then run the uninstall command:

The above commands would remove all files associated with the version excluding the cached files.

Now that you’ve completed this tutorial, you know 3 different and easy ways of installing Node.js on Ubuntu 18.04 server.

Happy Computing!