Sunday, April 21, 2024
HomeHow toHow to Install Mono .NET Framework on Ubuntu 22.04

How to Install Mono .NET Framework on Ubuntu 22.04

Mono is an open-source implementation of the .NET framework and compatible software framework for building cross-platform applications. It’s based on C# and Common Language Runtime, it also supports multiple platforms, including Linux, Windows, macOS, and embedded devices.

In this tutorial, you will learn how to install Mono on an Ubuntu 22.04 step-by-step. Then, you will learn how to create a Hello World application using Mono and some basic tools for developing your applications.

So let’s begin.

Prerequisites

To get started with this tutorial, ensure you have the following:

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

Adding Mono Repository

Mono is a .NET framework implementation that can be installed on Linux, Windows, macOS, and Docker. In this example, you will install Mono to your Ubuntu system via APT and using the official Mono repository.

To start, run the following command to install the ca-certificates and gnupg package to your Ubuntu server.

sudo apt install ca-certificates gnupg

Type y and press ENTER to confirm with the installation.

Now run the below command to add the Mono GPG key to ‘/usr/share/keyrings/mono-official-archive-keyring.gpg‘.

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

Then, add the Mono repository. In this example, you will install Mono Stable on your Ubuntu system.

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

Lastly, update and refresh your Ubuntu repository using the command below.

sudo apt update

Below you should see Mono repository has been added to your Ubuntu system.

Installing Mono to Ubuntu

Now that you’ve added the Mono repository, let’s start the installation.

First, run the apt command below to show available Mono packages.

sudo apt search mono-*

For the highlight, you can see multiple Mono packages available:

  • mono-devel: This package is used for compiling .Net applications.
  • mono-complete: Fully package for developing .NET applications via Mono.

Now install Mono to your Ubuntu system using the command below. Type y to confirm and press ENTER to proceed. In this example, you will install mono-complete and mono-dbg for debugging.

sudo apt install mono-complete mono-dbg

Once the installation is finished, check the Mono version using the following command.

mono -V

In the following output, you can see Mono 6.12 is installed.

Creating Hello-World Application with Mono

At this point, you’ve now installed mono-complete package to your Ubuntu system, and you’re ready to develop Mono applications. In this example, you will create a basic console application using Mono. You will also utilize the ‘csc’ compiler for compiling your application.

First, use your preferred text editor and create a new file hello-mono.cs.

vim hello-mono.cs

Copy the following code to the hello-mono.cs file.

using System;

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

Save the file.

Now, compile the hello-mono.cs file using the csc compiler like this. Your compiled application should be hello-mono.exe.

csc hello-mono.cs

Lastly, run the hello-mono.exe file using the mono command below. If successful, you should get the ‘Hello Mono World‘ message like the following.

mono hello-mono.exe

Creating ASP.NET Application with Mono

In the following example, you will create a simple ASP.NET application and run it on your Ubuntu system with Mono.

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

vim hello-world.aspx

Insert the following code into your file.

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

Save and exit the file when finished.

Now, execute the following command to run your ASP.NET program.

xsp4 --port 9000

As seen in the following, your application should be accessible via port 9000.

Open your web browser and visit http://localhost:9000/hello-world.aspx. If your installation is successful, you should see the following page.

To terminate the process, you can now press ENTER or Ctrl+c on your terminal.

Conclusion

To conclude, you’ve now installed Mono on your Ubuntu machine. You’ve also learned how to create simple hello-world applications with Mono for two different purposes, creating basic console applications and web applications via ASP.NET.

From now on, you can try to set up an IDE (Integrated Development Environment) on your Ubuntu machine. You can choose between Visual Code Studio, Eclipse, MonoDevelop, and ShardDevelop.


RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here


spot_img

Most Popular