Installing Mono .NET Framework on Ubuntu 22.04

Mono is an open-source implementation of the .NET framework, providing a compatible software framework for building cross-platform applications. It leverages C# and the Common Language Runtime, supporting multiple platforms such as Linux, Windows, macOS, and various embedded devices.

In this detailed guide, you’ll learn how to install Mono on an Ubuntu 22.04 system. We’ll also cover creating a simple Hello World application using Mono, along with some basic tools for developing applications.

Let’s get started.

Prerequisites

Before beginning this tutorial, ensure you have the following:

  • An Ubuntu 22.04 machine – Desktop or Server.
  • A non-root user with administrative privileges.

Adding the Mono Repository

Mono, a .NET framework implementation, can be installed on Linux, Windows, macOS, and Docker. In this tutorial, you’ll install Mono on your Ubuntu system using APT and the official Mono repository.

First, execute the following command to install the ca-certificates and gnupg packages on your Ubuntu server:

sudo apt install ca-certificates gnupg

Type y and press ENTER to confirm the installation.

install packages

Next, add the Mono GPG key to ‘/usr/share/keyrings/mono-official-archive-keyring.gpg‘ using the command below:

sudo gpg --homedir /tmp --no-default-keyring \
--keyring /usr/share/keyrings/mono-official-archive-keyring.gpg \
--keyserver hkp://keyserver.ubuntu.com:80 --recv-keys 3FA7E0328081BFF6A14DA29AA6A19B38D3D831EF

add gpg key

Then, add the Mono repository to your system. Execute the following:

echo \
"deb [signed-by=/usr/share/keyrings/mono-official-archive-keyring.gpg] https://download.mono-project.com/repo/ubuntu stable-focal main" | \
sudo tee /etc/apt/sources.list.d/mono-official-stable.list

add repository

Finally, update and refresh your Ubuntu repository using the following command:

sudo apt update

Your Ubuntu system should now recognize the Mono repository:

update repo

Installing Mono on Ubuntu

Having added the Mono repository, you can now proceed with the installation.

First, list the available Mono packages using the command:

sudo apt search mono-*

This will display Mono packages such as:

  • mono-devel: Used for compiling .NET applications.
  • mono-complete: A comprehensive package for developing .NET applications using Mono.

mono packages

Install Mono on your Ubuntu system with the following command. Confirm by typing y and pressing ENTER when prompted. In this tutorial, we will install mono-complete and mono-dbg for debugging purposes:

sudo apt install mono-complete mono-dbg

install Mono

After installation, verify the Mono version with the command below:

mono -V

The output should indicate that Mono 6.12 is installed:

checking Mono version

Creating a Hello World Application with Mono

With the mono-complete package installed, you’re equipped to develop Mono applications. Here, we’ll create a basic console application using Mono and compile it with ‘csc’.

Use your preferred text editor to create a new file called hello-mono.cs:

vim hello-mono.cs

Input the following code into hello-mono.cs:

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

Save the file.

Next, compile hello-mono.cs using the csc compiler. The output should be hello-mono.exe:

csc hello-mono.cs

Finally, run the compiled hello-mono.exe with the following command. You should see the output ‘Hello Mono World‘:

mono hello-mono.exe

create hello world

Creating an ASP.NET Application with Mono

In this section, we’ll create a simple ASP.NET application and run it on your Ubuntu system using Mono.

Create a new file ‘hello-world.aspx‘ with your preferred editor:

vim hello-world.aspx

Add the following code to hello-world.aspx:

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

Save and exit the file.

Execute the following command to run your ASP.NET program:

xsp4 --port 9000

Your application should be accessible via port 9000:

run aspx mono

Open a web browser and navigate to http://localhost:9000/hello-world.aspx. If the installation succeeded, the following page will display:

aspx mono

To terminate the process, press ENTER or Ctrl+c in your terminal.

Conclusion

You have successfully installed Mono on your Ubuntu machine. This guide also covered creating basic Mono applications, including console and web (ASP.NET) applications. Moving forward, consider setting up an Integrated Development Environment (IDE) such as Visual Studio Code, Eclipse, MonoDevelop, or ShardDevelop on your Ubuntu machine for enhanced productivity.

Frequently Asked Questions

  • What is Mono?
    Mono is an open-source implementation of the Microsoft .NET framework that works across multiple operating systems, enabling the development of cross-platform applications.
  • Can I use Mono with any IDE?
    Yes, Mono is compatible with several IDEs, including Visual Studio Code, Eclipse, MonoDevelop, and SharpDevelop.
  • What platforms does Mono support?
    Mono supports multiple platforms, including Linux, Windows, macOS, and various embedded systems.
  • Is Mono necessary for running .NET applications on Linux?
    Mono enables running and developing .NET applications on Linux, providing a versatile framework for cross-platform software development.
  • How do I update my Mono installation?
    You can update Mono through the Mono repository by running sudo apt update followed by sudo apt upgrade.