Install latest (or specific) version of Node.js on Ubuntu Linux

The typical way to install Node.js in Ubuntu Linux is to use the apt-get package manager (i.e., sudo apt-get install nodejs). However, the Ubuntu repositories tend to lag a good bit behind the latest version of the platform. In this tutorial, we’ll install Node.js by downloading the source and compiling it ourselves. We will install it our local $HOME directory to avoid any conflicts with the standard package. This tutorial should work fine on your local Ubuntu machine, as well as a Ubuntu Vagrant box virtual machine (VM).

First, we need to update our system and install a few packages, if they aren’t already installed. Specifically, we need to install build-essential, which contains the GCC C++ compiler, and curl for downloading files from the Internet via the command line. At the command prompt, run these commands:

sudo apt-get -y update
sudo apt-get -y install build-essential curl

To install Node.js to our $HOME directory, we need to add $HOME/local/bin to the PATH environment variable and then export it to our environment by running:

echo 'export PATH=$HOME/local/bin:$PATH' >> $HOME/.bashrc
. $HOME/.bashrc

Now, create two new directories, local and node-latest-install, in the $HOME directory. These directories will hold the installed version of Node.js and the Node.js source code, used to compile the new version, respectively.

mkdir $HOME/{local,node-latest-install}

Next, we change to the node-latest-install directory, download the Node.js source code and extract the source code into that directory.

cd $HOME/node-latest-install
curl -LOk http://nodejs.org/dist/node-latest.tar.gz
tar xz node-latest-tar.gz --strip-components=1

This will get the very latest released version of Node.js. If you prefer a specific version, navigate to http://nodejs.org/dist/ in your web browser and find the appropriate directory and file name ending with tar.gz. For example, to download the “latest” version from the 4.x branch (which at the time of writing is 4.2.2), we would download http://nodejs.org/dist/latest-v4.x/node-v4.2.2.tar.gz.

Finally, we compile the Node.js application. Notice specifically that we specify to install to the $HOME/local directory.

./configure --prefix=$HOME/local
make
make install

Depending on the speed/power of your machine, compiling may take several minutes. The main thing to look for is whether or not any errors are returned. If you get errors while compiling, ensure that you have build-essential package installed.

To verify that Node.js installed successfully, run node -v and the command prompt. It should return something like v4.2.2. To confirm that you are running Node.js from the local installation, you can run which node at the command prompt, which should return something like /home/username/local/bin/node where username is your username (or vagrant, if you are installing on a Vagrant box).

The Node.js installation also includes the Node Package Manager (NPM). This is the official package manager for Node.js and allows you to install almost any package for Node.js that you could need. In particular, NPM is required to use Laravel Elixir, Laravel’s development task running, which acts as a wrapper for Gulp.

Advertisements