Set up Lisp on your Linux or Mac – Easily!

Note: This post is now 1.5 years old. It uses OS X 10.4, and so may be out of date :(

Common Lisp for Common Folk

Many people in the CS community might associate Lisp with a certain snobbishness or air of superiority.

This has nothing to do with the supposed “power” or “versatility” of the language, nor does it relate to “parse trees”, “macros”, and “code=data”.  No, the actual reason that Lisp is placed so high above the rest of us is that you have to be a Lisp-hacker just to figure out how to install Lisp on your computer.

So: Here are the simple steps required to install and use Common Lisp, spelled out for those of us who aren’t already Lisp-fluent. There is a chance these steps will work for you, but no guarantees: I swear they make it tough on purpose.

Some notes before we begin: On Linux, I am using Ubuntu 10.10.  These steps should work similarly on any other Linux, but the way you install programs and your file paths may differ a bit.  On Mac, I am using Snow Leopard.

Overview

First, we need a compiler/interpreter for Common Lisp. We’ll be using SBCL. Why this particular implementation? Cause it’s free and it works. If you prefer to use another instead, download that instead and modify the steps appropriately.

Second, we’ll ensure we have emacs, and then install Slime to go with it.
Finally, we’ll edit the emacs configuration file so that it will work with Slime and SBCL.

Installing on Linux Ubuntu:

  1. Get SBCL. From the command line, run:
    sudo apt-get install sbcl
    For all OSes, you can also download SBCL from their website (just click on the appropriate green box).
  2. Now you have a working Lisp Interpreter!  You could be done right now!  From the command line, just run sbcl and you’ll be in a working Lisp environment.  Try running (format t "Hello World!") or run (sb-ext:quit) to exit.
  3. Get emacs and Slime. Run:
    sudo apt-get install emacs
    sudo apt-get install slime
    For all OSes, you can also download and install Slime from the website.
    On Linux, odds are good you already have emacs, but you probably want to make sure it’s the latest version just in case.
  4. Configure emacs to play nice with Slime. Open the file ~/.emacs (create it if it doesn’t exist) and add the following lines:
    ;; use SBCL and Slime with lisp
    (setq inferior-lisp-program "/usr/bin/sbcl")     ; tell emacs where the lisp interpreter is
    (add-to-list 'load-path "/usr/share/emacs/site-lisp/slime")     ; tell emacs where slime is
    (require 'slime)
    (slime-setup '(slime-fancy))  ; load almost all slime packages (minimalist: use (slime-setup))

    You must make sure to change the paths in lines 2 and 3 to match your computer. For me, sbcl is located at “/usr/bin/sbcl” and the slime folder is at “/usr/share/emacs/site-list/slime”, so that’s what I put in the file. Yours are probably similar but you may have to search around a little to figure them out.

Installing on Mac OS X (Snow Leopard recommended)

  1. You’ll need to install MacPorts if you don’t have it.  You can download Macports here.
  2. Install SBCL.  Download SBCL from their website (just click on the appropriate green box).
  3. Install emacs: Luckily, there is a site just for you.  Download and install.  We also need to be able to use the “alt” or “option” key as the meta key. In Snow Leopard, you can do so this way: With Terminal Open, go to the Terminal menu and click Preferences. Under the Settings tab, find the Keyboard tab, and at the bottom is a check box: “Use option as meta key.”  Check it.
  4. Install Slime. Download the file from their website and expand it to a folder. Mine was named “slime-2011-02-20”. Move this folder somewhere safe. I put mine in “/usr/share/emacs/site-lisp/” just like on Linux. Note /usr is an invisible folder in Macintosh HD.
  5. Configure the ~/.emacs file to work with Slime (create it if necessary; remember it’s an invisible file).  Mine looked like this:
    ;; use SBCL and Slime with lisp
    (setq inferior-lisp-program "/opt/local/var/macports/software/sbcl/1.0.45_0/opt/local/bin/sbcl") ; tell emacs where the lisp interpreter is
    (add-to-list 'load-path "/usr/share/emacs/site-lisp/slime-2011-02-20")     ; tell emacs where slime is
    (require 'slime)
    (slime-setup '(slime-fancy))  ; load almost all slime packages (minimalist: use (slime-setup))

    The strings on lines 2 and 3 are where my “sbcl” program and my slime folder are located. You’ll need to change them to match where your files are located.

Done!

Nice work.  To try Slime out, go to the command line and run
emacs
to open up emacs. If you type Alt-x slime (that is: hold alt while pressing x, then type slime, then press enter), you should be inside Slime. Try (format t "C++ rules!"). Press Ctrl-x Ctrl-c to quit.

If these steps didn’t work for you, leave a comment and let me know!  If they did work, leave a comment and send cash!

Link of the day: Chess + Music + 80s= Pure Awesomeness

Advertisements

14 comments on “Set up Lisp on your Linux or Mac – Easily!

  1. Anonymous says:

    a bit of a stupid question, but after removing and then reinstalling emacs (via http://emacsformacosx.com) I can no longer start it from the command line. ?

    • bowaggoner says:

      Hmmm. I think perhaps emacs for macosx is only an “application”, and not a command-line program. Because when you install it, you just drag “Emacs.app” into the Applications folder. But to run a program from the command line, usually it has to be in the folder /usr/bin. I’m not sure if you can do that with emacsformac or not…. So you may have to install it in a different way to be able to do that.

  2. anirban mandal says:

    I followed the steps like you have mentioned.Finally,when I type M-x slime this message is returned……………
    BSD-style licenses. See the CREDITS and COPYING files in the
    distribution for more information.
    *
    ; loading #P”/usr/share/common-lisp/source/slime/swank-loader.lisp”

    debugger invoked on a SB-C::INPUT-ERROR-IN-COMPILE-FILE in thread #:
    READ failure in COMPILE-FILE:
    SB-INT:SIMPLE-READER-PACKAGE-ERROR at 5611 (line 143, column 52) on #:
    package “ASDF” not found

    Type HELP for debugger help, or (SB-EXT:QUIT) to exit from SBCL.

    restarts (invokable by number or by possibly-abbreviated name):
    0: [ABORT] Abort loading file “/usr/share/common-lisp/source/slime/swank-loader.lisp”.
    1: Exit debugger, returning to top level.

    (SB-C::READ-FOR-COMPILE-FILE
    #
    5230)

    • bowaggoner says:

      Hi, I’m not sure on this, but from looking around it looks like there should be a (hidden) file “.sbclrc” in your home directory (that is, in your “username” folder). If so, try opening it and adding the line

      (require 'asdf)

      Good luck! If you find the solution, I’d love to hear it!

  3. Anonymous says:

    Thanks, your instructions worked great!

  4. Anonymous says:

    Fantastic – spot on! I have recently decided to take up Lisp – and this tut was perfect to get me up and running… cheers!

  5. Anonymous says:

    Thanks so much for posting this set up guide.
    However, I’m getting caught up on Mac OS X Step 5 (the last part, of course).
    Where am I supposed to save/create the file? This might be a stupid question, but I’m not great with all this installation stuff.

    • bowaggoner says:

      Hi, by ~ I mean your “home” folder — on Mac, it’s the one with your name. If you are in the Terminal, for instance, and enter “cd ~”, that will “change directory” — go to the home directory. You can also enter “pwd” (print working directory) from there to see the full path of that directory (also, directory == folder). Hope that helps!

      • Anonymous says:

        Thanks!- for the speedy and accurate reply. I got it working, although it seems like it shouldn’t be this difficult to get LISP set up.

  6. ;; use SBCL and Slime with lisp
    (add-to-list ‘load-path “/usr/share/emacs/site-lisp/slime-2011-02-20”) ; tell emacs where slime is
    (setq inferior-lisp-program “/opt/local/var/macports/software/sbcl/1.0.45_0/opt/local/bin/sbcl”) ; tell emacs where the lisp interpreter is
    (require ‘slime)
    (slime-setup ‘(slime-fancy)) ; load almost all slime packages (minimalist: use (slime-setup))

    I changed the order of add-to-list and setq and it worked for me. (based on the README of slime)
    I run on the MAC Lion.

  7. Anonymous says:

    Did everything as described. Opened emacs, typed m-x slime, got ‘Process inferior-lisp not running’

    I swear, lisp has to be the hardest language to learn as it is almost impossible to install on mac, to begin with, no to mention the fact that I need to learn emacs now…

  8. bowaggoner says:

    Sorry about that. This post may be out of date by now (was originally written using Tiger).

  9. Darshan Oswal says:

    thank you…

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s