Install Mono on Linux

http://www.mono-project.com/docs/getting-started/install/linux/#centos-fedora-and-derivatives

The Linux community has made Mono available for various distributions, check the download page for a list of packages.

Regardless of your distribution, you will need the Mono Project GPG signing key, which package managers require.

Debian, Ubuntu, and derivatives

Add the Mono Project GPG signing key in a root shell with:

apt-key adv --keyserver pgp.mit.edu --recv-keys 3FA7E0328081BFF6A14DA29AA6A19B38D3D831EF

Next, add the package repository in a root shell:

echo "deb http://download.mono-project.com/repo/debian wheezy main" > /etc/apt/sources.list.d/mono-xamarin.list

Update your package cache if necessary (i.e. run apt-get update), and run a package upgrade to upgrade existing packages to the latest available. Then install Mono as described in the usage section.

Note: While the APT package is built against Debian Wheezy, it is compatible with a number of Debian derivatives (including Ubuntu) which means you can use the same repository across all these distributions.

Ubuntu 12.04 and 12.10

To enable installation on Ubuntu 12.04 and Ubuntu 12.10 (and their derivatives), you will need to add a second repository to your system, in addition to the generic Debian/Ubuntu repository above:

echo "deb http://download.mono-project.com/repo/debian wheezy-libtiff-compat main" >> /etc/apt/sources.list.d/mono-xamarin.list

mod_mono on Ubuntu 13.10 and later, Debian 8.0 and later

To enable mod_mono installation on Ubuntu 13.10 and later, and Debian 8.0 and later (and their derivatives), you will need to add a second repository to your system, in addition to the generic Debian/Ubuntu repository above:

echo "deb http://download.mono-project.com/repo/debian wheezy-apache24-compat main" >> /etc/apt/sources.list.d/mono-xamarin.list

CentOS, Fedora, and derivatives

Add the Mono Project GPG signing key in a root shell with:

rpm --import "https://pgp.mit.edu/pks/lookup?op=get&search=0x3FA7E0328081BFF6A14DA29AA6A19B38D3D831EF"

Next, add the package repository in a root shell:

yum-config-manager --add-repo http://download.mono-project.com/repo/centos/

Update your package cache if necessary, and run a package upgrade to upgrade existing packages to the latest available.

Users of CentOS or RHEL (or similar distributions) may need to add the EPEL repository to their system to satisfy all dependencies

Usage

The package mono-devel should be installed to compile code.

The package mono-complete should be installed to install everything – this should cover most cases of “assembly not found” errors.

Notes

After the installation completed successfully, it’s a good idea to run through the basic hello world examples on this page to verify Mono is working correctly.

Note: Mono on Linux by default doesn’t trust any SSL certificates so you’ll get errors when accessing HTTPS resources. To import Mozilla’s list of trusted certificates and fix those errors, you need to run mozroots --import --sync.

Testing is mono installed successfully

Mono Basics

After you get Mono installed, it’s probably a good idea to run a quick Hello World program to make sure everything is set up properly. That way you’ll know that your Mono is working before you try writing or running a more complex application.

Console Hello World#

To test the most basic functionality available, copy the following code into a file called hello.cs.

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

To compile, use mcs:

mcs hello.cs

The compiler will create “hello.exe”, which you can run using:

mono hello.exe

The program should run and output:

Hello Mono World

Winforms Hello World#

The following program tests writing a Winforms application.

using System;
using System.Windows.Forms;

public class HelloWorld : Form
{
    static public void Main ()
    {
        Application.Run (new HelloWorld ());
    }

    public HelloWorld ()
    {
        Text = "Hello Mono World";
    }
}

To compile, use mcs with the -pkg option to tell the compiler to pull in the Winforms libraries:

mcs hello.cs -pkg:dotnet

The compiler will create “hello.exe”, which you can run using:

mono hello.exe

NOTE: on Mac OS X you’ll have to wait around a minute the very first time you run this command.

ASP.Net Hello World#

Create a text file with the name hello.aspx and the content:

<%@ Page Language="C#" %>
<html>
<head>
   <title>Sample Calendar</title>
</head>
<asp:calendar showtitle="true" runat="server">
</asp:calendar>

Then run the xsp4 command from that directory:

xsp4

Use a web browser to contact http://localhost:9000/hello.aspx

Gtk# Hello World#

The following program tests writing a Gtk# application.

using Gtk;
using System;

class Hello
{
    static void Main ()
    {
        Application.Init ();

        Window window = new Window ("Hello Mono World");
        window.Show ();

        Application.Run ();
    }
}

To compile, use mcs with the -pkg option to tell the compiler to pull in the Gtk# libraries (note that Gtk# must be installed on your system for this to work):

mcs hello.cs -pkg:gtk-sharp-2.0

The compiler will create “hello.exe”, which you can run using:

mono hello.exe
Advertisements

One thought on “Install Mono on Linux

  1. Unfortunately I get an error message at the first step:
    Add the Mono Project GPG signing key in a root shell with:
    apt-key adv –keyserver pgp.mit.edu –recv-keys 3FA7E0328081BFF6A14DA29AA6A19B38D3D831EF

    And then I can read:
    Executing: gpg –ignore-time-conflict –no-options –no-default-keyring –homedir /tmp/tmp.IQljclljKa –no-auto-check-trustdb –trust-model always –keyring /etc/apt/trusted.gpg –primary-keyring /etc/apt/trusted.gpg –keyserver keyserver.ubuntu.com –recv-keys 3FA7E0328081BFF6A14DA29AA6A19B38D3D831EF
    gpg: requesting key D3D831EF from hkp server keyserver.ubuntu.com
    gpg: key D3D831EF: “Xamarin Public Jenkins (auto-signing) ” not changed

    I don’t understand at all! Perhaps I made a mistake but I don’t know what!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s