Trying out .NET Core on Linux

Ever since the announcement of .NET Core I’ve been interesting in writing some C# code on my Linux desktop. Recently I found some time to actually try out Microsoft’s latest .NET Core release and actually get a program up and running. While there are plenty of guides out there I followed the official Microsoft Hello World tutorial here. I’ve included the steps I took and some other notes below mostly for my own reference later on.

First up you need to add the Microsoft key. I’m running Linux Mint which is a variant of Ubuntu 18.04 so I added the appropriate key via the following two commands:

wget -q
sudo dpkg -i packages-microsoft-prod.deb

With the key added I could now install .NET Core 2.1:

sudo add-apt-repository universe
sudo apt-get install apt-transport-https
sudo apt-get update
sudo apt-get install dotnet-sdk-2.1

That’s pretty much all it takes to actually get .NET Core on your system. To create a new console project you can simply run this command:

dotnet new console

When finished, this leaves you with a console based “Hello World!” application with the same name as the folder you’re currently in. From here you can build and run the project with:

dotnet build
dotnet run

If you want to add a Nuget package to your project you can do so by using the command:

dotnet add package {package name}

As an IDE on Linux I’m using Visual Studio Code which comes with some nice features for developing. Alternatively you could install something like MonoDevelop or even just use your favourite text editor.

Finally if you want to make a release version of the code that is self-contained (doesn’t require the end user to install any of the .NET dependencies themselves) you can use the publish command. As an example here is how to build it for 64-bit Windows 10 targeting .NET Core 2.1:

dotnet publish --configuration Release --framework netcoreapp2.1 --runtime win10-x64 --self-contained

All of the different runtime identifiers can be found on Microsoft’s website here.

This post originally appeared on my personal website here.

Be the first to comment

Leave a Reply

Your email address will not be published.