How to compile and execute C# programs on Linux?

To compile and execute C# programs on Linux, you have several options. The modern approach is to use the .NET SDK, which provides a cross-platform runtime and compiler. You can also use IDEs like MonoDevelop or Visual Studio Code for development.

The .NET SDK is Microsoft's official cross-platform development platform that runs natively on Linux. MonoDevelop is an open-source IDE that allows you to run C# on multiple platforms including Windows, Linux, and macOS. MonoDevelop is also known as Xamarin Studio and includes a C# compiler.

Installing .NET SDK on Linux

The .NET SDK is the recommended way to compile and run C# programs on Linux. Install it using your distribution's package manager −

# Ubuntu/Debian
sudo apt-get update
sudo apt-get install -y dotnet-sdk-8.0

# CentOS/RHEL/Fedora
sudo dnf install dotnet-sdk-8.0

Compiling and Running with .NET CLI

Create a new console application and run it using the .NET CLI −

dotnet new console -n MyApp
cd MyApp
dotnet run

Example Program

Create a simple C# program file named Program.cs

using System;

class Program {
    static void Main(string[] args) {
        Console.WriteLine("Hello from C# on Linux!");
        Console.WriteLine("Current OS: " + Environment.OSVersion);
        Console.WriteLine("Runtime: " + Environment.Version);
    }
}

Compile and run the program −

dotnet build
dotnet run

The output will be −

Hello from C# on Linux!
Current OS: Unix 5.15.0.91
Runtime: 8.0.0

Using MonoDevelop IDE

MonoDevelop provides a graphical development environment with the following features −

  • Multi-platform IDE − Supports Linux, Windows and macOS.

  • Supports multiple languages − C#, F#, Visual Basic .NET, and others.

  • Integrated Debugger − Built-in debugger for Mono and native applications.

  • Code Completion − IntelliSense, code templates, and code folding.

Installing MonoDevelop

# Ubuntu/Debian
sudo apt install monodevelop

# Fedora
sudo dnf install monodevelop

Alternative Development Options

Tool Description Best For
.NET CLI Command-line tools for building and running Scripting, automation, lightweight development
Visual Studio Code Lightweight editor with C# extension Cross-platform development, modern UI
MonoDevelop Full IDE with integrated tools Complete project management, debugging

Cross-Platform Compilation Example

You can also create platform-specific executables using the .NET CLI −

using System;
using System.Runtime.InteropServices;

class CrossPlatformDemo {
    static void Main() {
        Console.WriteLine("Running on: " + GetOperatingSystem());
        Console.WriteLine("Architecture: " + RuntimeInformation.ProcessArchitecture);
        Console.WriteLine("Framework: " + RuntimeInformation.FrameworkDescription);
    }

    static string GetOperatingSystem() {
        if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
            return "Windows";
        else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
            return "Linux";
        else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
            return "macOS";
        else
            return "Unknown";
    }
}

Compile for specific platforms −

dotnet publish -c Release -r linux-x64 --self-contained
dotnet publish -c Release -r win-x64 --self-contained

Conclusion

C# programs can be easily compiled and executed on Linux using the .NET SDK, which is the modern cross-platform solution. For IDE development, both MonoDevelop and Visual Studio Code provide excellent C# development experiences on Linux with debugging, IntelliSense, and project management capabilities.

Updated on: 2026-03-17T07:04:35+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements