Software Development

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:

% apt-get install monodevelop-versioncontrol monodevelop-java monodevelop-nunit monodevelop-boo monodevelop-query

Likewise, if you're planning to use monodoc (strongly recommended) you can install manuals for whichever toolkits you'll be using:

% apt-get install monodoc-nunit-manual monodoc-ipod-manual monodoc-gtk2.0-manual

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:

using System;
namespace Hello {
	class HelloWorld {
		public static void Main(string[] args) {
			Console.WriteLine("Hello World!");

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:

% apt-get install libmono-winforms1.0-cil libmono-winforms2.0-cil

Now the source code:

using System;
using System.Windows.Forms;

namespace HelloClickWorld {
	public class Hello : Form {
		public static void Main (string[] args) {
			Application.Run (new Hello ());

		public Hello ()
			Button button = new Button ();
			button.Text = "Click...";
			button.Click += new EventHandler (Button_Click);
			Controls.Add (button);

		private void Button_Click (object sender, EventArgs e)
			MessageBox.Show ("Hello Click World!");

Compiling to assembly this time is a little more complicated, since you need to tell the C# compiler to include the library for Windows Forms:

% mcs -r:System.Windows.Forms hiclickworld.cs
% mono hiclickworld.exe

Finally we need to make sure ASP.NET will work just as well. Save the following as index.aspx:

<%@ Page Language="C#" %>  
<script runat="server">  

void Button1_Click(object sender, EventArgs e)  
  Label1.Text = "Hi Click World!";  

<title>Hello World</title> 
<form runat="server">  
  <asp:Button id="Button1" OnClick="Button1_Click" runat="server" Text="Button"/>  
  <asp:Label id="Label1" runat="server"/>

Then start an xsp server in that directory.

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.


Editor's Picks