Friday, February 6, 2015

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.

No comments:

Post a Comment