by

How to install Mono on CentOS 8

Mono is a software platform intended for developing and running cross-platform applications based on ECMA/ISO standards. It is a free and open-source project sponsored by Microsoft that supports many modern operating system architectures. Mono is used to create .Net framework compatible software and includes a C# compiler and the CLR (Common Language Runtime).

In this article, we will describe how to install Mono on CentOS 8 and how to write and compile your first C# program on Linux.

Prerequisites

You must be logged in with sudo privileges or as a root user on your CentOS system.

Installation of Mono on CentOS 8

This is the recommended and easiest method to install Mono on CentOS 8 is to install it from its official repository. It is quite a simple process and will just take a few minutes. Follow the following steps to install the Mono on CentOS 8:

Open the terminal by clicking on the Activities located at the top left corner and click on the terminal icon from the left sidebar of the applications pane.

Import the GPG key

Install the necessary packages and start the installation by importing the GPG key of the required Mono repository. Execute the below-mentioned command to perform this step.

$ sudo rpm --import 'http://pool.sks-keyservers.net/pks/lookup?op=get&search=0x3fa7e0328081bff6a14da29aa6a19b38d3d831ef'

Add RPM key of the Mono repository

You will see no output on the terminal on success.

Add Mono Repository

In this step, you need to add the Mono repository to your CentOS system by executing the command below:

$ dnf config-manager --add-repo https://download.mono-project.com/repo/centos8-stable.repo

After running the above command, the Mono repository to be added to your system. The following output you should see on the terminal:

Add Mono Repository on CentOS 8

Install Mono

Once you setup the repository, install Mono on your system by running the following command on the terminal:

$ sudo dnf install mono-complete

Install Mono using dnf package managerAdvertisement

Press ‘y’ and then hit ‘Enter’ to proceed further.

Package installation progress

In the above command, the ‘mono-complete’ is used for the meta package that installs all development tools, libraries, and Mono runtime.

Check Mono version

To verify the Mono installation on your system, run the following command that displays the installed Mono version:

$ mono --version

At the time of writing this article, the latest Mono available version is 6.12.0.90. As you can see in the following image the Mono has been installed on our system:

Check installed Mono version

Once the installation of Mono is completed, you can use it on CentOS 8.

Run C# code example with Mono on CentOS

To check that Mono is set up correctly, we are creating a test C# program to run with Mono and prints a message ‘Test file for Hello World!’. For this purpose, create a file named ‘helloworld.cs’ by using the touch command:

$ touch helloworld.cs

Create C# Code file

Open the above file in your favorite text editor and paste the following code in it:

using System;

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

Save the file and compile or build the above program by using the csc compiler. Run the following command for building the above program:

$ csc helloworld.cs

Compile C# Code on CentOS

The above command will make an executable file named ‘helloworld.exe’.

Now, execute the program by running the following command on the terminal:

$ mono helloworld.exe

The following output should display on the terminal:

Run test program

You can execute the program by only typing its name. For this purpose, you need to set up an executable flag by using the following command:

$ chmod +x helloworld.exe

Now, you can run the helloworld.exe file by typing the following command:

$ ./helloworld.exe

Conclusion

In this article, we have learned how to install Mono on CentOS 8. You can find the latest Mono release packages for installation from the official Mono package repository. Feel free to give us your feedback in case of any problem.

Comment

Leave a Reply

Your email address will not be published. Required fields are marked *

News Feed