Programming C# and dotnet on linux


Using VSCode with dotnet in Linux

Visual Studio is not available on Linux thus you need to install either mono or Dotnet and use Visual Studio Code with C# dev kit extension.


Installing dotnet runtime

You can decide if you want to install dotnet-runtime to program console projects with csharp, or install aspnetcore-runtime to program web applications or to install “Dotnet SDK” which includes all 3 runtimes:

  1. .NET Runtime 8.0.2
  2. ASP.NET Core Runtime 8.0.2
  3. .NET Desktop Runtime 8.0.2

In Linux land, Debian offers the most stability and reliablity which requires testing of packages which results in Debian packages being often old. If you are using Debian-based distributions such as Ubuntu, Pop!_OS, or Linux Mint, you may need to add additional repositories to access a wider range of software packages.


First add the repository and then you can choose a runtime to install:

wget https://packages.microsoft.com/config/debian/12/packages-microsoft-prod.deb -O packages-microsoft-prod.deb
sudo dpkg -i packages-microsoft-prod.deb
rm packages-microsoft-prod.deb

# To install the SDK
sudo apt-get update && sudo apt-get install -y dotnet-sdk-8.0

# To install the ASP.NET Core Runtime
sudo apt-get update && sudo apt-get install -y aspnetcore-runtime-8.0

# To install the .NET runtime
sudo apt-get install -y dotnet-runtime-8.0


I have Fedora 39 on my machine, and Fedora has most updated packages available. You can install dotnet runtimes via dnf repository.

In fedora:

# search for dotnet packages in dnf to find the
# latest versions available
dnf search dotnet

# dotnet-sdk-8.0.x86_64 : .NET 8.0 Software Development Kit

# To install the SDK
sudo dnf install dotnet-sdk-8.0.x86_64

# To install the ASP.NET Core Runtime
sudo dnf install aspnetcore-runtime-8.0

# To install the .NET runtime
sudo dnf install dotnet-runtime-8.0

After installing you can check the installation by printing the dotnet version or the list of installed dotnet runtimes:

# check the version of dotnet
dotnet --version

# list installed runtimes 
dotnet --list-runtimes

Configure VSCode

Install VSCode if it is not installed yet. Then click on “extensions”, search for and install both “C# dev kit” and “C#“.

vscode extensions C# dev kit and C#


Write your first CSharp console program

You can write console programs and run them inside VSCode’s terminal using “dotnet” commands.

  1. First, use “dotnet new console” to create new console project. All new projects include a “Program.cs”. This command will create a top level statement program.
  2. “Program.cs” file is where you write your code. By default, there is a line of code which outputs “Hello, World!“. You can edit it or just run the program.
  3. In terminal, cd into the project folder and run the program using “dotnet run” command:
# make a directory and cd into it
mkdir test_folder
cd test_folder

# create new console project
# -n stands for name
# -o stand for output directory
dotnet new console -n hello -o ./hello

# cd into project folder
cd ./hello

# run the program
dotnet run

how to run c# programs in console


dotnet default templates

dotnet new command offers prebuilt templates to start a project. To see a complete list of available templates open terminal and type:

dotnet new list

Create new project named “basics1” with top-level statements:

dotnet new console -o ./basics1

The command creates a project folder with “Program.cs” file that only has one line of code which prints “Hello, World!” in to the console. There is no namespace, class or method (function inside a class).

// See https://aka.ms/new-console-template for more information
Console.WriteLine("Hello, World!");

dotnet new console projects with top-level statements


Create new project with program main

For creating a new project with “program and main” use the option “—use-program-main” with “dotnet new” command.

dotnet new console --use-program-main -o ./basics1

Which creates the project folder and a "Program.cs" file which looks like this:
namespace basics2;

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

dotnet new console projects with program main

Create new solution file

A .sln file, short for “solution,” is a file used by Visual Studio to organize and manage projects within a software solution. It serves as a container for one or more related projects, which could include libraries, executables, or other types of code files. Unlike Visual Studio, In VSCode you can create solution file and project folder separately and then add or remove projects from solution.

If there are multiple version of dotnet installed, use -f to specify the version of dotnet to build your project.

// cd into directory you want to create solution then:
dotnet new sln -n MyApp

dotnet new console -f net8.0 --use-program-main -o ./MyApp
dotnet sln add ./MyApp
dotnet sln remove ./MyApp



Tags: