C# - How to create, build and run a "hello world" console app on Linux

This post describes how to create a simple Console application and run it on Linux, for this we will be using WSL (Windows Subsystem for Linux) running Ubuntu 22.

Installing dotnet

We start by installing dotnet:

sudo apt-get update && \
  sudo apt-get install -y dotnet-sdk-6.0

For Ubuntu 22 you can install dotnet 6 with the above two commands, if you are on a different distro, follow this guide from Microsoft.

Creating a console app

Next up we will create a simple console application that writes "Hello World" to the console. Luckily this is the default implementation when you create a new console application in .Net:

cd /home/peterdrasmussen/
mkdir test
dotnet new console

If we use ls we see the following files:

Program.cs  obj  test.csproj

If we cat the Program.cs file we see the following:

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

Which is simply an application that logs "Hello World" to the console, exactly what we need! Next up we will build and run this.

Building and running the application

In order to build the application we need to run dotnet build and if we use the ls command afterwards, we will see that there is now a bin folder:

Program.cs  bin  obj  test.csproj

We have successfully built our application and we can now run it using the dotnet command on the dll in the bin folder:

cd bin/Debug/net6.0/
ls
ref  test  test.deps.json  test.dll  test.pdb  test.runtimeconfig.json
dotnet test.dll
Hello, World!

In the above we go to the directory where the output from the build is (bin/Debug/net6.0/) and use ls once again to see the contents. We then start the application using the dotnet command and it outputs the string "Hello, World!" to the console, that completes the goal of this blog post!

That is all

I hope you enjoyed this short post on how to write a simple "Hello world" .Net application on Linux, I wanted to write a simple and straightforward guide from start to finish. As always: feel free to leave a comment down below!