What is Mono? How to install and use it on Ubuntu?

Mono is a free and open-source implementation of the .NET framework which is used for developing and running cross-platform applications. Currently, its development is led by Xamrin which is a subsidiary of Microsoft.

In this article, I will discuss how to install and use Mono on a Ubuntu system.

Prerequisites

To follow this guide you should have the following –

  • A system running Ubuntu 20.04 LTS
  • Access to a user account with sudo privileges

How to install Mono on Ubuntu

The installation of Mono is available through its official repository so you first need to add this repository to your system and then you can run the command for its installation.

Use the following commands to download the repository key and add the mono repository to your system –

sudo apt install gnupg ca-certificates
sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys 3FA7E0328081BFF6A14DA29AA6A19B38D3D831EF
echo deb https://download.mono-project.com/repo/ubuntu stable-focal main" | sudo tee /etc/apt/sources.list.d/mono-official-stable.list

Next, refresh the local apt package repository by running the update command –

sudo apt update

Finally, use the given command to install Mono on your Ubuntu system –

sudo apt install mono-complete

Press y and then Enter if it asks for your confirmation.

You can verify the installation by using –

mono --version

This should display output something like given in the image –

mono version

Testing Mono on Ubuntu

To test if mono is working correctly or not, copy and execute the given code and see if you are getting output or not.

Run the given command to open a text editor-

nano hello.cs

Copy and paste the given code –

//A basic program to test mono on Ubuntu
using System;

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

Save this file as hello.cs and use the following command to compile the code –

csc hello.cs

mono hello.cs compile

This will produce hello.exe file that you can run by using –

mono hello.exe

execute hello.exe mono

By using the given command you can check if HTTPS connections are working or not –

csharp -e 'new System.Net.WebClient ().DownloadString ("https://www.nuget.org")'

This will print the website content if everything working correctly otherwise it will throw an exception.

Now for more information, you can visit its official website.

Conclusion

So I hope you have successfully set up Mono on your Ubuntu system. Now if you have a query then write us in the comments below.

Leave a Comment