Clojure is another dialect of Lisp programming language that targets the Java Virtual Machine. Clojure is a general-purpose language that emphasizes on functional programming. Clojure is also an Open-Source language with its development being heavily community-driven.

In Clojure working with immutable data structures and explicit progression-of-time constructs is one of its key features. This is why developing robust programs, e.g. multithreaded ones, is one of its strong suits.

Installation on Arch Linux

Clojure is provided straight from Arch’s package manager pacman. To install Clojure run:

$ sudo pacman -S clojure

Official Installation of Clojure on Linux

Clojure is relatively new programming language so many platform installers (e.g. apt-get, etc.) don’t provide installation for Clojure yet. To install Clojure on other distributions than Arch Linux, you need to make sure that following dependencies are installed: bash, curl, rlwrap and Java.

After you’ve installed those dependencies clojure.org provides installation script for Clojure, that creates /usr/local/bin/clj, /usr/local/bin/clojure, and /usr/local/lib/clojure to your system. To run this script, run:

$ curl -O https://download.clojure.org/install/linux-install-1.9.0.348.sh

It is recommended the check what you are installing when downloading from not official repositories. To check what the script contains, open it with your editor of choice or use less etc.

$ emacs linux-install-1.9.0.348.sh

After you’re sure what you are installing, run:

$ chmod +x linux-install-1.9.0.348.sh

$ sudo ./linux-install-1.9.0.348.sh

Testing Clojure

After the installation you can test the working of Clojure by running:

$ clojure

This will then open the built-in REPL, in which you can run Clojure commands. For example:

user=> (println "Hello, World!")
Hello, World!
nil

Or print the same message to GUI window using Java Swing library:

user=> (javax.swing.JOptionPane/showMessageDialog nil "Hello World")

Clojure uses the file extension of .clj. So if you want to create a Clojure program you can do it by:

$ emacs hello.clj
(println "Hello, World!")

To run and compile that program, run:

$ clojure hello.clj
Hello, World!

And your Clojure seems to be working correctly.

Leiningen

Leiningen is one of the more popular build tools for Clojure, the other one being Boot. Leiningen focuses on project automation and declarative configuration.

Installation on Arch Linux

Leiningen is provided in the Arch User Repository. To install it, run:

$ git clone https://aur.archlinux.org/leiningen.git.

It is also recommendended to read what you’re installing before the installation, with your editor or choice or less:

$ cd leiningen

$ emacs PKGBUILD

$ emacs leiningen.install

After you’re sure about what you’re installing, run:

$ makepkg -si
  • -s stands for --syncdeps which automatically resolves and installs any dependencies with pacman.

  • -i stands for --install

If you’re not running Arch, it is recommended to check this list before installing it the official way.

Official Installation

Leiningen can be installed by placing lein script to the $PATH where your shell can find it, e.g. /usr/bin.

After that give set that script to executable by chmod a+x /path/to/lein, e.g. chmod a+x /usr/bin/lein.

Then just run lein and it will download the self-install package.

Creating a Lein Project

When creating a new Lein project it will automatically great a Hello, World function in it, which is often recommended to run first before any development.

$ lein new app hello
Generating a project called hello based on the 'app' template.

$ cd hello

$ find .
.
./src
./src/hello
./src/hello/core.clj
./.hgignore
./LICENSE
./doc
./doc/intro.md
./test
./test/hello
./test/hello/core_test.clj
./README.md
./CHANGELOG.md
./.gitignore
./project.clj
./resources

Running the code in it inside Leiningen REPL:

$ lein repl
nREPL server started on port 33101 on host 127.0.0.1 - nrepl://127.0.0.1:33101
REPL-y 0.3.7, nREPL 0.2.12
Clojure 1.8.0
OpenJDK 64-Bit Server VM 1.8.0_162-b12
    Docs: (doc function-name-here)
          (find-doc "part-of-name-here")
  Source: (source function-name-here)
 Javadoc: (javadoc java-object-or-class-here)
    Exit: Control+D or (exit) or (quit)
 Results: Stored in vars *1, *2, *3, an exception in *e

hello.core=> (-main)
Hello, World!
nil

Or you can run the code as a .jar:

$ lein uberjar
Compiling hello.core
Created /home/topi/Projects/Clojure/hello/target/uberjar/hello-0.1.0-SNAPSHOT.jar
Created /home/topi/Projects/Clojure/hello/target/uberjar/hello-0.1.0-SNAPSHOT-standalone.jar

$ java -jar target/uberjar/hello-0.1.0-SNAPSHOT-standalone.jar 
Hello, World!

Further Readings