How to install Node.js on Ubuntu 18.04

0
293
How to install Node.js on Ubuntu 18.04
How to install Node.js on Ubuntu 18.04

This tutorial explains how to install Node.js and npm on a Ubuntu 18.04 machine. Node.js is an open-source, cross-platform JavaScript run-time environment that allows server-side execution of JavaScript code, which simply means that you can run JavaScript code on your machine as a standalone application, free of any web browser.

Node.js is mainly used in the back-end, but it is also popular as a full-stack and front-end solution. The default package manager for Node.js npm, the world’s largest software registry.

I. Install Node.js on Ubuntu

There are several different ways to install Node.js on an Ubuntu 18.04 machine. In this tutorial, we will show you three different ways to install Node.js; personally, I prefer to install Node.js using the NVM script.

1. Install Node.js from the Ubuntu repository

The Node.js package is available from the Ubuntu 18.04 distribution repository. At the time of this writing, the version in the repositories is v8.10.0 which may not always be the latest version.

As always, before installing a package with apt, first we need to refresh the packages index and then install the Node.js package by typing:

sudo apt update
sudo apt install nodejs
Install the Node.js package
Install the Node.js package

Then, verify the installation by typing:

nodejs --version
v8.10.0
Verify the installation NodeJS
Verify the installation NodeJS

The Node.js executable from the Ubuntu repositories is named nodejs instead of node because of a conflict with another package.

To be able to download npm packages, we also need to install npm, the Node.js package manager. We can do that by typing:

sudo apt install npm

“What is npm?”

Install npm, the Node.js package manager
Install npm, the Node.js package manager

2. Install Node.js from the NodeSource repository

NodeSource is a company focused on providing enterprise-grade Node support, and they maintain a repository containing the latest versions of Node.js.

To enable the NodeSource repository, type the following command:

curl -sL https://deb.nodesource.com/setup_8.x | sudo bash -
Enable the NodeSource repository
Enable the NodeSource repository

The current LTS version of Node.js is version 8.x, Carbon. If you need to install version 10.x, just replace setup_8.x with setup_10.x in the command.

Once the NodeSource repository is enabled, install Node.js and npm by typing:

sudo apt install nodejs
Install Node.js and npm
Install Node.js and npm

Check if the installation was successful by printing the Node.js and npm versions:

node –version
v8.11.1
npm –version
5.6.0

3. Install Node.js using NVM

NVM (Node Version Manager) is a bash script used to manage multiple active Node.js versions. With NVM, we can install and uninstall any specific Node.js version, allowing us to have any number of Node.js versions that we may want to use or test. To download the nvm install script, type:

What is NVM?

curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.11/install.sh | bash

The script clones the nvm repository from Github to ~/.nvm and adds the script path to your Bash or ZSH profile.

Download the nvm install script
Download the nvm install script

Reload system environment using this command. It will set the required environment variables to use nvm on the system.

source ~/.profile     ## Debian based systems 
source ~/.bashrc      ## CentOS/RHEL systems

As the output above says, we need to either close and reopen the terminal or run the commands to add the path to the nvm script to our current session. You can do whichever is easier for you.
To verify that nvm was properly installed, type:

nvm --version
0.33.11
Verify that nvm was properly installed
Verify that nvm was properly installed

Now that we have nvm installed, we can install the latest available version of Node.js by typing:

nvm install node
Install the latest available version of Nodejs
Install the latest available version of Nodejs

Let’s install two more versions, the latest LTS version and version 4.9.1:

nvm install --lts
nvm install 4.9.1
Let’s install two more versions
Let’s install two more versions

We can verify the Node.js version, by typing:

node --version
v4.9.1
Verify the Node.js version
Verify the Node.js version

Once LTS version and 4.9.1 are installed, we can list all installed Node.js instances by typing:

nvm ls
List all installed Node.js instances
List all installed Node.js instances

The output tells us that the entry with an arrow to its left (-> v4.9.1) is the version used in the current shell session, and the default version is set to v10.1.0. The default version is the version that will be active when opening new shells.
We change the current active version with:

nvm use 12.13.0

and verify it by typing:

nvm current
12.13.0

In case you want to set version 8.11.1 as a default Node.js version, you can do that with the following command:

nvm alias default 12.13.0
Change the current active version
Change the current active version

II. Install development tools

We need to install the development tools so that we are able to build native addons from npm. To install all the necessary packages, type:

sudo apt install gcc g++ make
Install development tools
Install development tools

III. Uninstall Node.js

If for some reason you want to uninstall the Node.js and npm packages, you can use the following command:

sudo apt remove nodejs npm

IV. Conclusion

We have shown you three different ways to install Node.js and npm on your Ubuntu 18.04 server. The method you choose will depend on your requirements and preferences.

While installing the packaged version from the Ubuntu or NodeSource repository is easier, the nvm method gives you more flexibility for adding and removing different Node.js versions on a per-user basis.

[junkie-alert style=”green”] If you have any questions or feedback, feel free to comment below. [/junkie-alert]

One more thing..

Share this tutorial with your hosting administrators and networking experts friends, as it will help them and make my countless hours of work count.

Thanks!

LEAVE A REPLY

Please enter your comment!
Please enter your name here