Danny Willems -- Work In Progress

Fighting to follow my values. Integrity is key. Reputation is key. Fighting for privacy and security on the Internet. Being respectful is your first duty.

Research Publications Public Talks Open source software contributions CV Blog PGP public key Recommended softwares Contact Proton calendar for cryptography and cybersecurity events
22 June 2016

OCaml on Raspberry Pi

by Danny Willems

This morning, I wanted to play with my Raspberry Pi which is, since a long time, not used. After downloading the last raspbian image and installing it with dd (see here), I wanted to make some scripts, to do some programming. I read some articles like this one from Jane Street installing OCaml on a Raspberry Pi and doing some coding stuff with Async. So, I had my morning project: install OCaml and OPAM on my Raspberry Pi 1 model B+ along with OPAM packages.

Installing OCaml

The first step is to install some dependencies like git and m4 to be able to install OPAM. Normally, git is pre-installed but not m4.

sudo apt-get install m4

After that, OCaml 4.01.0 has a package and can be installed with apt-get:

sudo apt-get install ocaml

The installation takes a few minutes but after that, we can use OCaml without problem.

OCaml on Raspberry Pi

Clone, compile and install OPAM

It’s time to be able to use OPAM to install our favorite packages. We will install OPAM from source to have an optimized installation. We need to configure, install external libraries, compile OPAM before being finally able to install it. All commands are given on the official GitHub repository:

git clone https://github.com/ocaml/OPAM
make lib-ext
sudo make install

Cloning and configuring are fast. The longest steps are installing external libraries and compiling OPAM: it takes several minutes. If you are using a Raspberry pi 3, which has a quad core CPU, you can use [code lang=”bash”]make -j 4[/code]. These commands will install the last version of OPAM (which is for the moment 2.0) but maybe you want OPAM 1.2. In this case, use:

git clone https://github.com/ocaml/opam
git checkout -b 1.2 origin/1.2
make lib-ext
sudo make install

We also need to launch OPAM init, which takes also several minutes:

opam init

Install the last OCaml version

Before using any package, I would like to switch to a more recent OCaml version, such as the last release: 4.03.0. We can easily do this with OPAM switch.

opam switch 4.03.0

It takes a very long time because the Raspberry Pi needs to compile OCaml. CPU is always at 100% but it doesn’t take too much RAM, the entire system taking 200 MB max. You will be stuck at this step for a long time.

OPAM switch 4.03.0 stuck

And after around 1 hour, we have an fresh OCaml 4.03.0 installation on our Raspberry Pi:

ocaml 4.03.0

Install an OPAM package: js_of_ocaml

As a fan of js_of_ocaml, I would like to try it on my Raspberry Pi.

OPAM install js_of_ocaml

Still no issues with the installation but it takes also a long time due to camlp4 in particular but everything is OK, without any memory issue.

tags: OCaml - FP - Raspberry Pi - RSS