Note: This blog post is no longer accurate as a lot of things have changed since I wrote this post. Please check this document for working instructions.
Clojure is a very modern functional programming language which runs on the JVM. It’s a very cleanly designed Lisp dialect and has all the features that any useful programming language would require. You can learn more about Clojure on the official website, the wikibook, a comprehensive article by R. Mark Volkmann or by buying the awesome book by Stuart Halloway.
Now to setup Emacs as a Clojure IDE. If you are new to Emacs then don’t worry as we will take the easiest way to set it all up. If you are a Emacs veteran, then keep your own dotemacs and the bunch of customisations that you have done in all these years aside and follow along.
Installing Emacs is easy. On an Ubuntu system, do this -
$ sudo apt-get install emacs-snapshot-gtk
That will install Emacs 23 for you. You will also need a few other things -
$ sudo apt-get install sun-java6-jdk ant git-core
You will need the JDK and Ant to build and run Clojure (it runs on the JVM, remember?) and Git to fetch the Clojure an other related libraries.
Now to setup Emacs, we will use a brilliant set of Emacs Lisp libraries aptly named Emacs Starter Kit (ESK). To get it, do this -
$ git clone git://github.com/technomancy/emacs-starter-kit.git ~/.emacs.d
That will get the ESK and will put it in your ~/.emacs.d (existing Emacs users, please move your ~/.emacs and ~/.emacs.d to some other location before doing this. You can get back your old settings later).
What ESK does is that it provides a bunch of sane defaults for pretty much everything in Emacs. You can safely use it and get a very usable Emacs setup almost immediately.
You can now launch Emacs by going to Applications > Accessories and selecting Emacs Snapshot Gtk. Once Emacs has started up, install the Emacs Clojure libraries by typing M-x package-list-packages. That will start up a buffer with a bunch of Emacs packages. Go to the line that says clojure-mode in it and press i. Then press x to install the package. Once that’s done you are ready to install Clojure. Now type M-x clojure-install and it will prompt you for a directory to install Clojure. Just press Enter for now and let it proceed. You can always change these things later.
That will fetch the Clojure runtime and also a few other useful libraries like clojure-contrib and will install them inside ~/src.
Once that is done, you can type M-x slime to start Clojure inside your Emacs. Play around with it and learn the various key-bindings for Slime and Paredit which is a fantastic system for doing Lisp development. Paredit is so good that it makes all those parentheses just vanish in thin air. But yes, it takes a bit getting used to which you can achieve with some practice. One tip is to type C-h m in Emacs to see the documentation of all the modes that are currently active. This gives a good overview of all the key-bindings and the functions bound to those keys.
To develop slightly more complex Clojure applications, you need to use different dependencies with your own code. Since Java has its own classpath related quirkiness it can become a slightly complicated task. But then, there is cool function in Emacs Starter Kit called clojure-project which will make your life easy.
To use it all you need to do is to follow this simple directory structure and the rest will be taken care of automagically. The directory structure should be like this -
myproject
|-- lib
|-- src
|-- target
| |-- classes
| `-- dependency
`-- test
Copy the Clojure & Clojure Contrib (or any other dependency) JAR files into lib (you also alternately use a build system like Maven to manage dependencies for you in which case you should consider configuring the build system to copy the dependencies into target/dependency). Keep all your code inside src and tests inside test. The src and test directories should contain namespaces like foo.bar & foo.bar.test respectively (you can later use clojure-test-mode to automatically pick up the tests and run them quickly). So when you run M-x clojure-project, it will ask you for a directory name. Give it the name of the project directory and boom! Emacs will launch Slime with the right classpath settings.
If you want to customise any Emacs setting, just create a file with your current system login id called <username.el> inside ~/.emacs.d and it will be automatically loaded when you restart Emacs. If you use additional libraries, create a directory with your username inside the same directory and everything inside will be in the Emacs load-path. Nifty, right?
So that’s all required to setup Emacs for Clojure. Join the Clojure mailing list to communicate with the Clojure community. Do check the Emacs Wiki in case you have any queries about Emacs.
Above all, have fun. That’s what we are here for.
By the way, I am @ghoseb on Twitter and I think you should follow me
UPDATE: Fixed a factual inconsistency regarding the paths used for clojure-project. Thanks to Phil (the creator of ESK & clojure-project) for pointing this out!
Tags: clojure, emacs, functional programming, git, jvm, lisp, Programming, Ubuntu
Thanks for this detailed overview!
One thing I’d clarify is that generally you don’t use lib/ and target/dependency together; lib is meant for when you want to manually manage your deps, and target/dependency is for if you are using mvn or corkscrew to handle dependent jarfiles. Of course, there’s nothing stopping you from using them together, it’s just not the intended use. Otherwise it seems right-on.
Phil: Whoops! That just slipped off my mind. I didn’t intend to write that
I guess I was too sleepy.
Thanks a lot for pointing it out. I will fix it right-away.
Thank you for this description. However, I ran in some problems and it would be great if you can help me with them.
I tried the steps above and managed to get working clojure under slime, however when I restarted emacs M-x slime didn’t work anymore. Perhaps I need to put something in the init files? I tried to put (clojure-slime-config) in my init file, but it didn’t work.
Ivant: Create a file with your username (for eg. ivant.el) and put it in your ~/.emacs.d/ folder.
In that file put this (clojure-slime-config). That would work if you had chosen to install Clojure in the default directory (~/src/). Else put this in the file instead (clojure-slime-config “/path/to/root/of/clojure”).
That should do the trick.
It works when I explicitly provide the path, even though it is the default one:
(clojure-slime-config “/home/ivant/src”)
Thanks!
Just to get this up to date,
After installing the starter-kit, start emacs, let it complete processing and then restart emacs
Instead of clojure-mode, install swank-clojure
M-x slime and it should automatically download clojure.
I also like using leiningen as my project builder
Did all, but “M-x clojure-install” didn’t work. (“No match”, i.e. this function did not exist.)
System: fairly recent stable release of Linux Mint.
Emacs: 23.1.50.1
The instructions have changed now. Check the comment above by Sidhant. Also, read this – http://www.assembla.com/wiki/show/clojure/Getting_Started_with_Emacs