How to Install Node.js from Linux Terminal

Node.js is an open-source, free, cross-platform JavaScript run-time environment which is responsible for executing JavaScript code on server. It allows developers to use JavaScript for server-side scripting – running scripts server-side to produce dynamic web page content before the page is sent to the user’s web browser. Consequently, Node.js represents a «JavaScript everywhere» paradigm, unifying web application development around a single programming language, rather than different languages for server side and client side scripts.

In this tutorial, I will show you how to install the latest Nodejs on Ubuntu 18.04, CentOS 7, and Arch Linux from the terminal.

There are several ways to install Node.js on Linux machine and we are going to cover them in this article.

  • Install using Node Version Manager (NVM)
  • Install using the Github source code clone
  • Install the Distro-Stable version of Node.js

Install using Node Version Manager (NVM)

NVM doesn’t work at the operating system level, instead it works at the level of a directory within your user’s home directory. It allows installing multiple, self-contained versions of Node.js without affecting the entire system.

Ubuntu 18.04

Before installing NVM we update OS with the following command

sudo apt-get update

To use nvm, we must have curl, build-essentials libssl-dev packages installed on the system. To install them type

sudo apt-get install build-essential libssl-dev

Once all prerequisites are available we install nvm with the following command

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

The script above clones the nvm repository to ~/.nvm and also adds the source line to your profile (~/.bash_profile, ~/.profile or ~/.bashrc). That means we need to source the profile file. To do so, type the following command

source ~/.profile

Now nvm functionalities and binaries are available for use and we can get list of available Node.js versions via the following command

nvm ls-remote

Tail of output must look like the one below:

...

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

Choose the version you want to install and type the command below to install it.

nvm install v9.10.1

After installing the desired version we can set that version as default Node.js version (in case you have multiple installations)

nvm use v9.10.1

To check if version is installed and set to default successfully, just check Node.js version with the command below

node -v

or

nvm current

The output of this command must show the installed version.

To update/install another version you can run:

nvm install v10.0.0

Make sure to run the command with the version you want to install. After you can set the freshly installed version as your default with the following command:

nvm use v10.0.0

To remove node.js version via nvm, first check if it’s not the current version with the command below

nvm current

and run the following command to remove the desired version:

nvm remove v9.10.1

CentOS 7

The process of installation of nvm and node.js using nvm is the same as on Ubuntu 18.04 with few differences. Below you can find commands running which in the same order will install nvm, node.js and set the default version.

Update OS

sudo yum update

Install nvm

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

Source profile file (note that in CentOS it differs from the one in Ubuntu)

source ~/.bashrc

List available node.js versions

nvm ls-remote

Install the desired version (make sure to change with the version you want to install)

nvm install v9.10.1

Set as default node.js version

nvm use v9.10.1

Check if installation and default setting are succeeded

node -v

or

nvm current

To update/install another version you can run:

nvm install v10.0.0

Make sure to run the command with the version you want to install. After you can set the freshly installed version as your default with the following command:

nvm use v10.0.0

To remove node.js version via nvm, first check if it’s not the current version with the command below

nvm current

and run the following command to remove desired version:

nvm remove v9.10.1

Install using the Github source code clone

To install Node.js using release from official Github repository we need to check make sure the make, gcc, g++ and python packages are installed on Ubuntu 18.04 machine. If there are missing packages, you can run:

sudo apt-get install make gcc g++ python

On CentOS 7 all needed packages are preinstalled in OS. You can run OS update command to make sure they are latest versions:

sudo yum update

The rest part of installation using GitHub repository’s official release is the same for both distributions.

Download the desired release (e.g. v6.2.1) from Github Repo using the following command:

 wget https://github.com/nodejs/node/archive/v6.2.1.tar.gz

Extract the content of the archive using the command below

tar zxvf v6.2.1.tar.gz

Enter the directory with extracted files

cd cd node-6.2.1

and run the following command to compile

./configure

After compilation you can now install node.js using the following command:

sudo make install

The process of installation can take little bit longer this way. It depends on resources of your machine.

Install the Distro-Stable version of Node.js

In default repositories of Linux distributions, there are versions of Node.js and you can install them using any package manager of the distribution (apt for Ubuntu, yum for CentOS and etc…). The main issue with this type of installation is that the versions available in official repositories may be outdated. For example, at the moment of writing this tutorial the latest version of Node.js in Ubuntu’s repositories is v8.10.0, and in CentOS epel-release repo the latest available version is v6.14.0. But using nvm we can install v10.0.0 at the same time. However, below you can find steps to install latest available stable version of Node.js for each distro.

Ubuntu 18.04

Before installing Node.js on your Ubuntu 18.04 machine, update you OS to the latest version. To do so, you can run:

sudo apt-get update

Now you can install Node.js using the following command:

sudo apt-get install nodejs

Also you can install npm (Node.js package manager) to install modules and packages. To install npm, run the command below:

sudo apt-get install npm

After installation, you can check the version of Node.js (note that the executable from the Ubuntu repositories is called nodejs instead of node)

nodejs -v

To update Node.js on Ubuntu 18.04 machine either update OS with the command:

sudo apt-get update

or run install command. It will update the existing version if there are updates:

sudo apt-get install nodejs

To remove Node.js from your Ubuntu 18.04 machine run the following command:

sudo apt-get remove nodejs

CentOS 7

To install Node.js on CentOS 7 machine using official repositories follow the steps below.

First, install epel-release repository on your machine using the following command:

sudo yum install epel-release

This will allow you to install Node.js. To do so, you can run:

sudo yum install nodejs

Check the installed version with the command below:

node --version

And finally you can install npm (Node.js package manager) using the following command:

sudo yum install npm

To update Node.js on CentOS 7 machine either update OS with the command:

sudo yum update

or run install command. It will update the existing version if there are updates:

sudo yum install nodejs

To remove Node.js from your CentOS7 machine run the following command:

sudo yum remove nodejs

Arch Linux

To install Node.js on Arch Linux, use the following command:

sudo pacman -S nodejs

To install npm, you can run:

sudo pacman -S npm

To remove Node.js from Arch Linux typ:

sudo pacman -R nodejs

Check if Node.js is working using script

To check if our installation (doesn’t matter which way) is successful and node.js is working properly, we are going to create very simple «Hello World» app and check if everything works fine. To do so, we create hello-world.js in our home directory file using the command below:

vim hello-world.js

and add the following content into it and save the file:

a="Hello";
b="World!";
console.log( a+' '+b);

Now we run the app with the following command

node hello-world.js

The output must be the one shown below

Hello World!

That means that node.js is installed correctly and working properly. You can now go ahead and create your incredible apps using node.js.

Testing With Node.JS Shell

Let’s start with the node command as follow and get its output result to see if Node.JS is fully functional.

# node
> console.log('Node.js Installed Using Package Manager');
Node.js Installed Using Package Manager

REPL For Your NodeJS Apps

REPL is the Node.js shell, any valid Javascript which can be written in a script can be passed to the REPL. So let’s see how REPL works with NodeJS.

root@ubuntu18:~# node
> var repl = require("repl");
undefined
> repl.start("> ");

Press Enter and it will show out put like this:
> { domain: null,
_events: {},
_maxListeners: 10,
useGlobal: false,
ignoreUndefined: false,
eval: [Function],
inputStream:
{ _connecting: false,
_handle:
{ fd: 0,
writeQueueSize: 0,
owner: [Circular],
onread: [Function: onread],
reading: true },
_readableState:
{ highWaterMark: 0,
buffer: [],
length: 0,
pipes: null,
...
...

Here is the list of command line help that we can use to work with REPL.

REPL Manual

Working with NodeJS Packet Manager

NPM is simple CLI tool for ensuring that a given node script runs continuously. It helps to Install and manage dependencies through the file package.json. We will start its using init command as.

# npm init

npm init
Conclusion

If you want to use the exact version of Node.js the better way is to install using nvm or cloning from Github official repository. If there is no matter which version to use one can install using distro’s default package manager, as that way is much faster than the others.

Artículos interesantes