How to Install CoffeeScript on Ubuntu 12.04

February 25, 2013

What is CoffeeScript?

As part of my quest to become a better rails developer, I decided to learn CoffeeScript, which is something The Rails Tutorial touches on extremely lightly. Jeremy Ashkenas, the creator of CoffeeScript, called it “Javascript’s less ostentatious kid brother.” Coffeescript is a language that compiles to Javascript. In a sense, it’s designed to be a more “readable version of Javascript.”

How to Install CoffeeScript

I went down a bit of a rabbit hole when I first tried to install the CoffeeScript compiler, so I’m documenting this to help others as well as myself. It turns out to run CoffeeScript the simplest way is to install NodeJS. First I follow the instructions for “use-nave.sh” to install nave, a tool which provides virtual environments for NodeJS (it’s sort of analagous to the way RVM creates gemsets). To do that I follow the instructions over at https://gist.github.com/isaacs/579814 under “use-nave.sh” (repeated below with a few tweaks for convenience).

# this way is really handy if you want to test things
# in different versions of node and use stable release
# versions of things.
# make a folder where you want to keep this stuff

mkdir ~/.nave
cd ~/.nave
wget http://github.com/isaacs/nave/raw/master/nave.sh
sudo ln -s $PWD/nave.sh /usr/local/bin/nave

# now you can forget about that folder.
# you never have to go back in there.
# to use a version of node in a virtual environment

nave use 0.4.8

# to install npm in that virtualenv

curl https://npmjs.org/install.sh | sh

In the above instruction set, instead of “nave use 0.4.8”, I give the command “nave use stable” to use the latest stable version of NodeJS, which at the time of this writing was 0.8.19.

Now install the CoffeeScript compiler:

npm install -g coffee-script

Now edit your .bashrc file with a text editor to add nave to your system path so your system finds nave everytime you open a terminal shell

For example, on my system I do:

sudo gedit ~/.bashrc

This opens the .bashrc file and I add the following line at the bottom of the file:

export NODE_PATH=/home/user_name/.nave/installed/0.8.19/lib/node_modules where user_name is your user login name. Your NODE_PATH variable might look a little different.

Testing it all worked

Try running "coffee -v" at the command prompt. If you get a version number, you’re set.

You may have to change permissions on your nave.sh file

If you have trouble running nave and get a permission denied error, cd into ~/.nave and issue the command "chmod a+x nave.sh".

References:
  1. http://apple.stackexchange.com/questions/12993/why-doesnt-bashrc-run-automatically

Profile picture

Written by Bruce Park who lives and works in the USA building useful things. He is sometimes around on Twitter.