Gallery: Installing and using mono on Ubuntu

A lot of people aren't aware that you can combine Microsoft's .NET Framework with the Linux operating system by using the Open Source mono project. This article will get you started on installing mono and running basic .NET applications on Linux.

Two of the most popular topics on Builder AU, and indeed in the wider industry, is Microsoft's .NET framework and Linux development and administration.

Most of the time the two are in conflict, and it's rare to find a developer who needs to know about both. A lot of people aren't aware that you can combine the two, however, by using the Open Source mono project. This article will get you started on installing mono and running basic .NET applications on Linux

Firstly you'll need to install the base mono package using apt-get. It is a good idea to install two other packages at this point: monodevelop — an interactive mono development environment similar in some ways to Visual Studio .NET (although nowhere near as sophisticated), and monodoc which provides help and technical documentation.

Just start up a root terminal and type:

% apt-get install mono monodevelop monodoc

When that's finished you've got a mono implementation ready to go, but while you're at it, you may as well include some of the add ons you'll need.

% apt-get install mono-utils mono-xsp monodoc-http

mono-utils provides some command line utilities that are useful if you'll be doing part of your development from the terminal. monodoc-http provides the monodoc manuals as a Web service, which requires the mono-xsp standalone Web server to operate. mono contains mcs the mono C# compiler, but it only compiles .NET 1.1 code, if you want to use the features of .NET 2.0 C# (such as the extremely helpful generics) then you'll need gmcs:

% apt-get install mono-gmcs

If you're planning to use monodevelop to write your code, then you can install a few more packages for SVN, Java, NUnit, Boo and MonoQuery support:

Before we get started on the code, let's take a look at some of the tools we've just installed. The monodoc browser lets you view the mono related manuals you have installed, including the useful C# language specification reference.

Or, if you'd prefer, you can read the documentation in your Web browser. The monodoc-http program starts up an XSP server that runs locally, allowing you to connect to it in any Web browser:

You can also start up the monodevelop IDE now if you wish, although you won't need anything that powerful for the examples we'll be looking at.

Now let's check that the whole thing is working by trying out some code. Take the standard C# Hello World program:

Compile it using mcs and run it with the command mono. And the result:

That works, but it's a completely trivial example, and it doesn't include the most used part of .NET: Windows Forms. Let's see if a simple Windows Forms application will work. First make sure that you've got the relevant libraries installed:

Finally, point your Web browser at http://localhost:8080/ and check out your brand new Linux-built ASP.NET site:

If everything is working so far you've got a completely functioning mono installation, and you should be able to build applications on either Linux or Windows and deploy them on either system.

Be warned, mono is not a perfect replacement, there are parts of the .NET framework currently not implemented, particularly in the area of Windows Forms, so be very careful if you're looking to implement something complicated in mono, or migrate an existing .NET project.