20 Jan 2009, 12:35 p.m.

Getting Started with Mono on Linux

I've recently had cause to look into various strategies for integrating PHP and .NET (on which subject, pointers will be most welcome), which led to a brief investigation of Mono. For the uninitiated, Mono is a "a cross platform, open source .NET development framework". In short, it allows you to compile and run .NET code on a Linux server.

Mono is fairly straightforward to install, although the process isn't terribly well documented, so I figured I'd run through the steps here in case it might help somebody. As a bonus, we'll have a look at something called XSP once we're done.

Getting Mono

Visiting the Mono downloads page you'll soon discover that there are a limited number of platforms for which officially supported Mono distributions are available. In fact, you're pretty much limited to Ubuntu and Windows.

For everybody else (and for the record, I'm using Fedora 8 here), there's the other downloads page, more specifically the section entitled "Other Linuxes". While the binary installer provided does suffer from version lag (current 1.9 as opposed to the latest of 2.2 or thereabouts), it seems to work a treat on Fedora, so that's what we'll be using here. Download that to your Linux machine, and you're ready to get started.

Installing Mono

The binary installer makes installing Mono nice and simple. You just need to make the installer file executable, and run it:

[root@vps02 src]# chmod +x mono-1.9.1_2-installer.bin
[root@vps02 src]# ./mono-1.9.1_2-installer.bin --mode text

Note that I've specified --mode text, as this is a remote server with no graphical desktop installed.

Installation is as simple as agreeing to the terms and conditions, and specifying a directory into which Mono will be installed. I stuck with the default /opt/mono-1.9.1, but will refer to the directory as /path/to/mono for the purposes of this post. Finally, you can choose to have the installer add /path/to/mono/bin to your shell path, which is jolly convenient, as we'll soon see.

After a brief warning about not being able to run graphical applications, which is non-fatal and to be expected, we're done. We now have Mono.

Running Mono

It's time to test your installation of Mono by compiling and running some C# code. On Linux! If you're anything like me, this will give you a mildly subversive thrill.

Here's a run-of-the-mill "Hello World" program in C#.NET:

using System; public class HelloWorld { static public void Main() { Console.WriteLine("Hello World"); } }

Save that as, for example, HelloWorld.cs, and compile it by running /path/to/mono/bin/mcs. Assuming that your shell path has been updated, that's as simple as:

[root@vps02 ~]# mcs HelloWorld.cs

That will create a .NET 1.1 assembly, whereas running gmcs will create a .NET 2.0 assembly. Either way, the compiler will drop an executable file named HelloWorld.exe into the current directory. You can run that using:

[root@vps02 ~]# mono HelloWorld.exe
Hello World

As if by magic. I take my hat off to the Mono folks, because I couldn't believe quite how easy it was to get to this point. In fact, the hardest part of it was navigating the Mono website.

Running XSP

As a bonus, you've also just installed XSP. XSP is a standalone web server/ASP.NET engine - it's kind of like Tomcat, but for .NET. It's useful for getting ASP.NET code up and running quickly (and is also required for running .NET through Apache and mod_mono), so it's worth a brief look.

To see XSP in action, cd into the Mono installation's XSP test directory, and run XSP:

[root@vps02 ~]# cd /path/to/mono/lib/xsp/test/
[root@vps02 test]# xsp
xsp
Listening on address: 0.0.0.0
Root directory: /path/to/mono/lib/xsp/test
Listening on port: 8080 (non-secure)
Hit Return to stop the server.

Bear in mind that by default, XSP will serve files from the current working directory, so do make sure you're in the test directory first, otherwise you may end up serving files that you don't really want to expose to the world. You can specify the root directory by using the --root option. (Similarly, you can override the default port using the aptly-named --port option).

You can now browse to http://yourservernamehere:8080/, to see a whole bunch of example ASP.NET code in action. There's quite a lot in there, covering all sorts of things - HTML Controls, Web Controls, form handlers etc - although I was disappointed to note that "Hangman" was a broken link.

When you're done, remember to hit "return" to halt XSP and stop serving pages.

Conclusions

Well, wasn't that fun? We've turned our Fedora Linux box into a .NET server, without a great deal of pain. I'm certainly impressed how easy the Mono people (and whoever created the binary installer) have made it. The Mono site could use a bit of attention though, as more than once I ended up chasing my tail following misleading links.

I'll plan to return to Mono in due course, and hopefully a second post will look at getting Mono and .NET integrated with Apache HTTPD using mod_mono, thereby creating a somewhat more "grownup" .NET environment.

In the meantime, if you have experience with Mono on other platforms, or have taken things further than I have - for example by installing mod_mono or Phalanger, or by running Mono in a real production environment - then do comment and let us all know how you got on.

Posted by Simon at 01:53:00 PM