How to Install Mono on Ubuntu 24.04?


Mono is an open-source initiative tool headed by Xamarin. It offers a suite of tools that are compatible with the .NET Framework. This suite includes, among other resources, a C# compiler and a Common Language Runtime. Mono’s standout feature is its facilitation of cross-platform application development from a unified codebase, enabling developers to streamline their workflow efficiently. The installation of Mono on Ubuntu 24.04 ensures you have the necessary tools to develop .NET applications on your Linux system.

Based on its importance, this guide will walk through the step-by-step instructions for installing Mono as well as compiling the C# program on Ubuntu 24.04.

Let’s begin this guide with installation.

How to Install Mono on Ubuntu 24.04?

The most reliable way to install Mono on Ubuntu 24.04 is by using the official Mono repository. This ensures users get the latest version and all the necessary dependencies. To install Mono via the Official Mono Repository, follow the below steps:

Step 1: Install Dependencies

Before you begin, make sure your system is up-to-date and, install the necessary dependencies for adding a new repository over HTTPS:

sudo apt update && sudo apt upgrade # Update System

sudo apt install dirmngr gnupg apt-transport-https ca-certificates software-properties-common # Install Dependencies

Step 2: Add Mono Repository GPG Key

Then, add the Mono repository GPG key to the system to ensure the authenticity of the package:

sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys 3FA7E0328081BFF6A14DA29AA6A19B38D3D831EF

Step 3: Add Mono Repository

After adding GPG keys, add the Mono repository to the system by mentioning the URL:

echo "deb [arch=amd64] https://download.mono-project.com/repo/ubuntu stable-focal main" | sudo tee /etc/apt/sources.list.d/mono-official-stable.list

Step 4: Update Package Index

Now, you can update the system packages list to configure the added repository:

sudo apt update

Step 5: Install Mono

Finally, install Mono from the added repository. The “mono-devel” package installs the Mono runtime, development tools, and libraries. While the “mono-complete” package installs all additional packages for extensive use. Let’s install the “mono-complete” package:

sudo apt install mono-devel # Developer Package
sudo apt install mono-complete # For Complete Package

Step 6: Check Mono Version

After the installation, users can check the version to confirm the successful installation via the “version” utility:

mono --version

Uninstall/Remove Mono

If users need to uninstall Mono, use the “autoremove” utility with the “mono-complete” package name:

sudo apt remove mono-complete # Remove Package Only
sudo apt purge mono-complete # Remove Package as well Dependent Files
sudo apt autoremove mono-complete # Completely Remove Mono

It removes Mono and its dependencies from the system.

If users need a specific version of Mono, users can manually download and install the desired version from the Mono project.

Let’s move forward to the usage of Mono by compiling and running the C# program on Ubuntu 24.04.

How to Use Mono on Ubuntu 24.04?

After the successful installation, users can compile and run C# programs with the help of the “csc” command. To run the executable, use the “mono” command. For compiling and running C# programs, follow these steps:

Step 1: Write C# Program

First, create a C# program file “linuxgenie.cs” using the nano editor as below:

sudo nano linuxgenie.cs

Write your C# code. For instance, let’s create a simple program for presenting a message:

using System;
public class linuxgenie
{
public static void Main(string[] args)
{
Console.WriteLine ("Linuxgenie empowers linuxusers");
}
}

Save the file using the “CTRL+O” key.

Step 2: Compile C# Source Code

Now, use the csc compiler to create an executable (linuxgenie.exe) from the “linuxgenie.cs” file:

csc linuxgenie.cs

Note: Users can replace the file name with the actual name of their C# file (e.g., csc linuxgenie.cs).

If your program has no errors, it will create a “linuxgenie.exe” file in the same directory.

Step 3: Execute C# Program

Finally, execute the compiled “linuxgenie.exe” file and press Enter to display the output:

mono linuxgenie.exe

Step 4: Set an Executable File

Alternatively, users can set the compiled file as an executable with the help of the “chmod” command with “+x” permission:

chmod +x linuxgenie.exe
./linuxgenie.exe

In this way, the C# program is successfully compiled as well as executed with the help of the csc command.

Conclusion

Mono provides a vital bridge for .NET developers to bring their applications to the Linux environment. To install Mono on Ubuntu 24.04, install dependencies, add the Mono repository, update the package index, and install Mono via the “sudo apt install mono-complete” command. Users can also compile and run C# program files using the “mono” command. This guide has explained the step-by-step procedure for installing Mono on Ubuntu 24.04.

Print Friendly, PDF & Email
Categories