Developing for .NET on the Mac, Part 1: Building Mono for the First Time

Ever since I first heard about Mono, I've been fascinated by the potential of .NET to become a viable platform for software that targets multiple OSes. Unfortunately, I've discovered (as seems to be the case with many OSS projects), the docmentation aimed towards beginners is quite limited, especially when one ventures away from running Mono on Linux.

I've spent many hours trying to get my development environment for the Mac just right, and at times, it's been a rather painful process. So, in the interest of reducing the pain felt by any beginners following after me, I've decided to start recording my processes for getting things set up. At best, this blog will become a useful resource for anyone doing .NET development on the Mac. At worst...well, at least I'll be able to set things up again the next time I have to wipe a hard drive or replace a machine.

One of the first things that I've found in my work is that the Mono installer available from the Mono Project is rarely sufficient for doing any serious development work: bugs abound in the new parts of Mono (I've found a number of problems in generics), and sometimes, I simply can't resist the urge to dig in and fix them. Therefore, when setting up a new development environment, my first goal is generally to get a custom build of Mono up and running.

Here's how I do it:

Prerequisites:
  • Install Mac OS X--this one's kind of a no-brainer, but just for the record, I'm currently running version 10.4.10. This is the case both for my first-generation Mac Mini and my Mac Pro.
  • Install XCode--again, this shouldn't be too surprising, but I'm aiming for completeness, here :).
  • Install Fink--I've found that this is the best way to get a lot of the common Unix tools you'll need along the way. Personally, I prefer using FinkCommander, which puts a nice GUI on top of Fink. Be sure to follow all of the installation instructions on their website, or things can go mysteriously and confusingly wrong.
  • Install pkg-config, glib2-dev, and svn-client; you can get these through Fink.
  • Install and install an official distribution of Mono--sometimes it comes in handy.
The actual work:
  1. Create a folder for your mono source. For the purpose of these instructions, I'll assume you're going to put it in ~/src/mono.
  2. Open a Terminal window and go to ~/src/mono. Run the command svn checkout svn://anonsvn.mono-project.com/source/trunk/mono svn://anonsvn.mono-project.com/source/trunk/mcs. This will download the code for the core Mono libraries and the Mono Compiler System. They'll end up in ~/src/mono/mono and ~/src/mono/mcs.
  3. Create a folder into which you can install your custom build. I put mine in /opt/mono.
  4. Run the command cd ~/src/mono/mono && ./autogen.sh --prefix=/opt/mono. This step should get Mono ready to build.
  5. You're now ready to build and install: make && make install. I'd love to be able to recommend that you also insert make check in that list, but the sad fact is that Mono on MacOS actually fails a number of tests.
After you've done all this, you should have a working (custom) build of Mono working on your machine. You can, at any time, upgrade to the latest code by going to ~/src/mono and running the command svn update mono mcs. Generally, you'll be able to jump straight to building Mono again, but you may sometimes be required to also run autogen.sh.

Also, it's often very handy to have a script around that will set up all of your paths and whatnot to your custom build of mono. The Parallel Mono Environments page on the Mono website describes this, although I had to modify mine a bit to get it working right. Here's what it looks like now:

#!/bin/bash
MONO_PREFIX=/opt/mono
GNOME_PREFIX=/opt/gnome
export LD_LIBRARY_PATH=$MONO_PREFIX/lib:/sw/lib:$LD_LIBRARY_PATH
export C_INCLUDE_PATH=$MONO_PREFIX/include:$GNOME_PREFIX/include:/sw/include:/Library/Frameworks/Mono
.framework/Versions/1.2.2.1/include/libpng
export ACLOCAL_PATH=$MONO_PREFIX/share/aclocal
export PKG_CONFIG_PATH=$MONO_PREFIX/lib/pkgconfig:$GNOME_PREFIX/lib/pkgconfig:/sw/lib/pkgconfig:/usr/
lib/pkgconfig:/usr/local/lib/pkgconfig:/usr/X11R6/lib/pkgconfig:/Library/Frameworks/Mono.framework/Ve
rsions/1.2.2.1/lib/pkgconfig
PATH=$MONO_PREFIX/bin:$PATH:/sw/bin
PS1="[mono] \w @ "

Mine is stored in ~/mono-dev-env and is invoked via source ~/mono-dev-env

That's it for my first post on setting up a development environment for Mac/.NET development. In the future, I'll go into setting up mod_mono (for ASP.NET), CruiseControl.NET (you do have unit test you'd like to run regularly, don't you?), and Subversion (I realize that Subversion isn't strictly related to .NET, but I didn't have a Subversion server when I started this project, and trying to figure out how to get it set up wasn't terribly fun).

Print | posted @ Friday, June 29, 2007 6:39 PM

Comments on this entry:

No comments posted yet.

Your comment:

Title:
Name:
Email:
Website:
 
Italic Underline Blockquote Hyperlink
 
 
Please add 7 and 5 and type the answer here: