Friday, February 6, 2015

Solving issues with installing the "Real World OCaml" prerequisite libraries under Mac OS X

    I’ve run into some troubles installing the «Real World Ocaml» prerequisites under Mac OS X Yosemite. The installation process is described on the books’ wiki here. I've found little help by googling the error messages, which possibly means they could be very specific to my installation. So I've decided to share my experience in case someone runs into similar issues.
    At one point, the instruction advises you to install some libraries that are used throughout the book, by issuing the following command:
opam install \
   async yojson core_extended core_bench \
   cohttp async_graphics cryptokit menhir

    At first I’ve failed to install the cohttp and async_graphics packages. Possibly that was because I already had objective-caml installed, and my process of installation deviated a bit from the one prescribed by the instruction.
    The cohttp package depends on ctypes package, which was failing with the following error:
# fatal error: 'ffi.h' file not found
# #include <ffi.h>

    To solve it, just install libffi and add it to LDFLAGS environment variable for the time of build:
brew install libffi
export LDFLAGS=-L/usr/local/opt/libffi/lib
opam install cohttp

    If during the installation of async_graphics module you receive an error:
# Error: Unbound module Graphics

    That probably means you’ve installed objective-caml without the Graphics module, which may be verified by the listing:
ls /usr/local/opt/objective-caml/lib/ocaml/graphics*

    If the listing is empty, then you should reinstall objective-caml with graphics:
brew uninstall objective-caml
brew install objective-caml --with-x11

Now the process of installing the async_graphics package should work just fine:
opam install async_graphics

Installing Ocsigen web framework under Mac OS X and CentOS and creating a simple web application

        I recently wanted to play around with OCaml and create a web application. It appears that the Ocsigen framework is the only (or the most popular) choice for building web applications with OCaml, so here’s how to install it on Mac OS X and CentOS 6 and create a simple web application.

Installing on Mac OS X

        This process was tested on Yosemite. First, install brew, if you haven't got it already:
ruby -e "$(curl -fsSL"

        Now install ocaml, opam package manager and all the prerequisites for the Eliom (web application framework) and Macaque (database framework). Macaque is not really needed to run a simple example, but you're going to need it soon enough if you're about to develop a database-backed web application.
brew install ocaml opam libev gdbm pcre openssl pkg-config sqlite3

        Create a symbolic link for pkgconfig to the sqlite package. Use your current installed version of sqlite instead. Not a very clean solution, as it will break if the sqlite package is updated via brew. If you know how to reference the latest sqlite version, please let me know.
ln -s /usr/local/Cellar/sqlite/ /usr/local/lib/pkgconfig/sqlite3.pc

        Now initialize the opam package manager. It will create the ~/.opam directory, where it keeps all of its data, including installed packages.
opam init

        Now edit the ~/.profile file and add this line:
eval `opam config env`

        Restart the terminal shell to pick up the environment variables, and then check that the scaffolding tool is available:

Installing on CentOS 6

        First add the OCaml repository to yum:
cd /etc/yum.repos.d/

        Now install OCaml, opam and all the prerequisites:
yum install ocaml opam ocaml-camlp4 ocaml-camlp4-devel ocaml-ocamldoc openssl-devel pcre-devel sqlite-devel

        Initialize the opam repository and install the eliom web framework and macaque database framework:
opam init
opam install eliom macaque

        If for some reason you encounter an error during installation:
# ocamlfind: Package `camlp4' not found

        Then try to reinstall the ocamlfind package and run the installation again:
opam reinstall ocamlfind
opam install eliom macaque

Creating and running your first Ocsigen web application

        Create a barebones application using the generator:
eliom-distillery -name mysite -template basic -target-directory mysite

        Run it:
cd mysite
make test.byte

        Open http://localhost:8080/ in your browser. You should see the «Welcome from Eliom’s distillery!» greeting message.