Saturday, June 25, 2011

Starting from scratch

I've been wanting to write about Clojure for some time now, and I've decided to do so from the perspective of a PHP programmer (that's me) making the transition to functional programming, using Clojure. I like to start at the beginning, so in this post, I'm going to set up a Clojure programming environment from scratch.

[NOTE: Early feedback on this post suggests that I may be making things look harder than they really are. Clojure works just fine in any environment that runs Java, and can be developed using any editor or IDE. My goal in starting from scratch is purely didactic. I'm starting with a simple, bare-bones environment so that anyone who wants to follow along can easily do so without being distracted by issues like "I played with emacs a while ago and my .emacs file has some cruft in it" or "My classpath seems to be pointing at some old jar files I installed a couple years ago."

If you're not interested in setting up a clean, minimal environment, feel free to skip ahead to the "Installing leiningen" section.]

When I say "from scratch," I really mean from scratch.  Here's the first few steps:

  1. Download and install VirtualBox (http://www.virtualbox.org/wiki/Downloads).
  2. Download Ubuntu 10.04 (http://www.ubuntu.com/download/ubuntu/download)
  3. Start up VirtualBox and create a new Linux virtual machine, with 512M of memory and an 8GB hard drive. Use the Ubuntu 10.04 ISO image as the attached CD.
  4. Fire up your virtual linux machine, and install Ubuntu 10.04 on it. You'll probably also want to install the Guest Additions as well, but that's optional.
So far so good. It's a small configuration, but it's consistent with what some of the online virtual host/cloud services offer for cheap/free, so I figure that's a good place to start. We can always expand later if we want.

We're going to need Java and some other useful software, so let's install them now. Most of them won't be used directly, but some of our tools will use them behind the scenes, so we'll get them installed now and save ourselves some hassle later.
  1. Pull down the Applications menu and select Accessories -> Terminal.
  2. Switch to superuser mode by typing sudo su at the prompt and entering your password.
  3. Install java by typing apt-get install openjdk-6-jdk.
  4. Install Emacs 23: apt-get install emacs23. (If you're new to Emacs, don't worry, we'll stick to the easy stuff.)
  5. Optionally (but very nice), we'll install rlwrap so our command-line experience will have all the handy key commands we're used to. Type apt-get install rlwrap.
  6. Type CTRL-D to exit superuser mode.
And we're done. We can close the Software Center window now. From a completely fresh system, we install  4 packages and our OS is prepped and ready to receive Clojure.

Installing leiningen


There's a growing library of tools we can use to work with Clojure, but one of the most mature and powerful packages is Leiningen (or just "lein" for short). We will be using the leiningen package to do all the heavy lifting for us, so let's install that next. Type the following in a terminal window. The part you type is in bold.

$ cd /usr/local/bin
$ sudo wget https://github.com/technomancy/leiningen/raw/stable/bin/lein
[sudo] password for mark:
--2011-06-25 11:53:16--  https://github.com/technomancy/leiningen/raw/stable/bin/lein
Resolving github.com... 207.97.227.239
Connecting to github.com|207.97.227.239|:443... connected.
HTTP request sent, awaiting response... 302 Found
Location: https://raw.github.com/technomancy/leiningen/stable/bin/lein [following]
--2011-06-25 11:53:16--  https://raw.github.com/technomancy/leiningen/stable/bin/lein
Resolving raw.github.com... 207.97.227.243
Connecting to raw.github.com|207.97.227.243|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 6580 (6.4K) [text/plain]
Saving to: `lein'


100%[======================================>] 6,580       --.-K/s   in 0s    


2011-06-25 11:53:16 (108 MB/s) - `lein' saved [6580/6580]


$ sudo chmod +x lein
$ cd
$ lein self-install
Downloading Leiningen now...
--2011-06-25 11:58:42--  https://github.com/downloads/technomancy/leiningen/leiningen-1.5.2-standalone.jar
Resolving github.com... 207.97.227.239
Connecting to github.com|207.97.227.239|:443... connected.
HTTP request sent, awaiting response... 302 Found
Location: http://cloud.github.com/downloads/technomancy/leiningen/leiningen-1.5.2-standalone.jar [following]
--2011-06-25 11:58:43--  http://cloud.github.com/downloads/technomancy/leiningen/leiningen-1.5.2-standalone.jar
Resolving cloud.github.com... 216.137.33.203, 216.137.33.220, 216.137.33.230, ...
Connecting to cloud.github.com|216.137.33.203|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 8195550 (7.8M) [application/java-archive]
Saving to: `/home/mark/.lein/self-installs/leiningen-1.5.2-standalone.jar'


100%[==========================================================>] 8,195,550   1.17M/s   in 6.7s    


2011-06-25 11:58:50 (1.16 MB/s) - `/home/mark/.lein/self-installs/leiningen-1.5.2-standalone.jar' saved [8195550/8195550]
$
Wow, not only did we just install leiningen, but we installed Clojure too! That was easy! Let's take it for a test drive.

$ lein repl


REPL started; server listening on localhost:37794.
user=> (defn hello-world [] (println "Hello, world!"))
#'user/hello-world
user=> (hello-world)
Hello, world!
nil
user=> [CTRL-D]
If you decided not to install rlwrap, the CTRL-D key may not get you out of the REPL, but you can type the longer command (System/exit 0) to get out the hard way.

In the next post, we'll turn this basic setup into a full-featured development environment.

2 comments:

  1. You don't need ant or maven to use Leiningen actually; it's self-contained. But I recommend Vagrant over raw VirtualBox. This script automates everything: https://github.com/Seajure/emacs-clojure-vagrant

    ReplyDelete
  2. Cool, I hadn't heard of Vagrant before, I'll check it out. I'll also update my post wrt and and maven. Thanks much.

    ReplyDelete