OCaml on Raspberry Pi

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

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
cd opam
./configure
make lib-ext
make
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

make -j 4

.
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
cd opam
git checkout -b 1.2 origin/1.2
./configure
make lib-ext
make
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

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.

Leave a Reply

Your email address will not be published. Required fields are marked *