Getting started with Clojure on GNU/Linux

2010-4-29

Getting started with Clojure is easy if you are running a fairly recent GNU/Linux distro (I use Fedora 11 - and everything worked perfectly). Just make sure that you have a working Java installation before you take the plunge!

The easy way

Clojure is distributed as a JAR File which you can download from here.

There are two ways in which you can "run" this JAR file. Beginners would prefer interacting with the Clojure REPL (read-eval-print-loop) which gives immediate feedback. Here is how you start the REPL: execute the following command in the directory where "clojure.jar" is present:

java -cp clojure.jar clojure.main

You will get an interactive prompt where you can evaluate Clojure expressions:

Clojure 1.1.0
user=> (+ 1 2)
3
user=> (* (+ 2 3) 4)
20
user=>

Here is what you should do if you wish to run Clojure programs as "scripts". Create a file containing Clojure code (say hello.clj) and just type:

java -cp clojure.jar clojure.main hello.clj

You will definitely want to run the REPL or execute scripts from directories other than the one in which you have copied "clojure.jar". The easy way to do this is to set the shell variable CLASSPATH to the name of the Clojure JAR file:

export CLASSPATH=/home/pce/clojure/clojure.jar

java clojure.main

Once you set the CLASSPATH properly, you need not type "-cp clojure.jar" as part of your commands to invoke the REPL or execute scripts.

The not-so-easy way

Emacs is *the* editor for developing with Lisp-like languages. Let's see how to get a Clojure REPL within an emacs buffer (if you are not familiar with Emacs, now is the time to spend a few minutes learning the basics!).

The first step is to download and run the emacs lisp package manager - follow the instructions given here. You have to install the following packages - clojure-mode, slime, slime-repl and swank-clojure. I got a few warnings during this process, but everything was OK in the end.

Once that is done, restart emacs and type "Alt-x slime" - the necessary clojure jar files will be automatically downloaded and installed and you will have a Clojure REPL within Emacs!

Resources for learning Clojure

What got me interested in Clojure was this presentation by Rich Hickey. Do check it out!

Clojure - Functional programming for the JVM is a great introduction to the language.

Stuart Halloway has written a nice Clojure programming book - this was my primary resource for getting started with Clojure, and I liked it very much!

Manning publications has two Clojure books in the pipeline - The Joy of Clojure and Clojure in Action; "early access" versions of both books are available.

Sean Devlin's fulldisclojure is a series of very interesting Clojure screencasts -- check out Code Kata - Fizz Buzz as a sample.

There is a Clojure channel on blip.tv which you might find interesting.

Planet Clojure collects Clojure related blog posts.

Here is a collection of links to videos and presentation about Clojure

[Go to Code Clojure home] [Follow me on Twitter] [Go to pramode.net home]