Compiling Mono & F# on the Raspberry Pi

This guide assumes you already have Raspbian installed on your Raspberry Pi, that the system is fairly up-to-date and Mono is not already installed.

If you already have Mono installed, do not follow this guide! We are going to compile Mono from source and install it into the /usr prefix, which will severely disrupt your system if another version of Mono was already installed before. If you want to have two different versions of Mono installed at the same time, you might want to read up on Parallel Mono Environments.

Enabling extra Swap-Space

Compiling F# takes a lot of RAM and Swap-Space. More than the default configuration offers. Plug a flash drive with at least 1GiB into the Raspberry Pi. Ensure that the drive already has one partition on it that we can use as a swap partition and you don’t have anything you want to keep on it.

Next, we have to figure out what path the flash drive has been given. Usually it’s /dev/sda for the device and /dev/sda1 for the first partition. To make sure, we can run the following command that will print the vendor and model name of the drive with the specified path.

udevadm info --name=/dev/sda1 --query=property | grep --color=never --perl-regexp "ID_(VENDOR|MODEL)="

Once you have ensured that /dev/sda1 is the right partition (or you have figured out the correct path, in which case you need to adjust the commands accordingly), we can enable the extra Swap-Space. This will only last until the next reboot.

sudo mkswap /dev/sda1
sudo swapon /dev/sda1

Compiling Mono

Fetch the current Mono source code. It will be placed inside a build directory in the current user’s home directory.

git clone ~/build/mono
cd ~/build/mono

Before running to generate the Makefile, we have to install a couple of dependencies: autoconf, automake and libtool.

sudo apt-get install auto{conf,make} libtool

Next, we run and set the installation prefix to /usr, which indicates the place where the binaries will be installed to. National Language Support is disabled with the --enable-nls=no argument, since we do not need it and it would add another dependency (gettext).

./ --prefix=/usr --enable-nls=no

Since parts of Mono are written in C#, we need a C# compiler to compile Mono, which we currently do not have. To bootstrap the compilation, we can do as the Mono Readme says and fetch the current monolite distribution, which enables us to compile C# code.

make get-monolite-latest

Now everything is ready to be built and installed, which takes quite some time.

make EXTERNAL_MCS=${PWD}/mcs/class/lib/monolite/gmcs.exe
sudo make install

Compiling F#  

Fetch the current F# source code. Like Mono, it will be placed inside the build directory in the current user’s home directory.

git clone ~/build/fsharp
cd ~/build/fsharp

In addition to the Mono dependencies, F# adds one more – pkg-config – which has to be installed before running

sudo apt-get install pkg-config

Then, as before, we run and set the installation prefix also to /usr, so that the Mono and F# binaries end up in the same place.

./ --prefix=/usr

To compile and install F#, run the following two commands. Make sure you have enough Swap-Space as described above.

sudo make install

Testing the Installation

To test the new F# installation, the following commands will create an executable F# script inside the current user’s home directory and call it.

cat << EOF > ~/helloworld.fsx
#light (*
    exec fsharpi --exec \$0 \$*
printfn "Hello, World!"
chmod +x ~/helloworld.fsx

Starting up fsharpi can take nearly a minute, so be patient.
And have fun with F# on the Raspberry Pi.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s