- Mono Basics
- Console Hello World
- HTTPS connections
- WinForms Hello World
- ASP.NET Hello World
- Gtk# Hello World
- Mono
- Installation
- Running Mono applications
- Testing Mono
- Development
- Troubleshooting
- I get an error when I try to run Mono binaries directly: «cannot execute binary file»
- I get an TLS handshake (or similar certificate based) error
- See also
- Running your first Mono application
- How to Install Mono (Microsoft’s .NET Framework) in RHEL Systems
- Installing Mono in RHEL-based Distributions
- On CentOS/RHEL/Rocky & AlmaLinux 8
- On CentOS/RHEL 7
- On CentOS/RHEL 6
- On Fedora 29 and Later
- How to Compile and Run Mono Programs in Linux Terminal
Mono Basics
After you get Mono installed, it’s probably a good idea to run a quick Hello World program to make sure everything is set up properly. That way you’ll know that your Mono is working before you try writing or running a more complex application.
Console Hello World
To test the most basic functionality available, copy the following code into a file called hello.cs.
using System; public class HelloWorld < public static void Main(string[] args) < Console.WriteLine ("Hello Mono World"); >>
Note: csc compiler is not available on all platforms or in very old Mono versions, in such cases use mcs instead.
The compiler will create “hello.exe”, which you can run using:
The program should run and output:
HTTPS connections
To make sure HTTPS connections work, run the following command to check whether you can connect to nuget.org:
csharp -e 'new System.Net.WebClient ().DownloadString ("https://www.nuget.org")'
The program prints the website contents if everything works or throws an exception if it doesn’t.
WinForms Hello World
The following program tests writing a System.Windows.Forms application.
using System; using System.Windows.Forms; public class HelloWorld : Form < static public void Main () < Application.Run (new HelloWorld ()); >public HelloWorld () < Text = "Hello Mono World"; >>
To compile, use csc with the -r option to tell the compiler to pull in the WinForms libraries:
csc hello.cs -r:System.Windows.Forms.dll
The compiler will create “hello.exe”, which you can run using:
NOTE: on macOS you’ll have to wait around a minute the very first time you run this command. You also need to use mono32 since WinForms isn’t supported on 64bit yet. As of macOS 10.15 Catalina there’s no 32bit support anymore so WinForms doesn’t work there at the moment.
ASP.NET Hello World
Create a text file with the name hello.aspx and the content:
Then run the xsp4 command from that directory:
Gtk# Hello World
The following program tests writing a Gtk# application.
using Gtk; using System; class Hello < static void Main () < Application.Init (); Window window = new Window ("Hello Mono World"); window.Show (); Application.Run (); >>
To compile, use mcs with the -pkg option to tell the compiler to pull in the Gtk# libraries (note that Gtk# must be installed on your system for this to work):
mcs hello.cs -pkg:gtk-sharp-2.0
The compiler will create “hello.exe”, which you can run using:
Mono
Mono is a [. ] project to create a .NET Framework-compatible set of tools including, among others, a C# compiler and a Common Language Runtime.
Installation
If you need VisualBasic.Net support you have to install the VisualBasic.Net interpreter with the package mono-basic AUR .
Note: Installing the package stores certificate authorities in /usr/share/.mono/certs/Trust/ but removing the package does not remove them.[1]
Running Mono applications
You can execute Mono binaries by calling mono manually:
You can also execute Mono binaries directly, just like native binaries:
$ chmod 755 exefile.exe $ ./exefile.exe
Testing Mono
using System; public class Test < public static void Main(string[] args) < Console.WriteLine("Hello World!"); >>
$ mcs test.cs $ mono test.exe Hello world!
Development
OmniSharp provides .NET/Mono development plugins/integrations for several editors, including Vim, Emacs, and Visual Studio Code.
Alternatively, you can install the rider AUR IDE. If you install Rider not from AUR, you would need to install mono-msbuild , as recent Rider versions dropped support of xbuild in favour of MSBuild from net-core.
If you want the API documentation browser and some testing and development tools you have to install mono-tools .
Troubleshooting
I get an error when I try to run Mono binaries directly: «cannot execute binary file»
The binfmt_misc handler for Mono has not yet been set up, as explained in detail on the Mono Project website.
To fix this, restart the systemd-binfmt service.
I get an TLS handshake (or similar certificate based) error
This can be caused by either certificates missing from Mono’s certificate store, or stale broken certificates remaining in Mono’s certificate store.
If possible, ensure that the system certificate store is in good order by running curl -vI or similar to replicate the failing request outside Mono.
- cert-sync /etc/ssl/certs/ca-certificates.crt synchronises the mono store with the system store, adding missing certificates.
- To remove broken certificates (i.e., if above didn’t help), remove the directory /usr/share/.mono , then re-run cert-sync /etc/ssl/certs/ca-certificates.crt
- As last resort, the older tool mozroots —import —ask-remove bypasses the system certificate store and directly downloads Mozilla’s trust store. This again does not remove broken certificates, and can cause other problems if you rely on private CAs.
Both cert-sync and mozroots are part of the mono package.
See also
Running your first Mono application
The normal way to run an application you have compiled with Mono would be to invoke it through the Mono runtime, like this:
However, there are two things you can do to make it more convenient to run Mono applications on Linux. The first is to use a shell script instead of the EXE file. For example, if you had “myprogram.exe” you could create a shell script called “myprogram” that had the contents:
#!/bin/sh /usr/bin/mono /usr/bin/myprogram.exe "$@"
When you run “myprogram,” the shell will replace $@ with any arguments you provided.
If you installed mono to a different location, substitute that for /usr/bin/mono. You can check with the “which mono” command.
Debian GNU/Linux systems also ship with the ability to launch Mono or WINE directly as appropriate when exe files are executed.
On other systems, this can be achieved manually using BINFMT_MISC to register the exe files as non-native binaries. Then, when trying to launch an exe file, the kernel will invoke the mono runtime to handle the command. BINFMT_MISC can also be used to launch Windows executables using WINE, or Java .class files using a JVM. To register exe with the kernel:
In addition, you may want to add that command to your /etc/rc.local boot script, so that it will be executed on boot.
binfmt_misc /proc/sys/fs/binfmt_misc binfmt_misc none
echo ':CLR:M::MZ::/usr/bin/mono:' > /proc/sys/fs/binfmt_misc/register
How to Install Mono (Microsoft’s .NET Framework) in RHEL Systems
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:
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.
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.
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.
Now run the hello2.exe executable, which will output a small GUI (graphical user interface) desktop application as shown in the following screenshot.
You can view all mono command options by running the following command:
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.