Finally writing something in Clojure: The Setup

I’m going to take us back a little bit, to the time before I decided to write something in Clojure, and explain how I got Clojure to run. It was most certainly not trivial. It’s not that any single step of the process is insurmountable, but figuring out what to install can be a pain.

I think Python is a model of how programming language setup should go. You download the binary from the site or install the package from the manager, set your path, and then you have a new command, python, that you can run on .py files. Just run the command without giving a file, and you get an interactive prompt.

I think Clojure is a model of how programming language setup should not go. If you download the jar file from the web site, you basically get a Java library. You have to build the rest of the environment yourself—right down to a repl that has command history.

Other people, fortunately, have done that work for you, by integrating Clojure with other development environments. You can pick the 1970s route, using Emacs with clojure-mode and paredit or Vim with Vim-Clojure, or you can pick the 1990s route of giant, heavy, desktop-crashing IDE with Clojure plugin. Since I was going through an “I hate the 70s” phase when I first installed Clojure, I took the 90s route and tried using Eclipse with the Counterclockwise plugin.

It’s a pretty nice environment, as far as functionality. It includes a nice repl with command history, as well as the Clojure compiler and the usual project management and code completion tools. But as usual, Eclipse barfed on me and couldn’t get off my couch, forcing me to cut the couch into kindling. In fact, part of the reason I didn’t do anything with Clojure for so long was that Eclipse got drunk, barfed and crashed my computers (all of them) nine times out of ten, and would have also done so the tenth time except I was called to dinner before it had the chance.

After my operating systems teacher basically forced us to use Emacs for our assignments, I decided maybe it wasn’t so awful, and after succumbing to peer pressure from reading Coders at Work, I declared an “I Love the 70s!” phase and started using Emacs. Emacs has nice syntax highlighting, and Paredit is awesome once you get used to it. (It’s a cussed pain before you get used to it, and it has an annoying habit of deleting all your text if you hit the wrong key combi, so make sure to keep backups.)

Emacs doesn’t include the Clojure compiler, so you need something else to build with. The most commonly used Clojure build tools are Leiningen and Maven; I went with Leiningen, because it’s the most sparkly and mustachioed build tool. (If Colonel Mustang had a Clojure build tool, I might have gone with that one instead, though.) Unlike many Clojure utilities, Leiningen seems to be pretty well documented. There’s a very nice tutorial on its GitHub page, and lots of help on Stack Overflow and elsewhere. It does basically what Eclipse does as far as managing projects and files, except without barfing. In particular, Leiningen will download the Clojure jar for you and make sure your classpath is set up correctly. (If you don’t understand Java classpaths, don’t feel bad, because neither do I. I let Leiningen handle that stuff for me. But it’s similar to the operating system’s path variable.) It can also add any other libraries you might want–I used it to add Clojure’s Enlive library for HTML manipulation, and the data.json library for reading JSON files.

One of the other nice features of Leiningen is that its configuration files are all written in Clojure. When you make a new project, the root directory will contain a project.clj file that has Clojure code you can edit to change the properties of your project. This is where you specify what libraries you want, including the version of Clojure you want to use. (If you need to know the exact syntax of the version string, most Clojure libraries are on GitHub, and most of the developers have a nice habit of putting the string for the latest version at the top of their readme.) Leiningen will download them all for you and make sure they’re accessible from your own code; all you have to do is import the namespaces (which can also be nontrivial, but we’ll talk about that later). Java’s main build system is Ant, which uses XML to specify the project configuration. XML is pretty verbose compared to Clojure, and you have to learn Ant’s dialect of XML to get anywhere. That’s still preferable to the abomination that is Make, though. But with Leiningen it’s all Clojure, so there’s no new syntax to deal with.

So, I went the total Linux weenie route and used Emacs and Leiningen. My hardware is usually sort of outdated, so I just couldn’t run Eclipse, but if you can and aren’t a total Linux weenie, you might try Eclipse. Netbeans also has a plugin, Enclojure, although I don’t know anything about it. Leiningen’s not too bad, though, so if you want something lighter than a big giant IDE, but don’t want to relive the days of disco and Charles Manson, using Leiningen with another editor (such as JEdit, which also has a Clojure repl plugin) is also an option.

The difficulty of getting everything set up is, I think, a sign of Clojure’s immaturity. It’s still a very new language, even if it is building on the ancient Lisp legacy. But things are improving; Leiningen is very nice, and I’m interested in checking out Light Table sometime. Someday I imagine we’ll have a Clojure setup process that’s as easy as Python’s.

Advertisements