Below is a brief description of how to get Clojure up an running on Mac OS X Leopard. I also describe a small shell script that invokes Clojure and sets its classpath using a simple, project-specific configuration file.
The first step is to create a Clojure directory in your Library folder that contains the subfolder
lib. Do this at your Terminal:
$ mkdir -p ~/Library/Clojure/lib $ cd ~/Library/Clojure
Once you’ve downloaded it, copy
clojure.jar to the
$ cp ~/Downloads/clojure/clojure.jar ~/Library/Clojure/lib/
To make Clojure’s interactive mode easier to you, you should grab the JLine library and install it as well.
$ cp ~/Downloads/jline-0.9.94/jline-0.9.94.jar ~/Library/Clojure/lib/jline.jar
I’ve created a bash script called
clj that I put in
~/Library/Clojure and symbolically link to from somewhere in my path.
This script sets up the Clojure classpath and, if it is present, also adds the contents of a
.clojure file to the classpath before executing Clojure. It also adds the current directory to the classpath for ease of use.
$ cp ~/Downloads/clj ~/Library/Clojure/ $ chmod u+x ~/Library/Clojure/clj
Now you will want to symbolically link to it from a directory in your
echo $PATH at the Terminal to see a list of options. I have a directory
~/bin/ where I keep such things.
To make the link use, for example:
$ ln -s ~/Library/Clojure/clj ~/bin/clj
Now I can type
clj from any directory and see:
$ clj Clojure user=> (= (* 6 8) 42) false user=>
I can also run Clojure programs. For example, suppose I have the following file called
test.clj in the current directory:
(println "Hello, world!")
Then, when I run the following, I see:
$ clj test.clj Hello, world!
If you need any project-specific jar files added to the classpath when running Clojure, this can be done by putting a
.clojure file in the same directory you will be running the project from.
For example, suppose I have a program
~/code/cafe/macchiato.clj that requires class from the Java libraries
frother.jar in the
I can easily create a file
.clojure that specifies where Clojure can find these extra libraries:
$ cd ~/code/cafe $ echo "lib/grinder.jar:lib/frother.jar" > .clojure
Then when I run:
$ clj macchiato.clj
~/code/cafe directory, the earlier
clj script will pick up the extra jar files from the
.clojure file and add them to the classpath before invoking Clojure.
This step is optional. You only need to do it if you wish to use some of the extra, community-contributed Clojure libraries.
Once you have git installed, you can get the current version of the contributed libraries like so:
$ git clone git://github.com/kevinoneill/clojure-contrib.git
Now build the jar file using
ant and copy it to the Clojure directory:
$ cd clojure-contrib $ ant -Dclojure.jar=$HOME/Library/Clojure/lib/clojure.jar $ cp clojure-contrib.jar ~/Library/Clojure/lib/
Now you should be able to use things like Stuart Sierra’s library. For example, suppose I was writing a simple vector library called
vec.clj and wanted to put the following tests in
test.clj in the same directory:
(ns test (:require vec) (:use clojure.contrib.test-is)) (deftest test-cross-product (is (= [-3 6 -3] (vec/cross [1 2 3] [4 5 6]))) (is (= [0 0 1] (vec/cross [1 0 0] [0 1 0])))) (run-tests)
Then, because the
clojure-contrib.jar is on the classpath, I can run and see the following:
$ clj test.clj Testing test Ran 1 tests containing 2 assertions. 0 failures, 0 errors.
It’s probably overkill for what I need since that bundle includes a working installation of Clojure (i.e., it doesn’t call the
clj I discuss here). I edit in TextMate and then run sessions and scripts from the Terminal. All I’m really taking advantage of is the syntax-highlighting, auto-formatting and the online help (
There are other options around such as Clojure modes for Vim and Emacs but I haven’t tried them.
I wrote these notes mainly to document the sometimes frustrating processes of getting a flexible, easy-to-use Clojure environment set up. The “Getting Started” page at the main Clojure site are great for getting a REPL up and running but didn’t help me at all when it came to using other jars and clojure-contrib. Of course, if I’m doing something here that is patently stupid, please let me know in the comments.
I keep a shorter version of these notes with more concise step-by-step instructions at the GitHub repository for this set up.
Hopefully, this short introduction will make it easier for others to get up and running with this great new language.
1 April 2009: Added notes about the editor I use and link to my Github repository.
4 August 2009: Fixed some incorrect paths in the instructions.