How to Set Up C# for Development on Linux

Saad Aslam Feb 02, 2024
  1. What You Need to Run C# on Linux
  2. Install Visual Studio Code on Linux
  3. Download and Install the Visual Studio Code DEB Package on Linux
  4. Install the C# Extension for Visual Studio Code on Linux
  5. Install .NET SDK for Linux
  6. Create a C# Console Application With Visual Studio Code
How to Set Up C# for Development on Linux

If you’re interested in the methods for developing C# applications in Linux, this article is specifically for you.

The components involved are Visual Studio Code editor and the .NET Core SDK, which you will need to develop and run C# applications on Linux.

What You Need to Run C# on Linux

The Visual Studio Code editor and its C# extension provide a nice and pleasant C# development environment on Linux.

The .NET SDK (Software Development Kit) is a developer platform that includes libraries and tools. Also, a runtime environment for developing and running C# programs.

If you want to execute C# apps, you need the .NET runtime.

This article shall cover the whole process, from installing the Visual Studio Code editor and the .NET SDK to creating, building, running, and debugging a basic console application.

The only thing you must run and work on is a Linux system with a desktop environment; the system can be physical or virtual.

Install Visual Studio Code on Linux

The Visual Studio Code website gives you to choose from two different methods for the installation of Visual Studio Code.

The two methods are:

  1. You can install it as a Snap package through the Snap store.
  2. You may also use the package manager included with your Linux distribution.

Though whichever method you choose will be a viable option, as it depends on your personal preferences.

For this article, we shall use the option to use Linux’s distribution package manager; because Snap packages update on their own in the background, so that can be one reason to choose either one of the options.

You may now utilize a DEB package for Debian or Ubuntu or an RPM package for Fedora or openSUSE, depending on the version of Linux you’re using.

Download and Install the Visual Studio Code DEB Package on Linux

  • Visit the Visual Studio Code website.
  • Go to the Downloads page.
  • Click on the .deb to download the Visual Studio Code editor deb package.
  • Please navigate to the directory you saved it to when it is downloaded.
  • To install, first open up the terminal.
  • Run the following code.
sudo apt install -f ~/Downloads/filename.deb

Install the C# Extension for Visual Studio Code on Linux

As Visual Studio Code for Linux does not support C# by default, you’ll need an extension for it. You can get the extension from the Visual Studio Code marketplace.

The installation instructions will be written on the extension download page.

  • Open Visual Studio Code.
  • In it, press Ctrl+P.
  • Type ext install ms-dotnettools.csharp.
  • Press Enter.

Install .NET SDK for Linux

Now, you need the .NET SDK to use the tools, libraries, and runtime environment needed to develop and run C# applications. Before moving forward, we need to install some prerequisites.

First, install the apt-transport-https package. Use sudo apt install apt-transport-https to install the package.

Then install package repositories. Use sudo dpkg -i packages-microsoft-prod.deb to install the package.

Finally, run sudo apt update and sudo apt install dotnet-sdk-5.0.

Create a C# Console Application With Visual Studio Code

So, finally, we have installed the Visual Studio Code and .NET SDK on our Linux system; let us now create our first C# console application.

  1. Create a new C# project

    The first step is to create a new C# project. To do that, first start Visual Studio Code, and once it is open, from the program menu, select Terminal and New Terminal.

    Once the new terminal window is open, run the following commands to create a C# console application.

    mkdir MyApp
    cd MyApp
    dotnet new console
    

    Go to File > Open > MyApp from the program menu to open this newly created project.

  2. Build the C# application

    When the C# console application was created by the dotnet tool, it automatically added a Hello World type program in Program.cs, which is as follows.

    using System;
    
    namespace MyApp {
      class Program {
        static void Main(string[] args) {
          Console.WriteLine("Hello World!");
        }
      }
    }
    

    Let us use this as a starting point. First, configure the default build task from Program > Terminal > Configure Default Build Task > build. To build the C# application, go to Program > Terminal > Run Build Task.

  3. Run the C# application

    Now that you have successfully developed your first C# application try to run it. To run it, go to Program > Run > Run Without Debugging, or if you want to use a shortcut key, use Ctrl+F5.

    The output of the code will be displayed like below in the Debug Console window.

    Hello World!
    
  4. Debug the C# program

    Finally, we need to test the debugging function, for it is one of the most important functions for an integrated development environment.

    Go to Program > Run > Start Debugging or press F5 to debug the program.

Author: Saad Aslam
Saad Aslam avatar Saad Aslam avatar

I'm a Flutter application developer with 1 year of professional experience in the field. I've created applications for both, android and iOS using AWS and Firebase, as the backend. I've written articles relating to the theoretical and problem-solving aspects of C, C++, and C#. I'm currently enrolled in an undergraduate program for Information Technology.

LinkedIn