SoFunction
Updated on 2025-04-06

How to run C# on Linux

This article describes how to run C# on Linux. Share it for your reference. The specific methods are as follows:

On any platform (operating system + hardware system), the three most fundamental requirements for writing and running programs are libraries, compilers/interpreters, and running environments. Library provides common routines in the form of classes and methods (functions), simplifying the writing of large programs. The .NET framework is no exception, and it contains many class libraries. In addition, when converting programs into executable forms and running execution files, the compiler and the running environment are essential. The Mono package contains part of the .NET class library, a C# compiler, and .NET runtime environment CLR (Common Language Runtime environment).

Mono claims to support Linux, Solaris, Free BSD and MS Windows; in addition to Intel x86 series CPUs (486, various Pentiums, etc.), it is said to also support Sparc, PowerPC and StrongArm processors.

Let’s take a look at how to run Mono on Linux.

Command line application

First download the software from the Mono homepage (/), and execute the following command to install all RPMs:

Copy the codeThe code is as follows:
rpm -ivh *.rpm

After installation, all basic dynamic execution libraries, including,,, and are copied to /usr/lib. Next, enter the following content in the Linux text editor and save the file as:

Copy the codeThe code is as follows:
class HelloMono
{
public static void Main(string[ ] args)
{ ("Hello Mono");
}
}

Execute the following command to compile the C# file:

Copy the codeThe code is as follows:
mcs

mcs is Mono's C# command line compiler. Similar to the csc compiler of MS .NET SDK, mcs also has many command line options. The above command will generate an executable file. Note that this executable file is not a Linux executable file, but a .NET executable file, or the executable code form of this file is an intermediate language (IL, Intermediate Language). To run this execution file, you must execute the following command:

Copy the codeThe code is as follows:
mono

"Hello Mono" will appear on the Linux console. For more instructions on mcs and Mono, please do man mcs or man mono to refer to its man documentation.

GUI Application

Mono does not support Windows Forms, but GUI can be written in C# programming using GTK #. GTK # is a C# support tool for the GTK+ graphics library, which can be downloaded from/to. After the download is complete, you must first install RPM:

Copy the codeThe code is as follows:
rpm -Uvh *.rpm --nodeps

Unlock gtk-sharp-0.2.:

Copy the codeThe code is as follows:
tar -zxvf gtk-sharp-0.2.

Enter the subdirectory gtk-sharp-0.2.1 and execute:
Copy the codeThe code is as follows:
./configure --prefix=/usr
make
make install

After completing this step, restart once. All dynamic code modules (,,,,,) related to GTK# will be copied to the /usr/lib directory. When compiling C# programs that use GTK# to make GUIs, they must be referenced through the -r parameter. The GTK# download package contains several sample programs, such as, etc. The command to compile these files is as follows:

Copy the codeThe code is as follows:
mcs -r gtk-sharp -r glib-sharp
mcs -r gtk-sharp -r glib-sharp -r
mcs -r gtk-sharp -r glib-sharp -r

Let’s take a look at how to reference the previous .dll dynamic modules through the -r option. Execute the startx command to start X Window, enter the terminal window, and then enter the directory where the sample program is located. Execute the following command to run each program:

Copy the codeThe code is as follows:
mono
mono
mono

The Mono project is still being improved and developed, and all .NET classes have not been transplanted yet. But anyway, it will be good news for many. For information about the progress of Mono C# library transplantation, interested friends can visit /.

Consider a typical server/client .NET application: end users only deal with clients, preferably convenient GUI applications, and many people will also want to use familiar Windows platforms. However, for them, it is completely irrelevant to use Windows or Linux on the server side. Therefore, the server side can be constructed with Linux with Mono, saving investment in equipped with dedicated Windows servers. Mono will also bring convenience to application migration. If Mono can develop smoothly, Windows developers will also be able to develop applications for Linux or other non-Windows platforms. If this is the case, it might be possible that a Linux development IDE similar to Visual Studio .NET is coming soon, but free or cheap.

However, whether all this can become a reality depends on whether Mono can thrive, or how compatible Mono is with the .NET framework on Windows platforms. Other factors are also critical, such as Mono's performance, GUI friendliness, etc.

I hope this article will be helpful to everyone's C# programming.