Clojure on Ubuntu

Install Java

Install Java if not currently available.


~$ sudo apt-get install openjdk-6-jdk

Install Ant

Install Ant if not currently available.


~$ sudo apt-get install ant

Install Git

Install Git if not currently available.


~$ sudo apt-get install git-core

Install Clojure

Check out the current version of clojure, compile it and copy it to ~/.clojure.


~$ mkdir ~/opt

~$ cd ~/opt

~$ git clone git://github.com/richhickey/clojure.git

~$ cd clojure

~$ ant

~$ mkdir ~/.clojure

~$ cp clojure.jar ~/.clojure

Test Clojure

Gentlemen, start your REPLs.


~$ cd ~/.clojure

~$ java -cp clojure.jar clojure.lang.Repl

user=> (+ 1 41)

42

Ctrl-d will exit the REPL.

Install clojure-contrib

clojure-contrib is the offical clojure language extension library. It also includes a start-up script for launching a REPL with various java options.


~$ cd ~/opt

~$ git clone git://github.com/richhickey/clojure-contrib.git

~$ cd clojure-contrib

~$ ant -Dclojure.jar=../clojure/clojure.jar

~$ cp clojure-contrib.jar ~/.clojure

Configure Bash Start-up Script

clojure-contrib contains a bash script called clj-env-dir for starting clojure with various options. Edit your ~/.bashrc file to configure this script.


~$ nano ~/.bashrc

### Add the following lines to the bottom of .bashrc

export CLOJURE_EXT=~/.clojure

PATH=$PATH:~/opt/clojure-contrib/launchers/bash

alias clj=clj-env-dir

### Save and exit the file (Ctrl-o, Ctrl-x)

The last line added to the file above sets an alias to the clj-env-dir script. This example uses clj but it could be set to anything.

Stephen Gilardi contributed this script and also documented how the environment variables are used. The example above only uses the required CLOJURE_EXT variable but three optional variables can also be used.


# Environment variables:
#
# Required:
#
# CLOJURE_EXT The path to a directory containing (either directly or as
# symbolic links) jar files and/or directories whose paths
# should be in Clojure's classpath. The value of the
# CLASSPATH environment variable for Clojure will be a list
# of these paths followed by the previous value of CLASSPATH
# (if any).
#
# Optional:
#
# CLOJURE_JAVA The command to launch a JVM instance for Clojure
# default: java
# example: /usr/local/bin/java6
#
# CLOJURE_OPTS Java options for this JVM instance
# default:
# example:"-Xms32M -Xmx128M -server"
#
# CLOJURE_MAIN The Java class to launch
# default: clojure.main
# example: clojure.contrib.repl_ln

These variables would be set using the export option in the .bashrc file as noted above.

Test Script

To test the new script and verify access to the clojure-contrib library, open a new terminal window and try this:


~$ clj

### That should have started a REPL.

### Now, verify the jars on the class path.

user=> (System/getProperty "java.class.path")

### Both clojure.jar and clojure-contrib.jar should be present.

If any other jars are needed either copy or link them to the ~/.clojure directory.

Updating Clojure and clojure-contrib

Updating clojure and clojure-contrib is easy in this basic setup.


### Update clojure

~$ cd ~/opt/clojure

~$ git pull

~$ ant

~$ cp clojure.jar ~/.clojure

### Update clojure-contrib

~$ cd ~/opt/clojure-contrib

~$ git pull

~$ ant -Dclojure.jar=../clojure/clojure.jar

~$ cp clojure-contrib.jar ~/.clojure