Setting up Node.js and Clojure
I’ve been playing with Node.js and Clojure recently. Below are the steps to get each of them up and running.
Setting up Node.js
The easiest way I’ve found so far is to use nvm.
Get nvm (Node Version Manager)
git clone git://github.com/creationix/nvm.git ~/apps/nvm
Source nvm.sh in bash or zsh
source $HOME/apps/nvm/nvm.sh
Install the version of node you want
nvm install v0.8.9
Select the version of node you want
nvm use v0.8.9 node --version
That’s it! From then on just write your js scripts and run them with node.
You can of course install from source or use package manager if that works for you. For vagrant, there’s also the nodejs cookbook.
Setting up Clojure
clojure.org should have a better Quick Start guide. Some people say that Node.js is easier to set up and I agree. However, I also expect any good developer to be able to set up their tools, especially if the benefits of the tool outweighs the initial hurdle.
I’m using Ubuntu on my machine, and the latest package for clojure on Ubuntu 12.04 is 1.3.0. If that’s fine for you then it’s simply an apt-get install clojure1.3
to get started.
The latest version of Clojure right now is 1.4.0 and I wanted the latest, so here’s how I got it set up.
- Install JDK
Apparently JDK is already installed on my machine, but if it’s not on your then install it by:
apt-get install openjdk-6-jre-headless
- Get a helper script to help run the java command with proper classpath
I modified the Ubuntu one from clojure1.3 package to look for clojure1.4 jar and also include *.jar in$HOME/apps/jars
directory. The good thing about this script is that I can now use whatever version of clojure I want. I saved the file asclj
.
#!/bin/sh
if [ "x$CLASSPATH" = "x" ] ; then
extra_classpath=""
else
extra_classpath=":$CLASSPATH"
fi
while true ; do
case "$1" in
-cp | -classpath)
extra_classpath=":$2"
shift 2 ;;
--)
shift
break ;;
*)
break ;;
esac
done
if [ "x$1" = "x" -a "x`which rlwrap`" != "x" ] ; then
rlwrap="rlwrap -r -c -C clojure -f /etc/rlwrap/clojure1.4 -b (){}[],^%\$#@\"\";:''|\\"
fi
for i in `ls $HOME/apps/jars/*.jar`
do
included=${included}:${i}
done
exec $rlwrap java -cp $HOME/apps/jars/clojure-1.4.0.jar:.:"$included""$extra_classpath" clojure.main "$@"
Grab clojure jar from http://clojure.org/downloads and put the jar in
$HOME/apps/jars
Run Clojure
clj
For any additional library you want to use, you can either pass the -cp argument or simply drop the jar files into $HOME/apps/jars
. E.g. I put data.json-0.1.3.jar and math.numeric-tower-0.0.1.jar in there.
An alternative method is to use Leiningen, but that’s more useful IMO for setting up projects. If I want to quickly write a script and run it, I prefer my clj script.
Setting up VIM
For VIM users, install VimClojure and vim-slime will make editing Clojure code much easier. After installing VimClojure, put the following in .vimrc:
autocmd BufEnter *.cljs set filetype=clojure
let g:vimclojure#HighlightBuiltins = 1
let g:vimclojure#ParenRainbow = 1
For vim-slime, I’m using tmux configured via .vimrc:
let g:slime_target = "tmux"
Node.js also benefits from vim-slime because you can then send javascript code to your node REPL.
Screenshot of my setup: