How to Install C# on Linux (and Usage with Practical Examples)

Linux TLDR
Last Updated:
Reading time: 4 minutes

C# (pronounced “C sharp”) is a modern, high-level, multiple paradigms, and object-oriented programming language developed by tech titan Microsoft.

Originally debuted in the early 2000s as part of the .NET framework, this technology has since evolved and become a core component of the .NET ecosystem.

It’s extensively used for Microsoft products like Windows applications and web applications, as well as various software like Unity, which uses C# for game development.

In this quick guide, discover the features, ideal use cases for choosing between C, C++, and C#, and learn how to install and write your first C# program on Linux.

Tutorial Details

DescriptionC Sharp Programming Language
Difficulty LevelModerate
Root or Sudo PrivilegesYes (for installation)
OS CompatibilityUbuntu, Manjaro, Fedora, etc.
Prerequisites
Internet RequiredYes (for installation)

Features and Drawbacks of C#

While C# is considered a more advanced programming language compared to C/C++, mostly due to the following features offered by it:

  • User-friendly, modern syntax for easier coding.
  • Support object-oriented principles, enhancing code organization and reusability.
  • Automatic memory management (garbage collection) reduces memory errors.
  • Cross-platform with .NET Core and .NET 6/7, allowing code to run on Windows, Linux, and macOS.
  • Offers a long-range of standard library, saving development time with pre-built functions and classes.
  • Visual Studio is a robust IDE for efficient C# coding, debugging, and testing.
  • Features a strong type system that catches type-related errors during compilation.
  • Asynchronous programming, crucial for responsive and scalable applications.

Though C# offers numerous advantages, it’s important to be mindful of certain drawbacks. These include the learning curve associated with the language and performance inconsistencies when compared to C/C++, particularly in contexts involving low-level tasks or resource-intensive applications.

While C# is cross-platform, Windows-centric applications might still have platform dependencies due to the Microsoft ecosystem.

How to Install the .NET SDK for C# on Linux

To write and run C# programs on Linux, you can use the .NET SDK, which is a cross-platform, open-source framework developed by Microsoft.

While writing this article, there were two variants of the .NET SDK available: “6” and “7”. Although “7” is the latest, it is not fully enrolled in all Linux distributions.

Here are the steps to install the .NET (versions “6” or “7”) SDK on your Linux distributions:

Installing the .NET SDK using an Automated Script

Microsoft offers a .NET script for automating the installation of the .NET SDK and Runtime. Ensure wget is installed on your system, then run:

$ wget https://dot.net/v1/dotnet-install.sh -O dotnet-install.sh
$ chmod +x ./dotnet-install.sh
$ ./dotnet-install.sh --version latest

To install a specific major version, use the “--channel” flag followed by the version number; for instance, this command installs the .NET 7.0 SDK.

$ ./dotnet-install.sh --channel 7.0

Installing the .NET SDK on Ubuntu or Debian-based Distributions

Execute the appropriate command according to your Debian/Ubuntu version:

#.NET SDK 6 and 7 are available for Ubuntu 18.04+ and Debian 10+
$ sudo apt install dotnet-sdk-7.0

#.NET SDK 6 is available for Ubuntu 16.04
$ sudo apt install dotnet-sdk-6.0

Installing the .NET SDK on Red Hat or Fedora-based Distributions

Execute the appropriate command according to your Red Hat/Fedora version:

#.NET SDK 6 and 7 are available for RHEL 8+ and Fedora 37
$ sudo dnf install dotnet-sdk-7.0

#.NET SDK 6 is available for RHEL 7
$ sudo dnf install dotnet-sdk-6.0

Installing the .NET SDK on Arch or Manjaro-based Distributions

Execute the following command to install the latest .NET SDK on your Arch system:

$ sudo pacman -S dotnet-sdk-7.0

Installing the .NET SDK on OpenSUSE 15.x

Before you install .NET SDK, run the following command to add the Microsoft package signing key to your list of trusted keys and Microsoft package repository:

$ sudo zypper install libicu
$ sudo rpm --import https://packages.microsoft.com/keys/microsoft.asc
$ wget https://packages.microsoft.com/config/opensuse/15/prod.repo
$ sudo mv prod.repo /etc/zypp/repos.d/microsoft-prod.repo
$ sudo chown root:root /etc/zypp/repos.d/microsoft-prod.repo

Then run the following Zypper command to install the latest .NET SDK on your OpenSUSE system:

$ sudo zypper install dotnet-sdk-7.0

After installing the package, run a test program to ensure everything works perfectly.

Testing a “Hello, World!” Program

After installing the latest .NET 6.0 or 7.0 SDK on your Linux system, run your test program using the provided methods.

1. Open your terminal and create a project named “MyApp” to begin:

$ dotnet new console -o MyApp

Output:

Creating .NET project in Linux

2. Enter your project directory and list its content:

$ cd MyApp/
$ ls -l

Output:

Entering in .NET project directory and listing its content

3. In your project directory, among multiple files and directories, “Program.cs” serves as the main C# source file.

A single line of C# code is written on this source file while creating the project, which you can read using the cat command.

$ cat Program.cs

Output:

Reading the content of C# source file

4. To write your own C# code, clear the content of the “Program.cs” file and replace it with your own C# code using any text editor. Here is the simple C# program that you can use for now:

using System;

class Program
{
    static void Main()
    {
        Console.WriteLine("Follow, LinuxTLDR!");
    }
}

Save and close the file.

5. Now, run it with .NET runtime (be patient while performing as it compiles and then executes, which may take some time).

$ dotnet run

Output:

Running first C# program on Linux

6. Once your program is ready, you can build it.

$ dotnet build

Output:

building the C# project

If there are no warnings or errors, run your program binary from the specified location as highlighted above.

$ /home/linuxtldr/MyApp/bin/Debug/net7.0/MyApp

Output:

running the C# program binary file

Congratulations on successfully installing and running your very first C# program on Linux!

Final Word

If you have any questions or are experiencing any issues, please don’t hesitate to share them in the comment section. Your feedback is valuable to us and will help us address any concerns you may have.

Till then, peace!

Join The Conversation

Users are always welcome to leave comments about the articles, whether they are questions, comments, constructive criticism, old information, or notices of typos. Please keep in mind that all comments are moderated according to our comment policy.