For as popular as this JavaScript run-time has become, you might be surprised to find out that it actually still isn't very easy to install Node.js on Ubuntu and other Linux distributions. It's still more of a manual process than it should be. I find myself Googling this just about every time I set up a new Linux machine, so I thought I'd write up some instructions on a few different ways you can install Node on Ubuntu.
Options for Installing Node.js on Ubuntu
There are a few different ways you can perform the install, depending on your requirements and customization needs. Here are a few of the more popular methods:
- Ubuntu's package manager
- Ubuntu package from NodeSource
- Binary directly from Node
- Install from Source
Ubuntu's package manager
This is, in my opinion, the easiest method. I always try to install packages through apt-get
whenever possible to keep everything in one place. This way you'll also know the commands to uninstall the package when needed.
NOTE: As of the time of this writing (11/25/15), the binary installed with apt-get
is only v0.10.25, while the latest stable version available is v5.1.0, so they're pretty far behind. For the newest version, check the other install methods below.
To install Node and npm via apt-get
, run these commands:
sudo apt-get update
sudo apt-get install nodejs
sudo ln -s /usr/bin/nodejs /usr/bin/node
sudo apt-get install npm
Since the package is linked as nodejs
and not the typical node
name, we have to add a symlink, otherwise some services won't work correctly as they expect the node
command to be available.
Ubuntu package from NodeSource
If you still want to use apt-get
, but need a much newer version of Node, you should use this method.
This is very similar to the last one I showed you, but instead we'll be running a script (maintained and distributed by NodeSource) to show the package manager where to get the latest version.
Here are the commands:
curl -sL https://deb.nodesource.com/setup_5.x | sudo -E bash -
sudo apt-get install -y nodejs
sudo ln -s /usr/bin/nodejs /usr/bin/node
There is no need to explicitly install npm as it is already included with this binary.
Binary Directly from nodejs.org
You can skip all of the package manager hand-waiving and just install a binary directly from nodejs.org itself.
Just make sure you install the correct version for your system. They have binaries for all different version, operatings systems, and CPU architectures. If you're not exactly sure what the name of the binary will be for your system, try browsing through the distributions.
In my case here, I needed version 5.1.0 for 64-bit Linux:
wget http://nodejs.org/dist/v5.1.0/node-v5.1.0-linux-x64.tar.gz
sudo tar -C /usr/local --strip-components 1 -xzf node-v5.1.0-linux-x64.tar.gz
This will give you both the node
binary and npm
.
Install from Source
This one is a bit more complicated, but you'll learn a bit more by doing it this way.
First, you'll need to install a few tools required to build the code. For this step, all you need to do is:
apt-get install make g++ libssl-dev
Once that has completed (or if you already have these packages installed), you can download the source code from nodejs.org:
cd /tmp
wget http://nodejs.org/dist/v5.1.0/node-v5.1.0.tar.gz
tar -xvf node-v5.1.0.tar.gz
cd node-v5.1.0
You'll probably notice that the URL looks much like the one from the last method. It is very similar, but if you look closer there is no "linux-x64" in the name, meaning it hasn't already been built for a particular system. This download gives us just the source code.
Next, configure and build the code:
Check out our hands-on, practical guide to learning Git, with best-practices, industry-accepted standards, and included cheat sheet. Stop Googling Git commands and actually learn it!
./configure
make -jX
And finally, if everything went well with the last command, install it:
make install
Conclusion
And that's it, you're done! Regardless of whatever installation method you used, you should run node -v
to verify the build/install worked correctly. If you used either of the last two methods, make sure the version printed out matches the one you intended to download.
Node.js is under heavy development right now and seems to at least be releasing a new minor version every week. What this means is apt
probably won't be able to keep up with all the new version coming out, so you'll probably be better off using the last two methods.