DEV Community

Cover image for Learning Clojure, part II
Камило Куньа
Камило Куньа

Posted on • Edited on

Learning Clojure, part II

Now you're convinced that is a good idea learn Clojure (and if you not I recommend to read the part I) we'll start the practical part installing the language and all the tools that we need for program in Clojure.

I'll not cover the process on Windows, most of things like SDKMan! only work on Linux/Mac Operating Systems, if you're using Windows I would recommend you to use WSL to follow this tutorial.

Install SDKMan! and Java

The first thing to do is install Java. Clojure is entirely written in Java (and Clojure itself) we need have Java installed and configured in our computer not only to run most of the tools that we use to program in Clojure, but also to run Clojure itself.

To do this we will install a bash tool called SDKMan! this is a version manager tool for all JVM languages (including our beloved Clojure). To install it we have to run the command in our terminal:

$ curl -s "https://get.sdkman.io" | bash
Enter fullscreen mode Exit fullscreen mode

After it complete the installation we have to source or open/close our terminal to load the changes in our ~/.bashrc. SDKMan is a collection of bash functions that are linked in our bash profile.

If you want to understand how the sdk function we'll use works you can use the following command on your terminal: $ describe -f sdk

Now we have to install Java on our machine. The actual Long-Term Supported (LTS) version of Java is the 17 we'll use it. In your terminal run the following command:

$ sdk install java 17.0.2-open
Enter fullscreen mode Exit fullscreen mode

All right! now you can check if Java is installed and running with the command:

$ java -version
Enter fullscreen mode Exit fullscreen mode

The SDKMan! not only helps you to install and configure Java as it also manages different JDKs on your computer if you want to learn more about it I recommend reading the documentation.

Install Maven and Leiningen

Now we have to install the build tools both for Java and for Clojure since we'll use them for manage dependencies and build our code. For our own luck SDKMan! also do this for us.

For install maven we have to run:

$ sdk install maven
Enter fullscreen mode Exit fullscreen mode

And for install leiningen we have to run:

$ sdk install leiningen
Enter fullscreen mode Exit fullscreen mode

After doing it we should have all we need to run Clojure. If you want to start doing things in REPL or just test the installation you can run it in you terminal:

$ lein repl
Enter fullscreen mode Exit fullscreen mode

By the way if you need to exit the REPL just run the function (exit) on REPL.

Installing VS Code and Calva

Wait... VS Code? Why not Emacs?

Well, a lot of people following this tutorial never learned Emacs before and I won't recommend learning Clojure and Emacs at the same time because things can become messy and confusing. After all, the complexity of the ecosystem and the elisp language be so similar that can be confusing.

By other way, probably you already have VS Code installed in your computer, and even if you don't you can just use apt or snap to install it.

$ sudo apt install snapd
$ sudo snap install –classic code
Enter fullscreen mode Exit fullscreen mode

After installing the IDE you can open it and install the Calva plugin it will add a lot of features to VS Code including things such as REPL integration and folding parens capabilities. To install it, open your VS Code press CTRL+P and paste this code into the open dialog:

ext install betterthantomorrow.calva
Enter fullscreen mode Exit fullscreen mode

After restarting your VS Code everything will be fine for we start the development in Clojure.

Hello Clojure!

After we have all set we can start programming in Clojure by building our first project. To do this you can just open a terminal in the folder where you want your project to be created and run the following command for leiningen create our project scaffold named getting-started based on app template:

$ lein new app getting-started
Enter fullscreen mode Exit fullscreen mode

And then we can open it on VS Code:

$ code getting-started
Enter fullscreen mode Exit fullscreen mode

The entry point of our project will be ./getting-started/src/getting-started/core.clj file that contains a Hello World program, let's change "Hello, World!" for "Hello, Clojure!" and then run the project with the command:

$ lein run
Enter fullscreen mode Exit fullscreen mode

If everything worked fine you should see the printed string printed in a newline in your terminal.

Using the REPL

One of the main advantages of using Clojure is the Read-Eval-Print-Loop (REPL) tool that helps a lot to get feedback while we're coding for work with Clojure is recommended that at least you learn to use the basics on it.

For start a REPL open the VS Code and press CTRL+ALT+C and the CTRL+ALT+J and then select leiningen and use the uberjar profile.

You will see a prompt with clj꞉getting-started.core꞉> open on the right of the code, this is our REPL window and you can run any Clojure code or part of the code you're writing on it:

clj꞉getting-started.core꞉>  (+ 1 1)
2
clj꞉getting-started.core꞉>  (-main)
Hello, Clojure!
nil
Enter fullscreen mode Exit fullscreen mode

At any moment you can also put the file cursor on the closing parenthesis in your file and run CTRL+ENTER and it will send the code to REPL and display the result of it.

If you want to learn more about how to send files to REPL, how to make a partial evaluation, and everything else in Calva I recommend you to look at the docs.

I'll be back

This is just the first step in the Clojure journey, now we installed and configurated our whole development ambient we're ready to start evaluating parenthesis on our next post.

Top comments (0)