Installing and Running .NET 5 on CentOS 8

Simple and easy to follow instructions on how to install .NET 5 on CentOS 8 and running your first app.

First have a CentOS image that you have root privilege’s on. I’m going to assume that you don’t have .NET 5 already installed but if you want to check, run the below command.

[testhost@localhost ~]$ dotnet --info

If you don’t get anything back or you get “bash: dotnet: command not found…” then you don’t have .NET installed. If you do get something back check which version you are running.

First we need to add the Microsoft package repository. On your CentOS box run…

[testhost@localhost ~]$ sudo rpm -Uvh https://packages.microsoft.com/config/centos/8/packages-microsoft-prod.rpm

If the above link is no longer valid, I found it by googling “Microsoft package repository”

There are two different packages that you can install, the SDK or the Runtime. My Linux host is going to be a server setup so I will not be doing any development on it I only need it to be able to run .NET applications. If you only need to be able to run .NET applications then you only need the runtime. If you are going to be doing development then you will need the SDK. The SDK will include the runtime so you do not need to install both if you go that route.

Installing the Runtime. There are two different Runtimes provided, the ASP.NET Core Runtime or the .NET Runtime. The .NET runtime does not include support for ASP.NET core. My two cents here, .NET Core seems to be the way MS is going so I recommend installing the ASP.NET Core Runtime as you will be able to support both .NET core applications and .NET runtime applications.

CentOS 8 uses DNF for package management for RPM distributions instead of yum so lets get started.

testhost@localhost ~]$ sudo dnf install aspnetcore-runtime-5.0

You will get a few prompts, just type “y” and hit enter. They are asking if you are okay installing the necessary packages, and to import the GPG key used to sign the package.

packages-microsoft-com-prod                                                                                                                                                                                                                    1.3 MB/s | 907 kB     00:00
Dependencies resolved.
===============================================================================================================================================================================================================================================================================
 Package                                                                  Architecture                                            Version                                                   Repository                                                                    Size
===============================================================================================================================================================================================================================================================================
Installing:
 aspnetcore-runtime-5.0                                                   x86_64                                                  5.0.0-1                                                   packages-microsoft-com-prod                                                  7.9 M
Installing dependencies:
 dotnet-host                                                              x86_64                                                  5.0.0-1                                                   packages-microsoft-com-prod                                                   64 k
 dotnet-hostfxr-5.0                                                       x86_64                                                  5.0.0-1                                                   packages-microsoft-com-prod                                                  170 k
 dotnet-runtime-5.0                                                       x86_64                                                  5.0.0-1                                                   packages-microsoft-com-prod                                                   29 M
 dotnet-runtime-deps-5.0                                                  x86_64                                                  5.0.0-1                                                   packages-microsoft-com-prod                                                  2.8 k

Transaction Summary
===============================================================================================================================================================================================================================================================================
Install  5 Packages

Total download size: 37 M
Installed size: 90 M
Is this ok [y/N]: y
Downloading Packages:
(1/5): dotnet-host-5.0.0-x64.rpm                                                                                                                                                                                                               245 kB/s |  64 kB     00:00
(2/5): dotnet-hostfxr-5.0.0-x64.rpm                                                                                                                                                                                                            610 kB/s | 170 kB     00:00
(3/5): dotnet-runtime-deps-5.0.0-centos.7-x64.rpm                                                                                                                                                                                               22 kB/s | 2.8 kB     00:00
(4/5): aspnetcore-runtime-5.0.0-x64.rpm                                                                                                                                                                                                        965 kB/s | 7.9 MB     00:08
(5/5): dotnet-runtime-5.0.0-x64.rpm                                                                                                                                                                                                            1.5 MB/s |  29 MB     00:19
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Total                                                                                                                                                                                                                                          1.9 MB/s |  37 MB     00:19
warning: /var/cache/dnf/packages-microsoft-com-prod-af931794efe7845f/packages/aspnetcore-runtime-5.0.0-x64.rpm: Header V4 RSA/SHA256 Signature, key ID be1229cf: NOKEY
packages-microsoft-com-prod                                                                                                                                                                                                                    5.5 kB/s | 983  B     00:00
Importing GPG key 0xBE1229CF:
 Userid     : "Microsoft (Release signing) <gpgsecurity@microsoft.com>"
 Fingerprint: BC52 8686 B50D 79E3 39D3 721C EB3E 94AD BE12 29CF
 From       : https://packages.microsoft.com/keys/microsoft.asc
Is this ok [y/N]: y
Key imported successfully
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
  Preparing        :                                                                                                                                                                                                                                                       1/1
  Installing       : dotnet-runtime-deps-5.0-5.0.0-1.x86_64                                                                                                                                                                                                                1/5
  Installing       : dotnet-host-5.0.0-1.x86_64                                                                                                                                                                                                                            2/5
  Installing       : dotnet-hostfxr-5.0-5.0.0-1.x86_64                                                                                                                                                                                                                     3/5
  Installing       : dotnet-runtime-5.0-5.0.0-1.x86_64                                                                                                                                                                                                                     4/5
  Installing       : aspnetcore-runtime-5.0-5.0.0-1.x86_64                                                                                                                                                                                                                 5/5
  Running scriptlet: aspnetcore-runtime-5.0-5.0.0-1.x86_64                                                                                                                                                                                                                 5/5
  Verifying        : aspnetcore-runtime-5.0-5.0.0-1.x86_64                                                                                                                                                                                                                 1/5
  Verifying        : dotnet-host-5.0.0-1.x86_64                                                                                                                                                                                                                            2/5
  Verifying        : dotnet-hostfxr-5.0-5.0.0-1.x86_64                                                                                                                                                                                                                     3/5
  Verifying        : dotnet-runtime-5.0-5.0.0-1.x86_64                                                                                                                                                                                                                     4/5
  Verifying        : dotnet-runtime-deps-5.0-5.0.0-1.x86_64                                                                                                                                                                                                                5/5
Installed products updated.

Installed:
  aspnetcore-runtime-5.0-5.0.0-1.x86_64                    dotnet-host-5.0.0-1.x86_64                    dotnet-hostfxr-5.0-5.0.0-1.x86_64                    dotnet-runtime-5.0-5.0.0-1.x86_64                    dotnet-runtime-deps-5.0-5.0.0-1.x86_64

Complete!

That’s it for installing, now you can verify if its installed by re-running the “dotnet –info” command, you should get something that look like the print out below.

[testhost@localhost ~]$ dotnet --info

Host (useful for support):
  Version: 5.0.0
  Commit:  cf258a14b7

.NET SDKs installed:
  No SDKs were found.

.NET runtimes installed:
  Microsoft.AspNetCore.App 5.0.0 [/usr/share/dotnet/shared/Microsoft.AspNetCore.App]
  Microsoft.NETCore.App 5.0.0 [/usr/share/dotnet/shared/Microsoft.NETCore.App]

To install additional .NET runtimes or SDKs:
  https://aka.ms/dotnet-download

On to the more fun stuff, writing your first app in .NET 5 and running it on linux. If you are using Visual Studio you will need VS 2019 v 16.8 or higher. Go ahead and create a console app, or you can run the below command and a console app will be created for you.

dotnet new console -o LinuxTest

You can then open up the project in VS or just open up the Program.cs file in a text editor. I added the following code to mine. The Hello World is was already there, its part of the default template for console apps.

using System;

namespace LinuxTest
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");
            Console.WriteLine("Running .NET on Linux");
            Console.WriteLine("Time: " + DateTime.UtcNow.ToString());
            Console.ReadLine();
        }
    }
}


To test to make sure everything is working on your machine, on the console go to the directory that contains your new projects .csproj file and run “dotnet run”

e:\netcore5\LinuxTest>dotnet run
Hello World!
Running .NET on Linux
Time: 11/21/2020 4:40:03 AM

If that does not work for you, go to the directory that houses the .dll, usually “~\bin\Debug\net5.0” then in the cmd prompt run “dotnet [NameOfYourDLL].dll”

e:\netcore5\LinuxTest\bin\Debug\net5.0>dotnet LinuxTest.dll
Hello World!
Running .NET on Linux
Time: 11/21/2020 4:43:57 AM

Okay once you have a working app lets get it moved over to the CentOS host. For this I just use “scp” its easy and was already installed on my Windows 10 version. I just moved the whole project and its subfolders over as this was just a test.

From an admin command prompt in the directory containing the “LinuxTest” project, I ran the following command, don’t forget the -r so it reclusively copies all subfolders and items.

e:\netcore5\LinuxTest>scp -r * testhost@192.168.0.12:/home/testhost/dotnet/linuxtest

Okay back on the Linux box now lets check our directory to make sure that all the files are there.

[testhost@localhost linuxtest]$ ls
bin  LinuxTest.csproj  LinuxTest.sln  obj  Program.cs
[testhost@localhost linuxtest]$

Looks good. Lets run the app and see if we get the same results, with different timestamps of course since some time has passed since the run on windows. On the Windows machine we just used the “dotnet run” command, that wont work here that command only works on environments that have the SDK installed, so in this case we need to go to the .dll and run it directly.

[testhost@localhost linuxtest]$ cd bin/Debug/net5.0/
[testhost@localhost net5.0]$ ls
LinuxTest.deps.json  LinuxTest.dll  LinuxTest.exe  LinuxTest.pdb  LinuxTest.runtimeconfig.dev.json  LinuxTest.runtimeconfig.json  ref
 [testhost@localhost net5.0]$ dotnet LinuxTest.dll
Hello World!
Running .NET on Linux
Time: 11/21/2020 5:02:49 AM

So why would anyone want to run .NET on Linux? Well I’m not sure of other peoples reasons but I do know why I’m doing it. I’m building a custom app using Kestrel as the web sever to expose a REST Api. What is Kestrel? Well stay tuned as there will be another post on that. But being able to run .NET on Linux allows me to use C# to build my backend a language that I’m very familiar with (since I also use it in my day job) and in my opinion is very easy language to use. However, I don’t have to pay the expensive license fees that go with running and acitaving Windows Server.

Cheers!