Sunday, August 22, 2010

Clojure REPL for command line use

Each time I setup a new box, or want to upgrade to a new version of Clojure (like 1.2, that was released quite recently), I have to remember how I've set it up in the past, and what needs to change. This time I'm writing it down (1.2.0 is the current version).

This is not the simplest way to launch Clojure, for that use java -cp clojure.jar clojure.main, it's just the way I like having it setup. It's mostly adapted from the Getting Started section in the old (?) Clojure Wikibook.

Clojure REPL with rlwrap support, clojure-contrib on the classpath, and using the clojure-contrib launcher:

  1. Put clojure in /usr/local/clojure-1.2.0
  2. Put clojure-contrib in /usr/local/clojure-contrib-1.2.0
  3. Symlink: /usr/local/clojure-contrib -> /usr/local/clojure-contrib-1.2.0 (for my rlwrap script)
  4. Make sure rlwrap is installed (apt-get install rlwrap on Ubuntu)
  5. ~/bin/clj exists, is executable, and looks like this:
    #!/bin/sh
    BREAK_CHARS="(){}[],^%$#@\"\";:''|\\"
    rlwrap -r -c -b $BREAK_CHARS -f $HOME/.clj_completions /usr/local/clojure-contrib/launchers/bash/clj-env-dir "$@"
    
  6. See Clojure wikibook for generating .clj_completions.
  7. Relevant environment variables (.bashrc or whatever you like):
    export CLOJURE_EXT=~/.clojure                                                                                                                                                
    export CLOJURE_OPTS="-Xmx128m -server"
    
  8. Make sure ~/.clojure ($CLOJURE_EXT) exists and contains symlinks to Clojure and contrib:
    ~/.clojure$ ls -l
    total 0
    lrwxrwxrwx 1 mark mark 59 2010-08-22 21:33 clojure-contrib-1.2.0.jar -> /usr/local/clojure-contrib/target/clojure-contrib-1.2.0.jar
    lrwxrwxrwx 1 mark mark 36 2010-08-22 21:48 clojure.jar -> /usr/local/clojure-1.2.0/clojure.jar