Building Mono and XSP Server on Linux

Overview

This tutorial describes how to build the Mono C# runtime framework and the XSP server on a Linux computer.  The Mono runtime framework allows you to run applications written in C# on a Linux machine.  The XSP server is required if you want to host ASP.NET web applications on the Linux server.

The mono runtime framework is installed on most popular Linux distributions. The official package for most distributions is not necessarily up to date. To get the latest version of Mono, you can either download the latest build from the Mono project website here, or you can build it yourself.

This tutorial was written for CentOS 6.5. It has been tested on a 64 bit virtual machine, and a 32 bit real computer.  CentOS was used for this project because it is one of the few popular distributions that comes with no Mono runtime framework installed, and this makes it a bit easier when you want to build from the source code.

Build and Install Mono

The following instructions were adapted from the guide on the Mono project site. The instructions on the mono project page describe setting up mono on a machine that already has mono.  The CentOS machine is starting without mono, so we are using –prefix=/usr instead of –prefix=/usr/local.  There are a few other changes required to get it all to work.

1. Before you start building mono, you need to make sure you have the basics

$ sudo yum install automake
$ sudo yum install libtool
$ sudo yum install gcc-c++

2. Open a terminal, and go to /usr/src, make a directory for mono, and go to that directory.

$ cd /usr/src
$ mkdir mono
$ cd mono

3. Get the latest Mono source code, build it, and install it (this will take a while)

$ git clone git://github.com/mono/mono.git

$ cd mono
$ ./autogen.sh --prefix=/usr
$ make get-monolite-latest
$ make EXTERNAL_MCS="${PWD}/mcs/class/lib/monolite/gmcs.exe"
$ make install

4. Test to see if it worked, check the mono version and the mcs version

$ mono --version
$ mcs --version

You should see something like this:

[root@localhost mono]# mono --version
Mono JIT compiler version 3.10.1 (master/05d3413 Fri Sep 19 12:41:30 PDT 2014)
Copyright (C) 2002-2014 Novell, Inc, Xamarin Inc and Contributors. www.mono-project.com
TLS:           __thread
SIGSEGV:       altstack
Notifications: epoll
Architecture:  x86
Disabled:      none
Misc:          softdebug
LLVM:          supported, not enabled.
GC:            sgen
[root@localhost mono]# mcs --version
Mono C# compiler version 3.10.1.0

5. Now build GTK, which is required for windows forms applications

$ PKG_CONFIG_PATH=/usr/lib/pkgconfig
$ export PKG_CONFIG_PATH
$ yum install glib2-devel libX11-devel pixman-devel fontconfig-devel freetype-devel libexif-devel libjpeg-devel libtiff-devel libpng-devel giflib-devel

//  note, x.x.x is current version such as 2.10.9

$ cd /usr/src/mono/libgdiplus-x.x.x
$ ./configure --prefix=/usr

6. This next step makes no sense at all, but it is required to make it work as described on this stack overflow post.

$ export echo=echo

7. Finally, make and install it

$ make
$ make install

Build and Install XSP Server

XSP is a standalone web server written in C# that can be used to run your ASP.NET applications with minimal effort. XSP works under both the Mono and Microsoft runtimes.  This component, together with your web server (Nginx, Apache, …) plays the same role as IIS on a Windows machine.

The instructions to build and install XSP are reprinted below from the link on the mono project web site:  http://www.mono-project.com/ASP.NET.

1. Get the source code

$ cd /usr/src/mono
$ git clone git://github.com/mono/xsp.git
$ cd xsp

2. This extra step is required to make it work

$ export PKG_CONFIG_PATH=/usr/lib/pkgconfig

3. Now configure, make, and install.

$./configure --prefix=/usr
$ make
$ make install

4. Check the results by checking the xsp version

$ xsp --version
Mono.WebServer2.dll 0.2.0.0
(c) (c) 2002-2011 Novell, Inc.
Classes for embedding an ASP.NET server in your application .NET 2.0.

You are now ready to run C# applications on your Linux machine. If you wish to host an ASP.NET web service, you must next configure your web server to work with the XSP server.  Installing the Nginx web server and configuring FastCGI to work with XSP so you can serve ASP.NET web applications is described in the next tutorial.

Build and Install MonoDevelop IDE

If you don’t have Visual Studio, you can use the MonoDevelop IDE to develop and debug your C# projects. Even if you do have Visual Studio, and you plan on doing most of your work on the PC, you may still run into issues where things don’t quite work right on the Linux machine. Therefore, you may want to install the MonoDevelop IDE on your Linux machine.

It is possible to build MonoDevelop from source code, as described here. For this project, we attempted this, but using both a CentOS 64 bit virtual machine and a 32 bit real our CentOS machine, we ran into the same problem that is described here.

For our development and testing, we setup a virtual machine with openSUSE linux to run the MonoDevelop IDE on.  This is described in more detail in our Beginner’s Guide to ASP.NET on Linux with Mono.