Wednesday, January 25, 2012

Setting up clojure emacs on windows

Setting up a Clojure development environment proved to be quite a challenge, especially given my handicap of using Windows.

The most widely used editor for Clojure is Emacs. I don't have any idea how to use it, but I have seen video of people who know what they are doing, and I really want that!

There are lots of options for setting up Clojure. If you are just starting out, there are too many. There are at least half a dozen editors that have Clojure plug-ins. Emacs itself has two. The instructions available also tend to present different ways of accomplishing the same thing.

I am not going to give you lots of options. I am going to give you the steps to go from a clean Windows 7 install to having the most popular plug-in for the most popular editor using the most popular build tool for Clojure. I am sure there is a lot of value to all of the flexibility that other people support, but it is lost on me and if you are reading this, it is probably lost on you too.

Also, I intend to cover each step in excruciating detail. I know from experience that any instructions on this subject, no matter how detailed, would include at least one or two steps that assumed I actually knew how to use Emacs. If you follow these instructions and there is a step that is not completely clear to you, please put it in the comments so that I can fix it. If the detail is too much for you, just read the bold print!

Our goal is to have a working installation of the Leiningen build tool, and the SLIME plug-in for the Emacs editor.

Before I start, let me say this works best if you do not already have clojure installed on your computer, or more specifically that you do not have the clojure.jar file in your class path. The Leiningen install will fail if you do.

Install the Java Development Kit (JDK )


Version 1.6 or better is required, I am running 1.7 Standard Edition. Download this from Oracle. There is also a link to installation instructions on that page.

After jdk is installed you need to make sure that the environment variables are set up correctly. (Control Panel >> System and Security >> System >> Advanced system settings >> Environment Variables...)

Under user variables I have JAVA_HOME with a path to the folder where the jdk is installed. On my system this is C:\Program Files\Java\jdk1.7.0_01

Under System Variables, the java bin directory needs to be in your path. click edit on the path, so you can scroll through and look to see if these settings are already there, and if not add them (path items are separated with ; on windows) %Java_Home%\bin\ and the physical path to the bin directory that holds the jdk executable. In my case this is C:\Program Files\Java\jdk1.7.0_01\bin\

Test the java installation by going to a command prompt (click the windows button in your taskbar and type cmd in the search box) at the command prompt type:
javac -version
if the computer responds with a version number the jdk is set up properly, if it says that javac is not found, you have a problem you need to fix before you move on.

Install Leiningen

(build tool)
Create a folder that will hold the leiningen batch file and also a helper utility it needs. Mine is in c:\lein. to create it I went to the command prompt, typed
cd \
md lein
After that type exit to close that command prompt window. Add the path to your new folder to your path setting.

Download curl from
http://www.paehl.com/open_source/?download=curl_723_1_ssl.zip
and place curl.exe and into your new folder.

download libssl from
http://www.paehl.com/open_source/?download=libssl.zip
and place libeay32.dll and ssleay32.dll into your new folder.

Download the lein.bat file from
https://raw.github.com/technomancy/leiningen/stable/bin/lein.bat
and put it in to your new folder. (My browser displayed the text, so to save it I did file >> save page as and then navigated to my c:\lein folder.

Open a new command prompt and type
lein self-install

After the installation completes you can test your leiningen install by typing
lein repl
at the command prompt. It should launch the clojure repl which you can test by typing
(+ 1 2)
at the user prompt.

click the x in the upper right to close the command prompt.

Install Emacs

(Editor)
Download emacs from
http://ftp.gnu.org/pub/gnu/emacs/windows/emacs-23.1-bin-i386.zip
extract the emacs-23.1 folder, and put it somewhere. I just put mine in c:\

Create a folder to hold plugins for emacs named .emacs.d I put mine in my emacs-23.1 folder.

Create a new user environment variable called HOME in the value put the path to the .emacs.d folder. in my case this is C:\emacs-23.1

Add the path to the emacs.exe folder to your path. mine was C:\emacs-23.1\bin

Install Clojure mode

Create a file called init.el in your .emacs.d folder and enter this text
(add-to-list 'load-path "~/.emacs.d/")
(require 'clojure-mode)
Add the file clojure-mode.el from
https://github.com/technomancy/clojure-mode/blob/master/clojure-mode.el
to your .emacs.d directory. I found the easiest way to do this was to copy the text in the code window at that url and paste it into a new text file that I called clojure-mode.el. If you just download the webpage, you will get lots of html commands that will cause errors in emacs.

Install Swank plugin

open a new command prompt and type
lein plugin install swank-clojure 1.3.4
-- note initially this install got hung for me, when I disabled avg link scanner it worked right away.

Create a new Clojure project from the command prompt, navigate to a folder where you would like to create clojure files. from c:\Users\Rick I typed
md projects
then
cd projects create the new project by typing
lein new testproj
then type
cd testproj
emacs

after emacs loads type
alt-x clojure-jack-in
emacs will spend a couple of moments processing the plugin. After this, you should have a running REPL that you can test the same way that you tested the repl from leiningen. type
(+ 1 2).
If you get 3, it works.

5 comments:

  1. Nice instructions. I recommend Emacs 24 over Emacs 23, I've been running it with no problems and package management is much better. You can get pre-compiled on Windows versions here, http://code.google.com/p/emacs-for-windows/updates/list. Also putting .emacs.d under the user HOME directory is better practice, see http://www.gnu.org/software/emacs/windows/Installing-Emacs.html#index-HOME-directory-49.

    ReplyDelete
  2. Thanks for the information. I saw a lot of places where people were using Emacs 24, but for whatever reason I couldn't find it. I assumed that it just wasn't available for Windows yet.

    Someone suggested on the Google group that I update the getting started guide on clojure.org if I found the documentation lacking. I will definitely check out moving the emacs.d folder before I do that.

    Now that I have it installed, I am trying to learn how to use Emacs. The tutorial that comes with Emacs is great, but do you know of any Clojure specific guides for Emacs?

    Thanks again,

    Rick

    ReplyDelete
  3. There are just bits and pieces here and there. If I were you I'd look up cheatsheets for paredit and slime. Here's a good start, http://stackoverflow.com/questions/2285437/a-gentle-tutorial-to-emacs-swank-paredit-for-clojure. Some information there is old, but take note of the Emacs starter Kit and anything else by Hagelberg.

    ReplyDelete
  4. Things have moved on... swank-clojure is now deprecated. Creating .emac.d in the emacs folder is interesting - starting emacs causes an error message not finding emacs_24.2/.emacs.d/ - note the underscore here. After this, a new folder emacs_24.2 appears, with its own .emacs.d folder. So I moved the above-mentioned files to the new .emacs.d folder and started emacs again. The error message "Symbol's value as variable is void: <!DOCTYPE"
    So I am stymied. The trouble with free software is that there are tall dependency columns and any alteration to any component - reorganisation, new version etc. causes the whole thing to collapse. It would be better if Clojure had a complete programming environment under the control of one organisation.

    ReplyDelete
  5. Thanks for this it has helped me a lot.

    ReplyDelete