Getting going with modern Common Lisp on Mac OS X

31 Jan 2011

Setting up for Common Lisp development on Mac OS X

High-level overview:

Download and install SBCL

There are several good Common Lisp implementations on Mac OS X. I happen to use SBCL because it worked better with lispbuilder-sdl last time I tried it, so that’s the one I cover here. Clozure CL (http://ccl.clozure.com/) is also very good.

Unfortunately, the SBCL maintainers don’t keep the pre-built Mac version up to date, so getting the latest SBCL is a two-step process on Mac OS X. First, hit SBCL’s download page and download both the latest source (1.0.45 at the time of writing) and the latest Mac binary (1.0.29). I’m using a 64-bit SBCL, but 32-bit should work fine as well.

Update: This has changed recently; the last several releases of SBCL have been pre-built for Mac OS X, so you can just grab one of those.

Now fire up Terminal, switch to where you downloaded SBCL, unpack and install it:

$ cd Downloads/
$ tar xjf sbcl-1.0.29-x86-darwin-binary-r2.tar.bz2
$ cd sbcl-1.0.29-x86-darwin
$ sudo sh install.sh

That’ll install SBCL into /usr/local/bin. At this point you should be able to run sbcl and get a Lisp prompt:

$ sbcl
This is SBCL 1.0.29, an implementation of ANSI Common Lisp.
More information about SBCL is available at <http://www.sbcl.org/>.
SBCL is free software, provided as is, with absolutely no warranty.
It is mostly in the public domain; some portions are provided under
BSD-style licenses.  See the CREDITS and COPYING files in the
distribution for more information.
*

Hit Control-D or type (quit) to exit.

Now you can run w/ that version, or if you want the latest/greatest you may need to build it. (Note: you may need Apple’s developer tools installed for this; I can’t remember.) Go back to your downloads directory and unpack the source distribution:

Solace:Downloads jfischer$ tar xjf sbcl-1.0.45-source.tar.bz2
Solace:Downloads jfischer$ cd sbcl-1.0.45
Solace:Downloads jfischer$ sh make.sh
Solace:Downloads jfischer$ sudo sh install.sh

Now you’ll have the latest and greatest SBCL installed.

Download and install Aquamacs

Aquamacs (http://aquamacs.org/) is an Emacs distribution customized to run nicely on Mac OS X. It obeys all the normal Mac shortcut keys, prints well, that sort of thing. Download it, open it up and drag the application to your Application directory as you would any other Mac app.

Download and install Quicklisp

Download Quicklisp from http://www.quicklisp.org/. (It’s in beta at the time this was written, but it’s fully functional and awesome). Download the install file (http://beta.quicklisp.org/quicklisp.lisp at the time of writing) and save it to disk somewhere easy to find.

Next, run sbcl and type in the following:

(load "/path/to/quicklisp.lisp")

After it loads, run:

(quicklisp-quickstart:install)

That’ll download the rest of the system and get it set up for you. Quicklisp will install by default in ~/quicklisp; you can change that by passing :path "/target/path/" to the install function.

Finally, run:

(ql:add-to-init-file)

That’ll add Quicklisp to your init file so that anytime you run SBCL Quicklisp will be loaded and ready to go.

Now go ahead and read on http://www.quicklisp.org/ about how to use it. It’s very easy to search for and install Common Lisp libraries. For example, to get ahold of “ieee-floats” for the previous entry, just run:

(ql:quickload "ieee-floats")

That will download the library if it hasn’t already and load it into your CL environment for you.

Configure everything so that it plays nice together

SBCL and Quicklisp are already playing nicely together at this point; you just need to let Aquamacs know about them.

First in SBCL run:

(ql:quickload "quicklisp-slime-helper")

This’ll install SLIME for you, an awesome Common Lisp development environment. It should give you a line to add to your .emacs configuration file:

slime-helper.el installed in "/Users/jfischer/quicklisp/slime-helper.el"

To use, add this to your ~/.emacs:

    (load (expand-file-name "~/quicklisp/slime-helper.el"))

Aquamacs will use .emacs, but they recommend keeping your configuration in ~/Library/Preferences/Aquamacs Emacs/Preferences.el. Either will work. You’ll need to both tell Aquamacs how to start your Lisp environment and add the above line to tell it how to find SLIME. To do that, add the following to your Preferences.el:

(setq inferior-lisp-program "sbcl")
(load (expand-file-name "~/quicklisp/slime-helper.el"))

At this point, you should be ready to go. To try it all out, launch Emacs, type Alt-x (Meta-x, technically), and type in “slime”. Hit enter and you should find yourself at a CL-USER prompt within Emacs.

Now, actually learning to use SLIME is well beyond the scope of this entry. For that, I recommend Peter Seibel’s Practical Common Lisp. Chapter 2 covers getting around both in Emacs and SLIME.