F# on (Ubuntu) Linux with Mono and Monodevelop

This week, I’m attending the Java Posse Roundup to learn about the interesting things happening on the JVM, meet smart people, and hopefully write some Scala and Clojure code. One of the more surprising emails that I got after registering for the conference came from Dick Wall who asked if I could take an afternoon during the conference to hack some F# with him. Dick prefers to run Ubuntu, and I have wanted to revisit F# on Ubuntu ever since I played around with it a few months back, so I set about installing F# and getting it to run with Monodevelop in Ubuntu.

Below is the process that I used to get everything working on my machine. I don’t claim that this is the best way to do things or that it will even work for anyone else. However, I didn’t find much up to date documentation on the web about running F# on Ubuntu, so hopefully this useful to others. I’ll do my best to keep this up to date, so feel free to post comments on your experiences.

Background

Unlike most installs on Ubuntu, mono is tricky. For various reasons, the mono packages in the Ubuntu repositories are significantly out of date, so you will have to build from source to get even semi-recent updates. I decided that I did not want to overwrite the mono installation that ships with Ubuntu, so these instructions will set you up with a parallel mono installation in /opt/mono.

Getting Started

Before you get started, you’ll need to make sure a few prerequisites are installed. You probably already have most of these if you’re a dev.

sudo apt-get install git build-essential autoconf libtool automake

Next, create (or make sure you have permission to access) /opt/src and /opt/mono since that’s where you’ll be putting all of the fresh mono bits.

cd ~
mkdir src
sudo mv ./src /opt
mkdir mono
sudo mv ./mono /opt

Installing mono

It’s easy to install a parallel mono environment once you know what you’re doing. First, you’ll need a couple of dependencies.

sudo apt-get install bison gettext

Next, download the mono source. I chose to install mono 2.10 (which was just released at the time of this writing), but you can install whatever version you’d like by modifying the git commands below.

Now build and install mono. Note: don’t forget the –prefix option since that’s how you avoid overwriting the default mono installation.

./autogen.sh --prefix=/opt/mono
make
make install

Setup a Parallel mono Environment

Now that your parallel instance of mono is installed in /opt/mono, you need a way to tell your bash environment to use it instead of Ubuntu’s default mono when you’re developing. You can do that with a short bash script as described on the parallel mono page.

You can create the script anywhere. I put mine in my home folder

vim ~/mono-dev-env

Add the following lines to your script to configure the parallel mono environment.

You can test that everything is working by running mono -V. Note that if you close your terminal window at any point during the rest of the install, you will have to source the mono-dev-env script again.

Install F#

There are a couple of ways to get F# running under mono, but I chose to also build F# from source. A mono-friendly version of the F# source lives on github. Since you will use F# Interactive, you’ll also want to grab libgdiplus, which F# Interactive requires to run properly.

sudo apt-get install libgdiplus

With that out of the way, download, build, and install F# from github. Again, don’t forget the –prefix option. Now is a good time to grab a drink since F# can take a while to build.

Right now you already have a full blown F# development environment at your fingertips, so you could stop at this point. I prefer to have monodvelop since I find that having mouse-over type information is very helpful when writing F#.

Install monodevelop’s Dependencies

Before you can install monodevelop, you’ll need to grab a few of dependencies, but those dependencies have a few dependencies of their own. Fortunately, you can get all of those from the Ubuntu repositories.

Install monodevelop

With the dependencies out of the way, you’re clear to install monodevelop. I got version 2.4 of monodevelop since the F# plugin does not work with the latest builds of monodevelop at the time of writing.

Install fsharpbindings

The fsharpbindings project is a plugin that makes adds support for F# to monodevelop. I had trouble getting the latest source working with a parallel mono 2.10 environment on Ubuntu, but I was able to fix the problems with my own branch of the plugin. I’ve put in a pull request, but for the time being, pull from my fork. I’ll update this post once the main fsharpbindings project works with Ubuntu and mono 2.10.

One Last Bit of Setup

You’re almost there. One last bit of setup is required since the current version of fsharpbindings uses different names for the F# compiler and F# Interactive executables. Create symlinks to get the bindings to work.

cd /opt/mono/bin
ln -s ./fsi ./fsharpi
ln -s ./fsc ./fsharpc

Try it Out!

If everything went according to plan (yeah, right), you should have a full blown F# IDE. Test it out!

Note: When testing the install, I received several weird errors the first time I created a F# project in monodevelop. Monodevelop segfaulted once and also complained about not being able to find “System.Array”. I’m not sure why these errors occur, but simply restarting monodevelop fixed them for me. I’ll update if and when I figure out more.

cd ~
monodevelop

You should be able to try out F# by running code in the F# interactive window similar to the one in Visual Studio.

You can also create a console application.

Summary

I hope you found the above install process useful, but feel free to leave comments or otherwise contact me if you find problems. F# on mono has come a long way since I last played around with it, and I’m looking forward to watching (and helping) things progress more in the future.

My (possibly misinformed) understanding is that there are a couple of objective reasons:

-Mono changes frequently enough that they are worried about breaking some of the apps that ship with Ubuntu that rely on mono
-Mono is tricky to package for Debian/Ubuntu

Probably more important is the subjective reason; I hear the Debian folks aren’t big fans of mono due to the Microsoft connection. I think it’s silly, but that’s my understanding of it. However, I’m not an expert on the politics of the open source world, so don’t take my word as law.

Very nice build process description. I’m working with a F# console app right now on Ubuntu 10.10 after finishing this process. Just enough detail to get things properly done.

The F# is gravy; I was happy to run across such a nice description of building a recent mono, seeing as how with the packages I was stuck with 2.6.7. For the past while I’ve been doing mono C# development on Mac OS X 10.6, where the available version is currently 2.8.2, hence also providing me with C# 4.0. But this is even better. The F# Interactive window is also working nicely. Many thanks.

Chris, thanks very much for fixing/posting your changes to the F# MonoDevelop plug-in for Mono 2.10. I was just debating going back a rev or debugging the issue myself but was hoping someone had already solve it. Thanks!

Hmm, I’m not positive, but it looks like something is wrong with your mono runtime. Did you make sure to source your parallel mono environment? Make sure that the output of mono -V looks like what I described in the post. If that looks the same and you followed all the steps that I outlined for building mono from source, I’m not sure.

Hi, Chris. I followed the steps on 11.04, and I believe everything installed correctly. However, when I launch monodevelop, I get this:

FATAL ERROR [2011-06-23 09:23:08Z]: MonoDevelop failed to start. Some of the assemblies required to run MonoDevelop (for example gtk-sharp, gnome-sharp or gtkhtml-sharp) may not be properly installed in the GAC.
System.MissingMethodException: Method not found: ‘System.Type.op_Equality’.
at GLib.Object.GetObject (IntPtr o, Boolean owned_ref) [0x00000] in :0
at GLib.Object.GetObject (IntPtr o) [0x00000] in :0
at Gtk.Settings.get_Default () [0x00000] in :0
at MonoDevelop.Ide.IdeStartup.Run (System.String[] args) [0x00000] in :0
at MonoDevelop.Startup.MonoDevelopMain.Main (System.String[] args) [0x00000] in :0

I haven’t tried on 11.04, but based on the error, it sounds like you’re missing gtk-sharp, gnome sharp, or gtkhtml-sharp. Either you aren’t running from the parallel mono environment and need to run “source ~/mono-dev-env” or one of the dependencies failed to install correctly. Also, I’m not sure if gtkhtml-sharp is a new dependency, or if it was installed as part of the other steps. Are you sure that you’re trying to install Mono 2.10 and Mono Develop 2.4?

Hey, thanks heaps for these instructions. I am nearly there! I got all the way to actually installing monodevelop but ran into a problem when executing the branch command. The git clone command worked successfully, I changed into the monodevelop directory, then executed ‘git branch 2.4 remotes/origin/2.4’ and received the following error message:
fatal: Not a valid object name: ‘remotes/origin/2.4’.

I notice on the monodevelop site that they now have a 3.0 version. Perhaps 2.4 has been moved to somewhere else in the repository? I am a complete beginner when it comes to git and don’t know how to list what is actually available in remotes/origin to see if there is maybe a monodevelop-2.4 or something like that. I realize these instructions are kinda old now, but they still work for the newest non-beta version of mono itself, and it was the first search result on DuckDuckGo for ‘mono f#’ so if it’s not too much trouble could you update them?

Yeah, the instructions are definitely a bit outdated. I believe F# comes out of the box in mono/monodevelop these days, and I’d recommend checking out http://fsharp.org/ for more info on how to get everything running with the latest bits.

As far as your question about the branch, going out to github, it looks like 2.4 is still in the repository, but it’s a tag now, not a branch. You should be able to do a “git checkout monodevelop-2.4 to get to that version of the code.

Sorry for the long turnaround on my response. Please feel free to send another reply or send me an email at [chris] @ [this web site]

2 Trackbacks

[…] hints that may reduce some Googling as you go down the path of creating your first package.” Chris Marinos’s F# on (Ubuntu) Linux with Mono and Monodevelop “Below is the process that I used to get everything working on my machine. I don’t claim that […]