clojure.roboloco.net

The following describes the packages I install when I need to set up a Clojure development environment the way I like it on a new computer running Ubuntu 10.04. These notes are mostly for my own use, but you may also find them useful.

Ubuntu Packages

We are going to need a few pieces of support software to begin development in Clojure. Assuming that we are starting from a fresh installation of Ubuntu 10.04, run the following commands in a terminal:
sudo apt-get install ant # Ant is a build tool for Java
sudo apt-get install maven2 # Maven is another build tool for Java
sudo apt-get install git-core # git is a version control system

There are many different JVMs available for Linux. By default, Ubuntu 10.04 uses OpenJDK, but many people prefer Sun’s implementation. To see what is presently installed, run:

java -version

So far, OpenJDK has worked fine for me. But, if you want to remove all evidence of OpenJDK and install the Sun implementation, try:

Populating the Directory Tree

Directories aren’t very useful without files inside them. Let’s fill the directories we just created with useful things.

First, let’s grab Clojure itself. Because features are continually being added to Clojure — as well as bug-fixes — for my purposes there is no reason not to use the latest version of Clojure. So let’s use git to get the newest version of Clojure and clojure-contrib:

It’s a good idea to test Clojure at this point. The basic method of interaction with a Clojure (and many other lisps) during development is through a REPL (Read Eval Print Loop). REPLs are great because they let you interact with the language in a very fine-grained way, without the annoyance of having to compile each time you want to test your code.

You can fire up a REPL by adding the clojure jar to the classpath — the ‘-cp’ argument given to the JVM which specifies where in the filesystem the JVM can look for usable .jar or .class bytecode files — and calling clojure.main:

cd ~/clj/src/clojure
java -cp clojure.jar clojure.main

Voila! You should now have a Clojure REPL! Anything you type into the REPL will be read, evaluated, and the results printed immediately. This is a great way of getting feedback from the computer about your code as you go along. Try typing something into the REPL, such as this example from the Clojure Getting Started Page:

Take a few moments to play around with your new friend. When you are done, exit the REPL by pressing Ctl+D (end of file), or if you are less polite, Ctl+C (interrupt signal) to directly kill the jvm.

Leiningen

The third and final build tool is called Leiningen. It is basically a wrapper around the maven build tool that lets us specify dependencies and build procedures using clojure code instead of xml expressions. It is trying to put a friendly face on the huge, mighty beast called maven.

Leiningen is easily installed by downloading a small script to ~/clj/bin/lein, setting it to executable, and asking it to install itself.

Leiningen will take a few moments to download some jars and place them under ~/.m2 where maven-related files will be stored. This is normal behavior of the beast called maven.

For convenience, it’s a good idea to add the ~/clj/bin directory to your bash $PATH, so that any scripts in this folder can be executed from anywhere and we can type just “lein” instead of “~/clj/bin/lein”.

echo 'PATH=$HOME/clj/bin:$PATH' >> ~/.bashrc
source ~/.bashrc

Now you can check that Leiningen is installed:

cd ~/clj
lein version

If you see some version information, congratulations!

Emacs Files

Emacs connects very well with Clojure using Slime and Swank. In the end, the connection between you and the hardware looks like this:

User<->Emacs <-> SLIME <->Swank-clojure<->clojure<->JVM

For the unfamiliar, Slime is a program for hacking lisp that runs in Emacs, and swank is a server program that lets you interact deeply with Clojure. Breaking that down a bit:

Alternate Method: ELPA

Emacs, like most good operating systems (such as Ubuntu), has a package manager to help install extra functionality. Emacs’ package manager is called ELPA, and can be done from within emacs following these instructions, which amount to pasting the following in the *scratch* buffer, placing the cursor after the last parenthesis, and pressing C-j:

That was it. Emacs installed ELPA by itself, and you can now launch ELPA from within emacs by typing:

M-x package-list-packages

Scroll down the list that appears, tapping ‘i’ to select packages you want to install. Finally, then hit ‘x’ (for execute) to install everything at once. Useful packages you would be interested in include: clojure-mode, gist, htmlize, paredit, slime, and w3.

3 Comments for this entry

You can get more recent versions of clojure-mode, paredit, and other libraries using the technomancy archive source rather than the central ELPA one, as detailed in the swank-clojure readme: http://github.com/technomancy/swank-clojure

Also, be sure to remove the apt-get installed slime if you’re going to use the version from package.el.