Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save bookercodes/aa3756a364e338214c0e to your computer and use it in GitHub Desktop.
Save bookercodes/aa3756a364e338214c0e to your computer and use it in GitHub Desktop.
I will put this on my blog once it is up and running.

#How to Install Node on Ubuntu, the Right Way

When I first started using Ubuntu a few months ago, I instinctively installed Node using these commands:

curl -sL https://deb.nodesource.com/setup_0.12 | sudo bash -
sudo apt-get install nodejs

This is not the correct way to install Node. Installing Node this way leads to two problems in my experience:

  1. Most modules depend on a package called node as where you just installed a package called nodejs. This causes some modules to fail because they cannot find node. To work around this, you have to create an alias, like this:

    sudo ln -s /usr/bin/nodejs /usr/bin/node

It is not the end of the world but it can throw people off. 2. When you install Node in this manner, you are obliged to use root. This turns out to be very problematic because now, when you install Node modules, you need to use root too. This is generally tedious and can lead to bad permission habits like running sudo -s all the time or changing the ownership of system directories.

The better option is to use Node Version Manager.

Node Version Manager is awesome. Not only does it allow you to install Node in a favourable manner, but it allows you to install more than one version of Node, side by side:

# Install the latest version of Node, Node 0.12.4
$ nvm install 0.12 
$ Now using node v0.12.4 (npm v2.10.1)

# Install io.js, a fork of Node that provides faster and more stable release cycles
$ nvm install iojs
$ Now using io.js v2.2.1 (npm v2.11.0)

# Use Node 0.12.4
$ nvm use 0.12
$ Now using node v0.12.4 (npm v2.10.1)

# Run script using Node 0.12.4
$ node app.js 
 
# Remove io.js
$ nvm uninstall iojs
Uninstalled io.js v2.2.1

(You can run each of these commands in the terminal.)

Awesome, right?

I am not going to teach you how to install Node Version Manager in this post - how to install Node Version Manager is a very well documented process already. Just bare in mind that, if you are using a shell other than Bash (such as ZSH or Fish), you'll want to search around for instructions that pertain to your specific shell. I am using Fish shell, so I used this tutorial.

Of course, Node Version Manager is not limited to Ubuntu. I am sure it works on other Linux distributions and OS X, too, but I have only tried it on Ubuntu thus far.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment