Installing OCaml Batteries
In this post I want to help OCaml newcomers to install Batteries. The task is trivial under Linux, while it's a bit tricky under Windows, because OCaml still lacks a self-contained Windows installer.
My assumption is that the reader is a coder, so I will not explain everything… Let's start with the easy part: Linux.
Linux
The installation of OCaml + Batteries under a Debian/Ubuntu system couldn't be easier, thanks the the hard work of the Debian OCaml Task Force. So open a terminal and type:
$ sudo aptitude install ocaml-batteries-included
That's all for Debian/Ubuntu. I don't know how Fedora works, but I think it's easy to install Batteries using YUM, something like:
$ yum install ocaml-batteries-included
If a RPM package for Batteries wasn't available, you could still install OCaml, Camomile (the Unicode library), and compile Batteries from sources, as described below for the Windows OS.
Windows
As said, this OS still lacks a self contained installer which is in progress, at least for installing OCaml. Since many OCaml versions are available for Windows, with different pros and cons, I had to decide which one to use, and I decided to follow the simplest path to reach the goal of installing all the stuff we need. The Cygwin port is by far the simplest way.
- Download Cygwin setup and double click the executable. In Windows Vista/7 (I made my test on a Windows 7 64bit box) you will be required to allow the program to be run a couple of times, as usual ;-) …
- when the list of available packages appears, select: each and every package containing "caml" (see the screenshot below), and also make, m4, libncurses-devel, git, wget and rlwrap;
- open the Cygwin shell;
- download the Findlib library, version 1.2.6:
$ wget https://download.camlcity.org/download/findlib-1.2.6.tar.gz
- unpack, compile and install Findlib:
$ tar -xpzf findlib-1.2.6.tar.gz $ cd findlib-1.2.6/ $ ./configure $ make $ make install
- download, unpack, compile and install Camomile 0.8.1:
$ wget https://prdownloads.sourceforge.net/camomile/camomile-0.8.1.tar.bz2 $ tar -xpjf camomile-0.8.1.tar.bz2 $ cd camomile-0.8.1/ $ ./configure $ make $ make install
- the last step is to download compile and install Batteries itself. I wasn't able to compile the latest stable release (1.2.2), for an obscure preprocessor error, but using the latest GIT branch everything went smoothly. So here are the steps:
$ git clone git://github.com/ocaml-batteries-team/batteries-included.git $ cd batteries-included/ $ make camomile82 $ make all doc $ make install install-doc
Testing the installation
Before starting to play with the library and the toplevel (the OCaml REPL is called toplevel) let's put into action a couple of helpers.
- The OCaml toplevel doesn't support readline. To get this feature back we add an alias to .bashrc. This works in both Linux and Windows:
alias ocaml='rlwrap -H /home/paolo/.ocaml_history -D 2 -i -s 10000 ocaml'
restart the terminal or load another bash; - we need to load Batteries in the toplevel. This is not strictly necessary, but it helps a lot and the Batteries ASCII logo is wonderful :-). All we need is to create a file named .ocamlinit in the home directory. Open your favorite editor and put this phrases in ~/.ocamlinit:
let interactive = !Sys.interactive;; Sys.interactive := false;; (*Pretend to be in non-interactive mode*) #use "topfind";; Sys.interactive := interactive;; (*Return to regular interactive mode*) Toploop.use_silently Format.err_formatter (Filename.concat (Findlib.package_directory "batteries") "battop.ml");;
If everything went well you can now type ocaml and something like this should appear:
$ ocaml Objective Caml version 3.11.2 _________________________ [| + | | Batteries - | |_____|_|_________________| _________________________ | - Type '#help;;' | | + |] |___________________|_|___| Loading syntax extensions... Camlp4 Parsing version 3.11.2
Conclusions
This (rather boring) post has been devoted to the installation details of Batteries under Windows, where it presents some difficulties for newbies. Next time we will start on exploring the library with simple examples to exploit its strength.
19 December 2010, 15:03
Paolo, good tutorial. For the record, this won't work for a MINGW-based port of OCaml, only for the Cygwin-based port. In particular, all paths must be specified in DOS format since under MINGW OCaml doesn't use cygpath. For Camomile, use:
./configure --datadir="c:/cygwin/usr/local/share"
(adjust to your environment; make sure it is a full path). It pays to check in the top level that Findlib correctly loads everything:
#require "camomile" ;;
should not fail with "Not_found"! I also had to correct Findlib's META file for Camlp4. Strangely enough, it doesn't specify Camlp4's library directory. Line 5 in sitelib/camlp4/META must read:
directory = "^camlp4"
Why does it work from the toplevel but not when invoking ocamlfind, I don't know. Also, building docs fails because of an incorrectly read symlink to somewhere that I couldn't determine exactly it should be.
In any case, I've got batteries, thanks to these instructions. Good work!