Installing Node.js on Linux
If you're running Linux, and are in any way interested in using Node.js, then your first port of call would by the package manager used by your distro (apt on Debian and Ubuntu, or Yum on Fedora/RedHat.) The problem is, even if you're running a completely bleeding-edge distro, your Node version will probably be quite out of date. So what are your options? A bit of research gave me several options:
- Using Linux Brew, which is a Linux version of Homebrew for Mac
- Compiling from source
- Updating Node from the version of NPM that comes with the distro
Using Linux Brew
This seemed to be the preferred option, as it was a port of the method used by MacOS. The first step was to ensure all the prerequisites were installed, which was pretty simple (and most of them were already installed on my system). One thing that I did note is that the listed dependencies on the GitHub page (https://github.com/Homebrew/linuxbrew/wiki/Standalone-Installation#dependencies) didn't mention about installing the development tools group of tools:
sudo yum groupinstall 'Development Tools'
After getting these installed, the next step was to install Brew and add it in to the path:
ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/linuxbrew/go/install)" export PATH="$HOME/.linuxbrew/bin:$PATH" export MANPATH="$HOME/.linuxbrew/share/man:$MANPATH" export INFOPATH="$HOME/.linuxbrew/share/info:$INFOPATH"
and then use Brew to install the latest version of Node:
brew install node
This was where I ran into problems with Brew complaining that it couldn't find GCC (which was definitely installed) and asking me to install it with Brew.
The exact error I was getting was this:
ccache: FATAL: Could not find compiler “gcc4.7” in PATH Error: pkg-config cannot be built with any available compilers - To install this formular you may need to: brew install gcc
This is not a very Linux way of behaving, leaving all kinds of versions of things all over the place. It seems that this is a bit of a known problem with Brew not correctly detecting the versions of things installed on the system.
The solution apparently is to create symlinks for the version of GCC that Brew is looking for, to point to the current version of GCC installed. This worked up until a point (after restarting a console session) but it just led to more and more unfulfilled requirements.
If you are able to get it running, it's worth noting that the install step takes a long while to run, so if it looks like it's hung, go off and make a cup of tea (or several) as you will be waiting around a while.
Compiling From Source
This is the standard compile, make, and install routine that anyone familiar with Linux will have gone through at one time of another. Given my “success” with the previous method, I wasn't going to waste a lot of time with this if I didn't need to.
The configure step worked as intended, but the make step bombed out with
Error 2. A bit of Googling suggested that one way to overcome this (beyond ensuring dependencies were set up, which were from the first install method) was to run the configure step with the
--without-snapshot flag. Unfortunately, this also had no affect, so I just gave up on this method as a waste of time (just the make step took over ½ an hour to run.)
Personally, I'm not a big fan of compiling from source, as you lose out on the advantages that a package manager gives you, but it did seem the next logical method to try after Brew.
Installing Through NPM
The final method left open to me was to try updating Node through NPM itself. If you didn't have NPM installed already, you can install this through the standard Linux package manager for the distro you're running. It doesn't actually matter that it's not the latest version, as we'll be updating it in the next step (you'll need to run these steps as root with
npm cache clean -f # this step just ensures that any local cache is force cleaned npm install -g n # installs node globally, n is a shortcut syntax n stable
This worked first time without any issues, and very quickly compared to the previous methods, bringing my Node version right up to 5, the latest stable version.
You may get better mileage out of the previous two methods I tried, and it does seem like the first one is what a lot of people have been using. Whichever you choose, you'll hopefully find this useful with installing and updating Node.js on your own Linux machine.