How to install Node.js on Linux

Need Hosting? Try ours, it's fast, reliable and feature loaded with support you can depend on. View Plans

Node.js is a JavaScript based open source platform, used for developing server-side and network applications. Node.js is cross-platform hence applications written in Node.js can run on any platform. It is built on Google's V8 JavaScript engine. Node.js is highly scalable, lightweight and very fast in code execution. It is a very popular scripting language to develop server-side apps.

Installing Node.js

There are many way of installing Node.js onto your Linux machine. Node.js supports almost all Linux distributions but in this tutorial we will learn to install it on Ubuntu/Debian based machine as well as CentOS/Fedora based machines. We can install Node.js using many methods but it is recommended that you install in using either NodeSource binary distribution repository or using Node Version Manager (nvm). Some Linux distributions like Ubuntu has included Node.js in their default repository. Installation using their default repository is super easy but you may not find the latest version there.

Using the NodeSource Binary Distributions Repository

Installing Node.js from the official NodeSource website will provide you most recent version of Node.js. NodeSource actively maintains official repositories of Node.js.

On Debian/Ubuntu Based Distributions

There are multiple stable versions of Node.js available, you can install the required version according to your choice. To install Node.js 4x run the following command:

curl -sL https://deb.nodesource.com/setup_4.x | sudo -E bash -

The above command will add the repository in your machine's configuration. Execute the following command to install Node.js in your machine.

sudo apt-get install -y nodejs

If you want to install Node.js v6 then execute the following commands:

curl -sL https://deb.nodesource.com/setup_6.x | sudo -E bash - sudo apt-get install -y nodejs

If you want to install an earlier, stable version of Node.js then execute the following commands:

curl -sL https://deb.nodesource.com/setup | sudo -E bash - sudo apt-get install -y nodejs

Installing nodejs will also install npm which is Node Package Manager. Using npm you can easily share JavaScript code with other developers. Some npm packages requires build tools in order to compile and install. To install build tools, execute the following command:

sudo apt-get install -y build-essential

On RHEL/CentOS/Fedora Based Distributions

To add the NodeSource official repository on RHEL/CentOS/Fedora based distributions run the following command. You will need to login as root user to execute the following command. If you are not root user then you can use sudo command at the start of all commands:

For Node.js v4x

curl --silent --location https://rpm.nodesource.com/setup_4.x | bash -

For Node.js v6x

curl --silent --location https://rpm.nodesource.com/setup_6.x | bash -

For Node.js 0.12x

curl --silent --location https://rpm.nodesource.com/setup | bash -

Once you add any of the above repositories, you can execute the following command to install Node.js.

yum -y install nodejs

To install build tools run the following command:

yum groupinstall 'Development Tools'

Using Node Version Manager (nvm)

nvm

nvm

nvm

is a simple script which is designed to install multiple versions of Node.js. In all other installation methods we only get the most recent version of Node.js available in that repository but usingwe have access to all the available versions of Node.js. We can also install more than one version of Node.js using

To install nvm we will need to install the required tool for building the source package. Run the following command to install the build tools in Ubuntu/Debian:

sudo apt-get update sudo apt-get install build-essential libssl-dev

If you are installing on CentOS/Fedora then use these commands to install build tools:

sudo yum update sudo yum groupinstall 'Development Tools'

Now as the build tools are installed, you will need to fetch and execute the installation script from official github repository of nvm .

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

This will install nvm on your machine. Close and reopen your terminal and run the following command to check if nvm is successfully installed.

command -v nvm

This command should simply give a output npm in your terminal. If yes then you have successfully installed nvm into your machine.

To find out the available versions of Node.js which you can install through nvm , run the following command.

nvm ls-remote

The output of the above command will provide you with a huge list of versions of Node.js.

... v5.9.0 v5.9.1 v5.10.0 v5.10.1 v5.11.0 v5.11.1 v6.0.0 v6.1.0 v6.2.0 v6.2.1

To install any of these versions use the following command:

nvm install version

Substitute version for the desired version of Node.js you want to install. For example if you want to install the latest version currently available, use the following command.

nvm install v6.2.1

This will install version 6.2.1 in your machine and you will see following output.

Downloading https://nodejs.org/dist/v6.2.1/node-v6.2.1-linux-x64.tar.xz... ######################################################################## 100.0% Now using node v6.2.1 (npm v3.9.3) Creating default alias: default -> v6.2.1

We can see in the output that nvm automatically configured v6.2.1 to use and also it made this version the default version. You can install more then one version of Node.js using the above command. Each version of Node.js will install and manage it's own npm .

You can explicitly ask nvm to use a specific version by executing the following command:

nvm use v5.11.1

You can replace v5.11.1 with any version of your choice. You can also change the default version by issuing the following command:

nvm alias default v5.11.1

To see a list of all installed versions run the following command:

nvm ls

You will see a output similar to this

v0.11.13 -> v5.11.1 v6.2.1 default -> v5.11.1 node -> stable (-> v6.2.1) (default) stable -> 6.2 (-> v6.2.1) (default) unstable -> 0.11 (-> v0.11.13) (default) iojs -> N/A (default)

In this output you can see a list of all installed versions. -> indicates the version which you are currently using. default -> tag indicates the default version of Node.js in your machine.

Getting Started With Node.js

After installation of Node.js you will can use the command node to execute the JavaScript. If node is used without any filename or argument then it will take you to JavaScript console from where you can type and execute JavaScript commands. To exit from node interface type .exit command. You can also create a http server using Node.js. Create a new file and add the following code into it. For example we are using nano editor and myserver.js file name.

To create a new file run the following code:

nano myserver.js

Now add the following code into the file:

var http = require('http'); http.createServer(function (req, res) { res.writeHead(200, {'Content-Type': 'text/plain'}); res.end('Node.js is running a servernHi There'); }).listen(8080); console.log('HTTP server running on port 8080.');

Now save the file and exit from the editor. Run the code by executing the following command:

node myserver.js

You will see following output on terminal:

HTTP server running on port 8080.

You can now go to your browser and access your http server on

http://your_ip_addr:8080

You will see the following message on page:

HP_NO_IMG/data/uploads/users/1a4f26de-5d97-48b7-abf5-631194ef3a10/1392512319.png" alt="" />

Conclusion

There are several different ways to install Node.js on any Linux machine but using nvm is recommended as it gives more flexibility and you can use the installer script on any operating system.