+4 votes
in Development by (1.5m points)

How to install Node.js on Ubuntu or Debian

1 Answer

+5 votes
by (725k points)
Best answer

More and more applications developed with Node.js and sooner or later we find the need to install it in our operating system. In this tutorial we will talk about how to install Node.js on Ubuntu or Debian and we will also install the npm package manager .


For our tests we have used Ubuntu 16.04 LTS, but the instructions we will give will work in Debian without problems. We will rely on the instructions provided from the official NodeSource website on GitHub , which also includes installation instructions for CentOS, Fedora and RedHat .

Introduction: preliminary issues that we should know.

The installation of Node.js in Ubuntu is surrounded by many conflicts caused by the name of the binary. This conflict is due to the fact that an application called node already exists in the Ubuntu repositories and then it was decided to call the Node.js binary as nodejs to avoid conflicts.

This seemed like a solution, has brought several compatibility problems and more of a headache to the users. To avoid possible conflicts, our first action will be to eliminate the possible node installations by executing the commands:
sudo apt-get --purge remove node
sudo apt-get --purge remove nodejs

In this tutorial we are going to show you two methods to install Node.js in Ubuntu or Debian, one to do it from the official repositories and another to install it from the official Nodesource PPA.

The installation from the official repositories has the advantage that it is considered stable, but it is also true that we will have to deal with the binary name conflict. On the other hand, if we install from the official Nodesource PPA, we can choose which version to install and we will not encounter the name conflict.

In my opinion, using the PPA has more advantages, so I recommend it , but here we will show you both options so you can choose.

How to install Node.js on Ubuntu or Debian from official repositories.

At the time of writing this article, the version installed from the official repositories is Node.js 4.2.6, which is considered as LTS and whose life cycle ends on April 1, 2018.

To install Node.js we execute the command:
sudo apt-get install nodejs

Once installed, we can access the binary with the nodejs command, but if we execute the node command, we will see that it returns an error. To solve this conflict we have to install the nodejs-legacy package with the command:
sudo apt-get install nodejs-legacy

Now we can execute nodejs or node interchangeably.

Finally, we can install the npm package manager with the command:
sudo apt-get install npm


How to install Node.js from the official NodeSource PPA on Ubuntu or Debian.

When using the official NodeSource PPA, the first thing to consider is that Node.js and its npm package manager will be installed at once, without installing them separately.

The second aspect that we should consider, is which version of Node.js to install . At the time of writing this tutorial we have available:

  • The 4.x series : is considered as stable and LTS for support on April 1, 2018.
  • 5.x Series : not LTS (installation not recommended) .
  • The 6.x series : LTS version with support until April 18, 2019 (recommended installation) .
  • Series 7.x : version in current non-LTS development (recommended installation only if you want to try the latest built-in news) .

With version 6.x I have not had problems so far and since it has long-term support until 2019, it is the one I choose to install normally. First of all we will make sure that we have cURL installed , otherwise we will install it easily with the command:
sudo apt-get install curl

To install Node.js from the PPA, we execute the commands:
curl -sL https://deb.nodesource.com/setup_6.x | sudo -E bash -
sudo apt-get install nodejs

Now we see that both the nodejs and node commands are functional and we also have the npm package manager installed.

If we wanted to install another version, the first command would be:

Finally, it is also advisable to install the build-essential package because some npm packages that compile code will need it. So we execute the command:
sudo apt-get install build-essential


To end.

We see that installing Node.js on Ubuntu or Debian is quite simple. There would be a last method using a script called nvm ( Node Version Manager ) that allows us to install and manage different versions of Node.js simultaneously.

We are not going to try this installation method, since it is generally not necessary to have several versions of Node.js installed, but it is good to know that possibility.

Finally, remind you that we also have a tutorial on how to install Node.js on Windows , in case you are interested.