• Articles
  • Github
  • Podcast
  • Articles
  • Github
  • Podcast
  • Setup Clojure in OS X

    27 Sep • 2016

    The following instructions are for installing Clojure on Mac OS X 10.11 (El Captain). Although they should work for any *nix based system.

    Dependencies

    Considering you already have Java and Brew installed (Java 1.5 or greater is require for running Clojure. This already comes pre-installed on OS X).

    Set the PATH

    The PATH variable tells the Terminal where to find the programs that are about to be installed.

    Go ahead and open the ~/.profile and make sure that the PATH variable is set and includes /usr/local/bin and /usr/local/sbin. You should have a line in the file that looks similar to the following, if not add it to the end of the file.

    export PATH="/usr/local/bin:/usr/local/sbin:$PATH"

    After save file, run the following command:

    source ~/.profile

    Scripting

    Donwload and unzip Clojure:

    curl -O -J -L http://repo1.maven.org/maven2/org/clojure/clojure/1.7.0/clojure-1.7.0.zip

    After unzip open clojure folder and test the build:

    java -cp clojure-1.7.0.jar clojure.main Clojure 1.7.0 user=> (+ 1 1) 2 user=> (System/exit 0)

    Let’s move jar file:

    mkdir /usr/local/lib/clojure && cp clojure-1.7.0.jar /usr/local/lib/clojure/clojure.jar

    rlwrap will make interacting with the Clojure REPL much more friendly. It is not necessary but I recommend it. With rlwrap you will have tab completion, parenthesis matching, and much more.

    brew install rlwrap

    Create clj script:

    vim /usr/local/bin/clj

    Copy the following text into the file:

    Make the script executable:

    sudo chmod +x /usr/local/bin/clj

    Now you should be able to run Clojure from the command line:

    clj

    clj REPL

    user=> (+ 3 5) 8 user=> (def A [[0 1 2] [3 4 5] [6 7 8]]) #'user/A user=> (let [[x y z] (first A)] (println (+ x y z))) 3 nil user=> (println "Hello, World!") Hello, World! nil

    clj run scripts

    clj clojure_script.clj

    And that’s it!! :)