Features .NET on Linux Lead image: Lead Image © 36clicks, 123RF.com
Lead Image © 36clicks, 123RF.com
 

Installing .NET on Linux

Foreign Territory

By the time the annual 2016 Red Hat Summit was held at the end of June in San Francisco, the previously announced support for .NET applications on Red Hat Enterprise Linux had been implemented. We take a closer look at the implementation and how admins can create a .NET environment under Linux. By Thorsten Scherf

To understand .NET fully, it is a good idea to look at past events. Development on the "Next Generation Windows Service" (NGWS) during the 1990s included work on a framework that was officially released in 2002 as .NET 1.0. This framework came from Microsoft and consisted of multiple components, among them a Common Language Runtime (CLR) and a Common Language Interface (CLI). Programs written using .NET code were first compiled into an intermediate language known as Common Intermediate Language before the run-time environment compiled it into the machine language for the platform.

From the beginning, the framework was supposed to support several programming languages, primarily C# and Visual Basic. Starting with version 3.5 in 2007, Microsoft released parts of the framework for the first time under the fairly restrictive Microsoft Reference Source License. Microsoft changed course radically at the end of 2014 when it put much of the .NET source code on GitHub [1] with an MIT license. This happened about six months before .NET version 4.6 appeared. These events prepared the way for a larger community to be able to work on pushing development of the framework forward.

Pressure from Java

Release into the wild did not come as a complete surprise, because for quite some time, Microsoft had found itself under pressure from Java-based solutions that could run on a number of different platforms. Classic .NET applications, on the other hand, usually only ran under a Windows operating system. This was the state of affairs even though .NET implementations had earlier already been made available under an open source license. However, none of these implementations came with all of the functionality offered by the Microsoft-based .NET implementation. Probably the best known of the open source versions was the Mono project started by Ximian. Novell drove the Mono project forward until Ximian took it over, and Microsoft bought Ximian at the beginning of 2016.

The announcement of a partnership with the Linux distributor Red Hat came one year after Microsoft began offering the .NET framework under an MIT license. The partnership was formed in part because of the desire on the part of both of the companies to use the .NET framework for a completely supported solution for Red Hat Enterprise Linux.

The two companies announced during the Red Hat Summit that the solution had been implemented with .NET Core 1.0 [2]. The .NET core project is based on the .NET source code that had been released the previous year, and it emphasizes portability to other platforms. Modular design was a guiding theme during the development of this platform from the very beginning. As a result, .NET Core comprises multiple individual components that can be loaded dynamically as needed with the package manager NuGet [3]. Just as with .NET itself, .NET Core was released under the MIT license.

Now a corresponding RPM package for .NET Core comes as part of the Red Hat Software Collection. The package is fully supported by Red Hat and, according to the terms of the partnership agreements, by Microsoft, as well. The platforms specified for support include Red Hat Enterprise Linux 7 and OpenShift, which is a PaaS implementation from Red Hat.

The topic is sure to offer subject matter for some heated discussions. However, .NET Core is very useful and therefore not easily ignored. The trend that appears to have prevailed over the last few years is to avoid purely monolithic enterprise applications in favor of modular software architecture, referred to as microservices, which lets software architects use different platforms during the course of a single project. As a result, it is entirely possible that some parts of a stack will be written in Java, Python, or C, and other components will be based on C# and the .NET Core framework. Naturally, some of the individual components that are part of a larger cloud infrastructure can run in containers.

Red Hat makes things very easy for developers with the Container Development Kit (CDK) [4], which offers a cross-platform container environment, so .NET developers can also continue to develop under Windows.

The No-Cost Developer Program

Red Hat has a Developer Program [5] that offers free access to all of the developer tools. The Red Hat website provides interested users the option of obtaining a completely free subscription, a $0 subscription, for Red Hat Enterprise Linux. Once subscribed, you can register the Linux system in the Red Hat network, after which you have access to all of the developer software repositories. An ISO image for Red Hat Enterprise Linux 7 also is available for download. When you have successfully installed a system with the image, you can register it using the commands

# subscription-manager register
# subscription-manager attach --auto
# subscription-manager repos --enable=rhel-7-server-dotnet-rpms
# yum install scl-utils
# yum install rh-dotnetcore10
# scl enable rh-dotnetcore10 bash

and then subsequently download the .NET Core software.

You should make sure that the .NET framework is made available via the software collections and that it can be activated. The dotnet tool helps you determine whether the installation has been successful (Listing 1).

Listing 1: dotnet --info

# dotnet --info
.NET Command Line Tools (1.0.0-preview2-003121)
Product Information:
  Version: 1.0.0-preview2-003121
  Commit SHA-1 hash: 1e9d529bc5
Runtime Environment:
  OS Name: rhel
  OS Version: 7.2
  OS Platform: Linux
  RID: rhel.7.2-x64

As ever, the classic "Hello World" serves as the starting point in C#. Three commands at the beginning suffice to set up the program successfully, resolve dependencies, and compile. These commands include new, restore, and run. First, you need to create the project:

# mkdir hwapp && cd hwapp
# dotnet new

The framework then creates the two files Program.cs and project.json. The "Hello World" code is contained in the first file (Listing 2).

Listing 2: Hello World in C#

using System;
namespace ConsoleApplication
{
  public class Program
  {
    public static void Main(string[] args)
    {
      Console.WriteLine("Hello World!");
    }
  }
}

Afterward, the following commands make sure that the dependencies stated in the project.json file are resolved by the NuGet package manager. You won't be able to compile and execute the program before these dependencies are resolved:

# dotnet restore
# dotnet run
Hello World!

If you want to reproduce this sample project with the Fedora community distribution instead of Red Hat Enterprise Linux, it is very easy to load the necessary software packages for the .NET framework onto the local system:

# sudo yum install libunwind libicu
# curl -sSL -o dotnet.tar.gz https://go.microsoft.com/fwlink/?LinkID=816869
# sudo mkdir -p /opt/dotnet && sudo tar zxf dotnet.tar.gz -C /opt/dotnet
# sudo ln -s /opt/dotnet/dotnet /usr/local/bin

Conclusion

Pairing the release of .NET Core for Red Hat Enterprise Linux with the distributor's Developer Program makes it very simple to develop and port .NET applications under Linux. These applications provide developers a larger selection of platforms that can be used for the development of microservices.