Get Started with Mono

Philip Miseldine
Philip Miseldine

Here’s a disclaimer: I avoid Linux and am no Linux expert by any means. I shudder at the thought of black screens with a flashing cursor. I find myself moving my mouse around trying to find an icon to click or a menu to select.

It’s from such a perspective that this article will demonstrate how anyone (even I!) can get Mono up and running on a fresh, clean Linux box. I’ll walk through my experience of installing the package, and discuss all the components needed to run Mono.

What Is Mono?

Mono is an open source project used to code much of the .NET framework in cross-platform executable code. Sponsored by Novell, Mono gives developers the chance to develop ASP.NET and Windows Forms applications not only for Windows, but now, principally, for Linux.

While they’re not yet complete, applications written for the standard Microsoft .NET framework can now be ported and run on Linux through Mono. However, holes within the framework implementation on Mono mean you’ll still need to check that your application doesn’t use any services that are not yet implemented.

What’s Usable in Mono?

Currently, Mono is in development, but the project has reached some significant milestones that make it suitable and stable enough for deployment today. The C# compiler is the only fully featured compiler for Mono. Yes, that’s right: VB.NET and J# developers will need to switch to C# to fully use Mono. Watch the progress of the VB.NET compiler here.

The compiler itself is written in C#. You can download all 1.7 million lines of C# code and compile this yourself, or, as we’ll see shortly, you can use one of the many distributions to ease the installation process.

ASP.NET is fully-featured and supported within Mono. In fact, it’s the great strength of Mono at present. You can build and deploy both Web forms and Web services using either the built-in Web server that ships with Mono (XSP) or through an Apache modification, Mod Mono. For those who are uncomfortable using Windows and IIS to host applications, Mono provides a viable alternative.

Windows Forms is currently under development, but functionality is progressing. Though complex WinForm applications, such as Web Matrix, are currently unavailable, there are alternatives to WinForms in Mono that build GUI applications. Gtk# and #WT are wrappers to the popular GUI tools on Linux. WinForms itself is being built as a library extension to Wine, the Win32 implementation on Linux. The project’s progress is documented here.

ADO.NET and the System.Data classes are also fairly mature; however, they aren’t at production level at the time of writing. This is one of the largest projects within Mono, and at present you can connect to a wide variety of databases. Native support is provided for many of the databases usually associated with Linux, such as PostgreSQL.

Mono has successfully been used in commercial and heavy-use applications. Novell used the tool to build two of its products, iFolder and ZenWorks. Also, SourceGear has used Web services deployed in Mono within its Vault application.

Getting Started: Download Mono

Mono is available in many packages. You can download the latest source code build and a daily snapshot source code build, through CVS, an RPM package, or a Red Carpet install for those with Xiamian Desktop. By far the easiest to use is the Red Carpet system, which, while similar to RPM, offers good versioning control, allowing you to upgrade Mono on your machine very easily.

The Mono download page details the various packages and how they can be downloaded, as well as specific packages for specific varieties of Linux.

I downloaded the Mono Runtime tarball and used the following command to unpack the distribution:

tar -zxvf mono-1.0.tar.gz

Once the tarball was extracted, I could start the installation process using:

make install

It was at this point I realised I needed to upgrade pkg-config on the system as the installation spat out some errors. I found the RPM distribution for this, and installed it using the following command:

rpm -Uvh pkgconfig-0.15.0-4mdk.i586.rpm

The make process now worked without a hitch.

Hello World in C# Running on Linux

It’s always a good thing to use the cliched “Hello World” example to test an installation!

Open your favourite Linux text editor (I used vi) and enter the following simple C# application code:

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

Save this file as HelloWorld.cs and compile the class with the Mono C# compiler:

mcs HelloWorld.cs

In the directory to which you saved HelloWorld.cs, you should now see a HelloWorld.exe file. This is standard MSIL code that can be executed on any computer on which the .NET framework is installed, including Windows.

There are two ways to run Mono applications. One method is to use “mint,” which is an interpreter for the MSIL bytecode. Mint does not compile and run your applications into native machine code, hence it is not a JIT runtime. “Mono” however is a JIT runtime, which compiles bytecode when first requested into machine code native for the platform and processor for which it was designed.

This means that, for performance, the Mono application is much faster than mint, though it’s not as portable as it is tied to the particular operating system. Mint, on the other hand, is far more portable and, as it’s written in ANSI C, may be used on a multitude of deployment platforms.

To run our Hello World application, we can use the following command, which invokes Mono:

mono hello.exe

We use the following command to invoke mint:

mint hello.exe
Dishing Up ASP.NET

Mono comes with its own Web server, ready to dish out your ASP.NET applications. Mono. XSP is very easy to use and makes for a simple ASP.NET Web server that is almost akin to the Cassini server that ships with Web Matrix on Windows. For more dominance, you can download a module called mod_mono that allows Apache to support ASP.NET. In this article, however, we’ll examine the formation of a simple Web application and host it using XSP.

I must admit that I cheated when I created the code for the Web application: I used Visual Studio to create the Web application and its associated files. Then, I copied the code over to the Linux box that was ready to host.

For our example, we’ll create a Web page with a simple button and a label. When the button is clicked, the label will show that the user clicked the button.

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

void Button1_Click(object sender, EventArgs e)  
  titleTag.InnerText = "You clicked the button!";  

void Page_Load(object sender, EventArgs e)  
  if (!IsPostBack)  
    titleTag.InnerText = "Page has loaded";  

<title runat="server" id="titleTag"/>  
<form runat="server">  
  <asp:Button id="Button1" OnClick="Button1_Click" runat="server" Text="Button"/>  

You can save this file to a directory that will act as your root for the Web application.

If you are using codebehinds for your application, you will also need to compile the source files using the Mono compiler, in order to obtain the compiled assembly for the application. Just as with ASP.NET on Windows, you’ll need to drop this into a /bin directory on the root.

XSP, by default, listens on the 8080 port so as to not interfere with Apache; however, you can set up the server, via the command line, to listen at a different port. You can also specify the root directory of the application you wish to host:

xsp.exe  [--root rootdir]  
               [--port N]

With the server running, you can access your page through any standard Web browser. And, there we have it: ASP.NET served over Linux!

Third Party Tools

There are numerous tools available to aid your developments in Mono so that, unlike me, you don’t need to resort back to Visual Studio.

  • MonoDevelop: With GUI features still lacking in Mono, MonoDevelop is really pushing the current implementations to show what can be achieved in Mono. Resembling #develop for Windows, MonoDevelop supports syntax highlighting and the compilation of projects from an easy-to-use interface. However, this tool is still at an early stage of development and presently lacks the features needed to make it a truly useful instrument.
  • Eclipse: Billed as “a kind of universal tool platform — an open extensible IDE for anything and nothing in particular,” Eclipse is a great solution for developing Mono applications. By downloading and installing the Improve C# plugin for Eclipse you can have a fully-featured and free Java based IDE for your Mono developments.
  • MonoDoc: This is a browser for the Mono documentation. It isn’t installed by default through the standard Mono packages, but it is a life saver for those needing to check whether certain APIs and parts of the framework are available in Mono.

From my study of Mono, I’ve come to understand that this is a very important project for .NET. The release of .NET from the confines of Microsoft operating systems will allow it to expand within communities that are normally off-limits to Microsoft technologies.

Mono can only grow stronger, and perhaps in the near future, .NET developers will be able to develop for Linux as easy as we develop for Windows today.

Frequently Asked Questions about Getting Started with Mono

What is Mono and why is it important in software development?

Mono is an open-source platform that allows developers to build cross-platform applications with .NET. It is important in software development because it provides the necessary tools to develop and run .NET client and server applications on different platforms such as Linux, Windows, and Mac. This means that developers can write a single application that can run on multiple platforms, reducing the time and effort required to develop separate applications for each platform.

How does Mono compare to .NET Core?

While both Mono and .NET Core allow for cross-platform development, there are some key differences. Mono is a full-stack framework that includes everything from the runtime to the GUI toolkit, while .NET Core is a modular framework that allows developers to pick and choose the components they need. Additionally, Mono supports more platforms than .NET Core, including older versions of Windows and some game consoles.

How can I install Mono on my system?

Installing Mono on your system is a straightforward process. You can download the latest stable release from the Mono project’s official website. Once downloaded, you can follow the installation instructions provided for your specific operating system. It’s important to note that Mono requires certain dependencies to be installed on your system, so make sure to check the requirements before installing.

What programming languages are supported by Mono?

Mono supports a wide range of programming languages, including C#, F#, Visual Basic.NET, and more. This makes it a versatile tool for developers who work with different programming languages.

Can I use Mono to develop mobile applications?

Yes, you can use Mono to develop mobile applications. Xamarin, a popular mobile app development platform, is built on Mono. This means you can use C# and .NET to build apps for iOS, Android, and Windows.

What are some common issues developers face when using Mono?

Some common issues developers face when using Mono include compatibility issues with .NET, performance issues, and lack of support for some .NET features. However, the Mono community is active and constantly working to improve the platform and address these issues.

How can I contribute to the Mono project?

As an open-source project, Mono welcomes contributions from developers. You can contribute by submitting bug reports, proposing new features, or contributing code. You can find more information on how to contribute on the Mono project’s official website.

Is Mono suitable for enterprise-level applications?

Yes, Mono is suitable for enterprise-level applications. It provides a robust and scalable platform for developing and running .NET applications on multiple platforms. Many large companies use Mono for their software development needs.

Can I use Mono with my existing .NET codebase?

Yes, you can use Mono with your existing .NET codebase. Mono aims to be compatible with .NET, so you can use it to run your existing .NET applications on different platforms.

What is the future of Mono?

The future of Mono looks promising. With the acquisition of Xamarin by Microsoft, Mono has gained more recognition and support. The Mono project continues to evolve and improve, with new features and improvements being added regularly.