Sunday, March 15, 2009

Erlang and CouchDB on MacOS

As I mentioned before I am looking into how I could use Erlang in our own efforts. Sure, it is not the silver bullet for all problems ("Can it make coffee?") and all the hype on the developer ether. But it is certainly built to be the basis of large concurrent systems, for example Facebook's chat system. Or the below mentioned Amazon Dynamo clone called Dynomite.

For starters I purchased "The Pragmatic Programmers" screen case Erlang in Practise with Kevin Smith. I have to say, it is worth every cent! I did watch it on a flight from Munich to Las Vegas and it made the hours literally "fly by". There is something really cool about seeing a program being developed in front of your eyes and re-written many times to explain more advanced concepts as you go along. Highly recommended.

Now I have a background in Prolog and Lisp so getting into the Erlang way of doing things was not too difficult. Of course, the difficult yet again is how to use it for something own. I decided to first build Erlang on my MacBook Pro and then try an Erlang based system to see it working. Building Erlang on MacOS is described in here but overall it is a simple "wget && tar -zxvf && ./configure && make" obstacle course with a few hickups thrown in for good measure. As the post describes, you first need to install XCode from either the supplied MacOS disks or by downloading it from the net - easy peasy. Next is to build libgd. The above post has a link to the details required to build libgd on MacOS. It first required downloading all the required library tar balls and then running the following commands:


$ cd /downloads/
$ tar -zxvf zlib-1.2.3.tar.gz
$ tar -zxvf gd-2.0.35.tar.gz
$ tar -zxvf freetype-2.3.8.tar.gz
$ tar -zxvf jpegsrc.v6b.tar.gz
$ tar -zxvf libpng-1.2.34.tar.gz

This assumes all tar balls are saved in the "/downloads" directory. Next is zlib:

$ cd zlib-1.2.3 ; ./configure --shared && make && sudo make install
$ ./example

Then libpng:

$ cd ../libpng-1.2.34
$ cp scripts/makefile.darwin Makefile
$ vim Makefile
$ make && sudo make install
$ export srcdir=.; ./test-pngtest.sh

Next is the jpeg library. Here I did not have to symlink the libtool as described in the post. I assume that is because I am on MacOS 10.5. So all I did is this:

$ cd ../jpeg-6b/
$ cp /usr/share/libtool/config.sub .
$ cp /usr/share/libtool/config.guess .
$ ./configure --enable-shared
$ make
$ sudo make install
$ sudo ranlib /usr/local/lib/libjpeg.a

We are getting closer. The freetype library needs these steps, where the last line is for the subsequent libgd build:

$ cd ../freetype-2.3.8
$ ./configure && make && sudo make install
$ sudo ln -s /usr/X11R6/include/fontconfig /usr/local/include

OK, now the libgd library:

$ cd ../gd-2.0.35
$ ln -s `which glibtool` ./libtool
$ ./configure
$ make && sudo make install
$ ./gdtest test/gdtest.png

With this all done we can build Erlang from sources like so:

$ cd /downloads
$ tar -zxvf otp_src_R12B-5.tar.gz
$ cd otp_src_R12B-5
$ ./configure --enable-hipe --enable-smp-support --enable-threads
$ make && sudo make install
$ erl

All cool, the Erlang shell starts up and is ready for action:

$ erl
Erlang (BEAM) emulator version 5.6.5 [source] [smp:2] [async-threads:0] [hipe] [kernel-poll:false]

Eshell V5.6.5 (abort with ^G)
1> "hello world".
"hello world"
2>

With this in place I decided to try CouchDB. Again, here are the steps to get it running:


$ cd /downloads
$ tar -zxvf apache-couchdb-0.8.1-incubating.tar.gz
$ cd apache-couchdb-0.8.1-incubating
$ less README
$ sudo port install automake autoconf libtool help2man
$ sudo port install icu spidermonkey
$ ./configure
$ make
$ sudo make install
$ sudo -i -u couchdb couchdb

I did not have to execute this line $ open /Applications/Installers/Xcode\ Tools/XcodeTools.mpkg. I assume it is because I had the full XCode install done beforehand. You can also see that you need to install the MacPorts tools. With those you will have to add a few binary packages to be able to build CouchDB. Especially the Unicode library ICU and the spidermonkey C based JavaScript library provided by the Mozilla organization. The last line starts up the database and directing your browser to http://localhost:5984/_utils/ allows you to see its internal UI. Now relax! ;)

If you look closely at the screen shot you will see an inconsistency to the notes above. I will describe this in more detail in a future post about getting the sample Twitter client for CouchDB running using CouchApp.

No comments:

Post a Comment