Friday, June 20, 2008

Setting Up Clojure

(I added another reason why I like Clojure to my last post: community, reason #1 under "Why Clojure?". Check it out.)

Before we get started, we need to install Clojure and set up our environment to make the best use of it. This will involve installing Java, installing Clojure, creating some scripts to make calling Clojure easier, and setting up a text editor.

Below, I'll sketch out one quick way to get set up. There's more information on the Getting Started page and on the Clojure Wiki.

(These instructions are a little higher-level than I would like, for two reasons. First, how to do these things depends on your operating system. Second, if you have much computer experience, you probably already know how to do the tasks I gloss over below. But you may not. Grab a friend.)

Java

First, you may need to install a Java Runtime Environment. You may already have this installed, and if so, skip to the next section.

If you don't have Java installed or aren't sure whether you do, go to http://java.sun.com/javase/downloads/ and download the latest release of Java SE 6. Once you've downloaded it, double-click the installer to run it and install Java.

Depending on which installer you downloaded, at this point you'll either need to wait for Java to install or wait for it to download, then install. Either way, be patient.

Clojure

(If you're comfortable living on the edge and compiling Java with Maven, get the development code for Clojure from SVN and install that. I've been using the SVN version from the beginning, and it hasn't given me any trouble.)

Now you're ready to install Clojure. Go to its Sourceforge download page and select the latest release (20080612 at the time of this posting). Save it on your disk someplace you'll remember.

Now create a new directory named Clojure and unzip the file you downloaded into it. In that directory will be a file named clojure.jar. This contains the Clojure system.

Scripts

The Clojure JAR file isn't executable itself, so you'll want a script, which you can call from the command-line or double-click on, to run Clojure.

The easiest way to do this is to use the directory named Clojure, which contains clojure.jar as your working directory. Under UNIX/Mac OS-X, create a file in Clojure named clj and add these lines:

#!/bin/bash
java -cp clojure.jar clojure.lang.Repl

Of course, you'll need to set the permissions on this file to make it executable.

On Windows, create a file in Clojure named clj.bat and add these lines:

@ECHO OFF
java -cp clojure.jar clojure.lang.Repl

(Both of these assume that java is in your PATH. If it's not, you'll need to add it.)

Your start-up script can be a lot more complicated, of course. The Clojure Wiki has one that loads other libraries and can also be used to run scripts written in Clojure.

Double-click on or execute the file you created above and you should get a console window with this:

Clojure
user=>

Congratulations! Hit Ctrl-D to exit.

(Using the Clojure directory as your working directory is a good way to get started quickly, but its long-term merit is debatable. Set up the script so you can work from another directory. If that means nothing to you, find a friend who knows Java, and he can help.)

Editors

Because of the variety of editors out there, I'm punting on this one. The Clojure Wiki has information on setting up Emacs and Vim, and there is also enclojure, a plugin for the NetBeans IDE.

So far, I've been using Vim, and I haven't had any complaints.

No comments: