Chapter 1. Using .NET Core 2.1 on Red Hat Enterprise Linux
This Getting Started Guide describes how to install .NET Core 2.1 on Red Hat Enterprise Linux (RHEL). See Red Hat Enterprise Linux documentation for more information about RHEL 7.
1.1. Install and Register Red Hat Enterprise Linux
Install RHEL 7 using one of the following images:
- Red Hat Enterprise Linux 7 Server
- Red Hat Enterprise Linux 7 Workstation
Red Hat Enterprise Linux for Scientific Computing
See the Red Hat Enterprise Linux Installation Guide for details on how to install RHEL.
See Red Hat Enterprise Linux Product Documentation page for available RHEL versions.
Use the following command to register the system.
$ sudo subscription-manager register
You can also register the system by following the appropriate steps in Registering and Unregistering a System in the Red Hat Subscription Management document.
Display a list of all subscriptions that are available for your system and identify the pool ID for the subscription.
$ sudo subscription-manager list --available
This command displays the subscription name, unique identifier, expiration date, and other details related to it. The pool ID is listed on a line beginning with Pool ID.
Attach the subscription that provides access to the
dotNET on RHELrepository. Use the pool ID you identified in the previous step.$ sudo subscription-manager attach --pool=<appropriate pool ID from the subscription>
Enable the .NET Core channel for Red Hat Enterprise 7 Server, Red Hat Enterprise 7 Workstation, or HPC Compute Node with one of the following commands, respectively.
$ sudo subscription-manager repos --enable=rhel-7-server-dotnet-rpms $ sudo subscription-manager repos --enable=rhel-7-workstation-dotnet-rpms $ sudo subscription-manager repos --enable=rhel-7-hpc-node-dotnet-rpms
Verify the list of subscriptions attached to your system.
$ sudo subscription-manager list --consumed
Install the
scltool.$ sudo yum install scl-utils
1.2. Install .NET Core
Install .NET Core 2.1 and all of its dependencies.
$ sudo yum install rh-dotnet21 -y
Enable the
rh-dotnet21Software Collection environment so you can rundotnetcommands in the bash shellThis procedure installs the .NET Core 2.1 runtime with the latest 2.1 SDK. When a newer SDK becomes available, it automatically installs as a package update.
$ scl enable rh-dotnet21 bash
This command does not persist; it creates a new shell, and the
dotnetcommand is only available within that shell. If you log out, use another shell, or open up a new terminal, thedotnetcommand is no longer enabled.WarningRed Hat does not recommend permanently enabling
rh-dotnet21because it may affect other programs. For example,rh-dotnet21includes a version oflibcurlthat differs from the base RHEL version. This may lead to issues in programs that do not expect a different version oflibcurl. If you want to enablerh-dotnetpermanently, add the following line to your~/.bashrcfile.source scl_source enable rh-dotnet21Run the following command to verify the installation succeeded.
$ dotnet --info .NET Core SDK (reflecting any global.json): Version: 2.1.300 Commit: ded465c666 Runtime Environment: OS Name: rhel OS Version: 7 OS Platform: Linux RID: rhel.7-x64 Base Path: /opt/rh/rh-dotnet21/root/usr/lib64/dotnet/sdk/2.1.300/ Host (useful for support): Version: 2.1.0 Commit: N/A .NET Core SDKs installed: 2.1.300 [/opt/rh/rh-dotnet21/root/usr/lib64/dotnet/sdk] .... omitted
1.3. Create an Application
Create a new Console application in a directory called
hello-world.$ dotnet new console -o hello-world The template "Console Application" was created successfully. Processing post-creation actions... Running 'dotnet restore' on hello-world/hello-world.csproj... Restoring packages for /home/<USER>/hello-world/hello-world.csproj... Generating MSBuild file /home/<USER>/hello-world/obj/hello-world.csproj.nuget.g.props. Generating MSBuild file /home/<USER>/hello-world/obj/hello-world.csproj.nuget.g.targets. Restore completed in 224.85 ms for /home/<USER>/hello-world/hello-world.csproj. Restore succeeded.
Run the project.
$ cd hello-world $ dotnet run Hello World!
1.4. Publish Applications
The .NET Core 2.1 applications can be published to use a shared system-wide version of .NET Core or to include .NET Core. These two deployment types are called framework-dependent deployment (FDD) and self-contained deployment (SCD), respectively.
For RHEL, we recommend publishing by FDD. This method ensures the application is using an up-to-date version of .NET Core, built by Red Hat, that includes a specific set of native dependencies. These native libraries are part of the rh-dotnet21 Software Collection. On the other hand, SCD uses a runtime built by Microsoft. Running applications outside the rh-dotnet21 Software Collection may cause issues due to the unavailability of native libraries.
1.4.1. Publish .NET Core Applications
Use the following command to publish a framework-dependent application.
$ dotnet publish -f netcoreapp2.1 -c Release
Optional: If the application is only for RHEL, trim out the dependencies needed for other platforms with these commands.
$ dotnet restore -r rhel.7-x64 $ dotnet publish -f netcoreapp2.1 -c Release -r rhel.7-x64 --self-contained false
Enable the Software Collection and pass the application assembly name to the
dotnetcommand to run the application on a RHEL system.$ scl enable rh-dotnet21 -- dotnet <app>.dll
This command can be added to a script that is published with the application. Add the following script to your project and update the
ASSEMBLYvariable.#!/bin/bash ASSEMBLY=<app>.dll SCL=rh-dotnet21 DIR="$(dirname "$(readlink -f "$0")")" scl enable $SCL -- dotnet "$DIR/$ASSEMBLY" "$@"
To include the script when publishing, add this ItemGroup to the
csprojfile.<ItemGroup> <None Update="<scriptname>" Condition="'$(RuntimeIdentifier)' == 'rhel.7-x64' and '$(SelfContained)' == 'false'" CopyToPublishDirectory="PreserveNewest" /> </ItemGroup>
1.4.2. Publish ASP.NET Core Applications
When using the Microsoft SDK, ASP.NET Core 2.1 web applications are published with a dependency on the ASP.NET Core shared framework. This is a set of packages that are expected to be available on the runtime system.
When publishing on RHEL, these packages are included with the application. To include the packages using the Microsoft SDK, the MicrosoftNETPlatformLibrary property must be set to Microsoft.NETCore.App in the project file as shown below.
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>netcoreapp2.1</TargetFramework>
<MicrosoftNETPlatformLibrary>Microsoft.NETCore.App</MicrosoftNETPlatformLibrary>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.App" Version="2.1" />
</ItemGroup>
</Project>As an alternative, this property can be set when publishing the application.
$ dotnet publish -f netcoreapp2.1 -c Release -r rhel.7-x64 --self-contained false /p:MicrosoftNETPlatformLibrary=Microsoft.NETCore.App
1.5. Run Applications on Docker
This section shows how to use the dotnet/dotnet-21-runtime-rhel7 image to run a precompiled application inside a Docker container.
Create a new mvc project in a directory named
mvc_runtime_example.$ dotnet new mvc -o mvc_runtime_example --no-restore $ cd mvc_runtime_example
Restore and publish the project.
$ dotnet restore -r rhel.7-x64 $ dotnet publish -f netcoreapp2.1 -c Release -r rhel.7-x64 --self-contained false /p:MicrosoftNETPlatformLibrary=Microsoft.NETCore.App
Create the
Dockerfile.$ cat > Dockerfile <<EOF FROM registry.access.redhat.com/dotnet/dotnet-21-runtime-rhel7 ADD bin/Release/netcoreapp2.1/rhel.7-x64/publish/ . CMD ["dotnet", "mvc_runtime_example.dll"] EOF
Build your image.
$ docker build -t dotnet-21-runtime-example .
Run your image.
$ docker run -d -p8080:8080 dotnet-21-runtime-example
- View the result in a browser: http://127.0.0.1:8080.

Where did the comment section go?
Red Hat's documentation publication system recently went through an upgrade to enable speedier, more mobile-friendly content. We decided to re-evaluate our commenting platform to ensure that it meets your expectations and serves as an optimal feedback mechanism. During this redesign, we invite your input on providing feedback on Red Hat documentation via the discussion platform.