How to Install Mono on Ubuntu 20.04?

Overview

Mono is an open-source implementation of Microsoft's .NET Framework that allows you to run C# code on Linux. In this tutorial, we will focus on installing Mono on Ubuntu 20.04 and building a program with Mono.

Prerequisites

There are certain prerequisites that need to be met before you begin.

  • Ubuntu 22.04 LTS configured on your system.

  • Non-root sudo user privileges.

Get Started

Step 1: Installing Mono

  • Update the Ubuntu package index:

sudo apt update
  • Install the essential dependencies to incorporate a fresh repository via HTTPS:

sudo apt install dirmngr gnupg apt-transport-https ca-certificates software-properties-common
  • Add the Mono repository GPG key:

sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys 3FA7E0328081BFF6A14DA29AA6A19B38D3D831EF
  • Add the Mono repository to your system's sources list:

sudo apt-add-repository 'deb https://download.mono-project.com/repo/ubuntu stable-focal main'
  • Install Mono by running the following command:

sudo apt install mono-complete
  • After the installation is complete, verify that Mono is installed by checking its version:

mono --version

Step 2: Building a basic program with Mono

  • Open the nano text editor and create a new file with the extension '.cs':

nano program2.cs
  • Add the following code to the file:

using System;
 
public class WelcomeMono
 
{
      public statics void Main(string[] args)
      {
          Console.WriteLine ("Welcome to Mono!");
       }
 
}

Press CTRL + X to save and exit the file.

  • Compile the program:

csc program2.cs

This command will compile the file program2.cs and create an executable file called program2.exe.

  • Run the program:

mono program2.exe

Conclusion

In this tutorial, you have successfully installed Mono, built and run a basic program with Mono on Ubuntu 20.04. You can now start developing C# applications on Linux.

Last updated