Getting Started with Emacs and Clojure on OS X

I’ve been learning Clojure in my spare time and coming from a predominantly Python-focused career, I wanted to chronicle my experiences with learning Clojure and integrating it within my Emacs config.

This article will be geared towards beginners, like me, looking to get started with Clojure and Emacs on OS X.

  1. Install Homebrew -

I won’t go into too much detail here, aside from providing the link where you can find all the resources on installing this. I highly recommend using Homebrew for your packaging needs on OS X. The one liner to install looks like this:

ruby -e "$(curl -fsSL"

We’ll be using Homebrew to install some of the necessary programs and libraries.

  1. Install Homebrew Cask

Next, we’ll need to install Java, as Clojure runs on the Java Virtual Machine (JVM). You’ll need to install the Java Development Kit (JDK).

Java from Oracle

You can go to the Java website above to download it and install manually, but I prefer to let package managers do that for me. It is recommended to download at least version 6 of the JDK.

For this, we can install the JDK through Homebrew Cask, which we will need to install:

brew install caskroom/cask/brew-cask

Homebrew Cask is really useful for managing application installs. No more dragging to your Applications folder anymore!

  1. Install Java

Now that we have Homebrew Cask installed, we can install the JDK by simply doing:

brew cask install java

This will pull the latest version of the JDK according to Homebrew Casks and install it on your machine. It should prompt you for your password to proceed with the install.

  1. Install Leiningen

Leiningen is a user interface to the Clojure library that helps you automate projects and manage dependencies, while fully setting up lein and Clojure for you.

brew install leiningen

You now have Clojure installed!

  1. Configure Emacs

This is mostly an optional step if you haven’t done this already. Make sure you have something like this in your Emacs config:

(add-to-list 'exec-path "/usr/local/bin")

If your Homebrew install is standard, this is where your binaries should be located. If not, adjust the path as necessary.

  1. Install Emacs packages cider

m-x package-install RET cider

Optionally, if you want to do 4clojure problems in Emacs, also install 4clojure.el:

m-x package-install RET 4clojure

  1. Add a profiles.clj

In your home directory, create a new profiles.clj in ~/.lein/ and put this barebones config to start with:

{:user {:plugins [[cider/cider-nrepl "0.9.0-SNAPSHOT"]]
        :dependencies [[org.clojure/tools.nrepl "0.2.10"]]}}

*Note - modify the cider-nrepl and tools.nrepl versions if you have issues. It should match the versions you have installed above.

And there you have it! You should be able to fire up Emacs and start a Clojure REPL through cider and even answer a few 4clojure questions as you dive into Clojure. Moving forward, I’m going to try and chronicle my adventures learning Clojure.

Happy hacking!