How to Install Mono .NET Framework in RHEL-based Linux
Mono is a free, open-source, and cross-platform implementation of Microsoft’s .NET framework, which runs on Linux, macOS, BSD, and Windows and supports various CPU architectures such as x86, ARM, PowerPC, and more. Mono enables software developers to easily create cross-platform applications using C# language.
The Mono project is sponsored by Microsoft and is part of the .NET foundation, which is comprised of the C# compiler, the mono runtime, the base class library, and the mono class library.
In this guide, we will show how to install Mono (Open Source .NET Framework) in RHEL-based distributions such as CentOS, Rocky Linux, AlmaLinux, and Fedora Linux. It also shows how to compile and run mono programs from the command line.
Installing Mono in RHEL-based Distributions
To install Mono, first, set up the package repository on your system, then install the mono package by running the following commands. Remember to run the appropriate commands for your operating system version.
For each version section, the first command imports the repository key, the curl command sets up the repository configuration file, and the final dnf command installs the mono package.
On CentOS/RHEL/Rocky & AlmaLinux 8
# rpmkeys --import "http://keyserver.ubuntu.com/pks/lookup?op=get&search=0x3FA7E0328081BFF6A14DA29AA6A19B38D3D831EF" # su -c 'curl https://download.mono-project.com/repo/centos8-stable.repo | tee /etc/yum.repos.d/mono-centos8-stable.repo' # dnf install mono-devel
On CentOS/RHEL 7
# rpmkeys --import "http://keyserver.ubuntu.com/pks/lookup?op=get&search=0x3FA7E0328081BFF6A14DA29AA6A19B38D3D831EF" # su -c 'curl https://download.mono-project.com/repo/centos7-stable.repo | tee /etc/yum.repos.d/mono-centos7-stable.repo' # yum install mono-devel
On CentOS/RHEL 6
# rpm --import "http://keyserver.ubuntu.com/pks/lookup?op=get&search=0x3FA7E0328081BFF6A14DA29AA6A19B38D3D831EF" # su -c 'curl https://download.mono-project.com/repo/centos6-stable.repo | tee /etc/yum.repos.d/mono-centos6-stable.repo' # yum install mono-devel
On Fedora 29 and Later
# rpm --import "https://keyserver.ubuntu.com/pks/lookup?op=get&search=0x3FA7E0328081BFF6A14DA29AA6A19B38D3D831EF" # su -c 'curl https://download.mono-project.com/repo/centos8-stable.repo | tee /etc/yum.repos.d/mono-centos8-stable.repo' # dnf update # dnf install mono-devel
The following are extra mono packages you can also install:
- mono-complete – contains everything required for working with Mono applications.
- mono-dbg – offers debugging symbols for framework libraries.
- referenceassemblies-pcl – offers PCL compilation support, and.
- xsp – a web server required for running ASP.NET applications.
For example, you can install the mono-complete package like so:
# yum install mono-complete OR # dnf install mono-complete
After successfully installing the Mono package on your system, you need to verify the installation. A simple step is to check the version of the mono package installed, as follows:
# mono -V OR # mono --version
How to Compile and Run Mono Programs in Linux Terminal
The next step is to ensure that the Mono components are properly set up. You can test this using a small hello world program. Copy the following code and paste it into a file called hello.cs.
using System; public class HelloWorld { public static void Main(string[] args) { Console.WriteLine ("Hello Mono World – This is TecMint.com"); } }
Now compile the hello.cs program using the C# compiler (csc) by running the following command. It will generate a hello.exe executable file within the current directory.
$ csc hello.cs
Next, run the hello.exe executable file using the mono command. It should display the string: Hello Mono World – This is TecMint.com as shown in the screenshot that follows.
$ mono hello.exe
Let’s look at another simple example code, which will create a System.Windows.Forms desktop application. Copy and paste it into a hello2.cs file.
using System; using System.Windows.Forms; public class HelloWorld : Form { static public void Main () { Application.Run (new HelloWorld ()); } public HelloWorld () { Text = "Hello Mono World – This is TecMint.com"; } }
Next, compile the program like before using the csc command as follows. It will also generate an executable file called hello2.exe within the working directory.
$ csc hello2.cs
Now run the hello2.exe executable, which will output a small GUI (graphical user interface) desktop application as shown in the following screenshot.
$ mono hello2.exe
You can view all mono command options by running the following command:
$ mono -h
That’s all for now! The Mono project is supported by an active and enthusiastic contributing community. If you are interested in the project, you can contribute by filing a bug report, adding new code or chatting with the developers, or much more.
For more information about the Mono project, check out the project’s official website: https://www.mono-project.com/