I’ve had a few false starts in getting a good Clojure installation working. I know there’s a lot of information about installing Clojure, including this tutorial that I used as a basis for my own installation on Arch Linux. However, I still think that people can benefit from my troubles, so here we go:
Before You Install
Install Git. On Arch Linux, pacman -S git on ubuntu, apt-get install git-core
Installing Java
If you already have a working Java installation, you can skip this step.
This is interesting. There are lots of alternatives to Sun’s JDK and JRE, specifically OpenJDK. However, I’m a fan of using the actual Sun products. On Arch Linux, they can be installed with pacman -S sun-jdk sun-jre If your Linux distribution doesn’t have Java in a repository, you can download it here and extract it somewhere (I like to put complete distributions of large programs in /opt/). Then, you just need to set your PATH environment variable to include the /bin folder of your java installation. This is best done by adding the following lines to your ~/.bash_profile or ~/.bashrc:
export PATH="$PATH:"
Example:
export PATH="$PATH:/opt/java/bin"
Understanding Java’s Import
This is what took me the longest to get, as a non-Java programmer. In Java, you don’t import or require files individually like you do in most other languages. Instead, Java uses an environment variable called CLASSPATH, much like Linux’s PATH variable, to determine where to look for files. If you have /usr/share/java in your CLASSPATH, for example, you would call a file like
/usr/share/java/lol/cats/catterizer.java
with (in Clojure)
(include 'lol.cats.catterizer)
JAR files, on the other hand, have to be included in your classpath directly to get at their contents, like so:
export CLASSPATH="$CLASSPATH:/usr/share/java/lolcats.jar"
or, java -cp "/usr/share/java/lolcats.jar" file-to-execute
You can then access the classes inside those JAR files just like they were directories in your CLASSPATH (which they sort of are).
Understanding Clojure’s CLOJURE_EXT
Luckily, there is a pretty cool script for launching Clojure (we will be using this script, which is part of clojure-contrib, in this guide) that lets us use a much better system with a variable called CLOJURE_EXT. The way it works is simple: all the folders and JAR files in the folder specified in CLOJURE_EXT are added to the Java classpath when you run Clojure via the script. This means that you don’t have to include all sorts of JAR files and stuff in your classpath manually, which is great!
Installing Clojure
This part is actually really simple. Just clone Clojure’s Git repository somewhere. For example, I like my Clojure to be in its own directory in /opt/ (for easy removal), so I would do the following in the terminal:
cd /opt/
git clone git://github.com/richhickey/clojure.git
Now we’re going to use a program called Ant to build Clojure into a JAR file for use. If you don’t have Ant, you can install it with
apt-get install ant or (on Arch) pacman -S apache-ant Note that on Arch, the Ant installation doesn’t seem to add “ant” to your PATH, so you can call it like this:
/usr/share/java/apache-ant/bin/ant
Then, just run
cd /opt/clojure
ant
You should see a file called “clojure.jar” when it’s done.
If you don’t have Git, you can download a pre-built version of Clojure here.
Tada! Clojure is installed. (But don’t stop just yet!)
Installing Clojure-Contrib
Clone the clojure-contrib git repository somewhere (once again, I prefer /opt/):
cd /opt/
git clone git://github.com/richhickey/clojure-contrib.git
Building clojure-contrib is easy, with one twist: we want to specify where clojure.jar is, because clojure-contrib depends on it:
cd /opt/clojure-contrib
ant -D="/opt/clojure/clojure.jar"
Another JAR file for you! Great!
Configuring Clojure So It Works
In your clojure-contrib/launchers/bash/ directory, you should see a file called “clj-env-dir”. This is the shell script that makes the magic CLOJURE_EXT happen. We want to make an alias for this (generally, people make it “clj”), so that we don’t have to type it all out (“/opt/clojure-contrib/launchers/bash/clj-env-dir”) every time we run it. So, let’s do that: Add the following to your .bashrc or .bash_profile:
alias clj="/opt/clojure-contrib/launcher/bin/clj-env-dir"
Almost done. Now, we have to put a directory in our CLOJURE_EXT variable, for our script to look for files. Generally, “~/.clojure” is a good choice (but you can do whatever you want). To add a folder to your CLOJURE_EXT variable, put the following in your .bashrc or .bash_profile:
export CLOJURE_EXT="~/.clojure"
Finally, we need to put clojure.jar and clojure-contrib.jar in this folder. I recommend doing this with symlinks:
mkdir ~/.clojure && cd ~/.clojure
ln -s /opt/clojure/clojure.jar clojure.jar
ln -s /opt/clojure-contrib/clojure-contrib.jar clojure-contrib.jar
If you don’t know what we did there, I highly recommend you read up on symlinks. Symlink stands for “symbolic link” and it lets you make a reference to a file in a place other than where the file is. Even cooler, the reference acts just like the file: edit the link, and the referenced file is edited! It’s great. Anyways…
Test Clojure
Well, now it should be as simple as running
clj
to get a REPL (Read-Eval-Print-Loop), which lets you execute Clojure statements for testing and stuff. If you want to run a script, just do
clj script-name.clj arg1 arg2
Easy! However, if you’ve used Clojure’s REPL much, and particularly if you are used to the REPLs of other languages like Python, Ruby, and Common Lisp, you will see that it is lacking. We can fix that.
Fixing the REPL
In order to make Clojure’s REPL support goodies like the arrow-keys working and command history, we’re going to pass it through JLine. Download the JLine JAR from here, and save it to your ~/.clojure (or wherever).
The only problem now is, how do we make it so that Clojure is run through JLine only when we are going to use the REPL? Fire up your favorite editor and open the clj-env-dir script (/opt/clojure-contrib/launchers/bash/clj-env-dir). After the comments but before any other code, add the following lines, which set Clojure to run through JLine only if there are no arguments passed to the script:
if [ -z $1 ]; then
CLOJURE_MAIN="jline.ConsoleRunner clojure.main"
fi
Libraries! Need Libraries!
Relax, relax! Just stick the JAR files into ~/.clojure, then you can include them right off! For example: jFreeChart. Good for data analysis! Stick all of its JARs into ~/.clojure, then in your clojure scripts you can just include it:
(include '[org.jfree.chart ChartFactory])
Now, how do I use Clojure?
I’m still working on this one. For now, check out the main site, the API reference, or some tutorials. Actually, someone’s already done this: check out Matt Sears’ blog for a great list of links.
