Clojure and Leiningen to manage Clojure projects

Subscribe to my newsletter and never miss my upcoming articles

I would like to introduce you to Clojure. I am fascinated by how it works and it is also known to sharpen your thinking as a programmer. In this article, we'll do a basic setup and start programming in Clojure!

What is Clojure ?

Daniel Higginbotham in his book Clojure for the Brave and True has humorously defined Clojure as

an alloy of Lisp, functional programming and a lock of Rich Hickey's own epic hair

It combines Lisp dialect, powerful and expressive features of functional programming. Clojure is a hosted language meaning Clojure programs run on platforms like Java, Javascript and .NET and use their underlying features.


Leiningen is a useful tool to automate and manage your Clojure projects. Leiningen and Clojure require Java. Since, we are focusing on the JVM implementation of Clojure, OpenJDK will be required. You must have > 1.6 java version installed.

Follow the steps on to download Leiningen. It will automatically download Clojure compiler, clojure.jar

Creating a new project

lein new app clojure-project

After installing you would find the following files in the directory.

  • project.clj (like package.json of npm) contains project dependencies.
  • resources folder to save assets (like images).
  • src/clojure-project/core.clj will be where you write your code.

Running the project

(ns clojure-project.core

(defn -main
  "I don't do a whole lot...yet."
  [& args]
    (println "Hello, World!"))

This code will be already present in the src/clojure-project/core.clj file. If you have ever used C, C++ you would know namespace and the main function. Well, the defn -main is the starting point of your program.

To run, cd into the clojure-project and run:

lein run

You should see Hello, World! as the output!

Building the project

Clojure works even with environment change. It is not necessary that Leiningen is required to run the project elsewhere. To allow code shareability, we can create a stand-alone file that works where Java is installed.

lein uberjar

After running this command a stand-alone file would be created in a target/uberjar/ folder (same directory level as src). This jar file can be distributed on any platform.

Using REPL

REPL - read, evaluate and print loop. It is a tool that takes single line and executes the code as soon as it evaluates (just like Console in web developer tools or python prompt).

To start Clojure repl

lein repl

The prompt would look something like this:

nREPL server started on port 56969 on host - nrepl://
REPL-y 0.4.4, nREPL 0.7.0
Clojure 1.10.1
OpenJDK 64-Bit Server VM 1.8.0_242-b08
    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


Clojure follows prefix notation and everything resides in matching parentheses.

Samples to try out:

  • (+ 1 2 3 4 5) should add up all numbers and return 15
  • ( str "hello world" ) should print "hello world"
  • ( str "Hi! " "John " "Doe" ) should concatenate all the strings
  • ( * 1 2 3 4 5 ) should return multiplied value 120

Other Blogposts you would like to checkout!

No Comments Yet