.NET Core: Cross Platform – Ubuntu Linux

In the previous posts we briefly described how .NET Core is making .NET cross platform available for Windows, OSX and now Linux!

If you search a little bit you’ll find different instructions to install .NET Core tooling (DNVM, DNX and DNU) on Ubuntu Linux. In this blog post you’ll find the bare minimum steps to get a Hello World sample running in Ubuntu 14.04.

For instance, the docs specify a Mono dependency to make DNU work on Linux. Apparently, this is no longer required 🙂

Remember, the .NET team is currently working on it and the CoreCLR is work in progress and open source!

.NET Version Manager (DNVM)

The .NET Version Manager helps retrieving versions of the DNX, using NuGet, and allowing you to switch between versions when you have multiple on your machine. DNVM is simply a way to manage and download NuGet packages and it is a set of command line utilities to update and configure which .NET Runtime to use.

We’ll use a little script to download and install DNVM in the machine. This script uses curl and unzip which can be installed with the following command:

sudo apt-get install unzip curl

 

After installing the prerequisites we are ready to download and install DNVM:

curl -sSL https://raw.githubusercontent.com/aspnet/Home/dev/dnvminstall.sh | DNX_BRANCH=dev sh && source ~/.dnx/dnvm/dnvm.sh

 
DNVM_install
After the script is executed you should be ready to go and install DNX. In my case, I had to execute one additional command to source the dnvm.sh and make it available to the console. Helpfully, this hint was provided by the script itself:

Type 'source /home/user/.dnx/dnvm/dnvm.sh' to start using dnvm

 

DNVM

Once this step is complete you should be able to run DNVM and see some help text.

Again, as I said in the OS X post, from this point this post is mostly a copy of my previous .NET Core: Cross platform – Windows post. Which is nice 🙂

 

.NET Execution Environment (DNX)

The .NET Execution Environment (DNX) contains the code required to bootstrap and run an application. This includes things like the compilation system, SDK tools, and the native CLR hosts.

DNX provides a consistent development and execution environment across multiple platforms (Windows, Mac and Linux) and across different .NET flavors (.NET Framework, .NET Core and Mono).

It is easy to install the .NET Core version of DNX, using the DNVM install command:

dnvm install -r coreclr latest

 

You can then use dnvm to list and select the active DNX version in your system (In my case, the latest version of DNX CoreCLR is 1.0.0-beta8)

dnvm use 1.0.0-beta8 -r coreclr

dnvm list

 
DNVM_list
 

Important: This is not strictly required at this point but before you can DNX run your application you must install some general purpose dependencies. You can install those using the following command:

sudo apt-get install libunwind8 libssl-dev

 

Hello world

A DNX project is simply a folder with a project.json file. The name of the project is the folder name.

Let’s first create a folder, set it as our current directory in command line:

mkdir HelloWorld && cd HelloWorld

 

Now create a new C# file HelloWorld.cs, and paste in the code below:

using System;

public class Program {
    public static void Main(string[] args){
        Console.WriteLine("Hello World from Core CLR!");
        Console.Read();
    }
}

 

Next, we need to provide the project settings DNX will use. Create a new project.json file in the same folder, and edit it to match the listing shown here:

{
    "version": "1.0.0-*",
    "dependencies": {
    },
    "frameworks" : {
        "dnx451" : { },
        "dnxcore50" : {
            "dependencies": {
                "System.Console": "4.0.0-beta-*"
            }
        }
    }
}

 

.NET Development Utility (DNU)

DNU is a command line tool that helps with the development of applications using DNX. You can use DNU to build, package and publish DNX projects. Or, as in the following example, you can use DNU to install a new package into an existing project or to restore all package dependencies for an existing project.

 

Important: The DNU utility has some dependencies on itself. Before you can dnu restore a project packages you must install the dependency using the following command:

sudo apt-get install libcurl3-dev

 

The project.json file defines the app dependencies and target frameworks in addition to various metadata properties about the app. See Working with DNX Projects for more details.

Because the .NET Core is completely factored we need to explicitly pull those libraries that our project depends on. We’ll  run the following command to download and install all packages that are listed in the project.json:

dnu restore

 

You’ll notice that even though our project only required System.Console, several dependent libraries have been downloaded and installed as a result.

 
DNU_restore
 

DNX run the app

At this point, we’re ready to run the app. You can do this by simply entering dnx run from the command prompt. You should see a result like this one:

dnx run

 
DNX_run
 

Further reading

https://dotnet.github.io/core/getting-started/

http://docs.asp.net/en/latest/getting-started/installing-on-linux.html#installing-on-debian-ubuntu-and-derivatives

http://www.mono-project.com/docs/getting-started/install/linux/#debian-ubuntu-and-derivatives

Intro to .NET Core

.NET Core: Cross Platform – Windows

.NET Core: Cross Platform – OS X

 

Summary

IHaveNoIdeaWhatImDoing



Leave a Reply