.NET Core: Cross Platform – OS X

.NET Core is making .NET cross platform an available for Windows, Ubuntu and OSX with a little help of our friends: DNVM, DNU and DNX.

The .NET Execution Environment (DNX) is a software development kit (SDK) and runtime environment that has everything you need to build and run .NET applications for Windows, Mac and Linux.

.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.

While it is pretty easy to install DNVM in a Windows box, on OSX the easiest way to get DNVM is to use Homebrew.

Homebrew: Bootstrapping the Bootstrapper

Package managers are the key component that have completely changed the face of modern software development. Homebrew is an OSX package manager which makes easy to install, upgrade and remove software packages.

In a nutshell, it uses GitHub repositories to download software (such as scripts), stores them in its own location and the creates symlinks providing easy access to them.

 

If you don’t have Homebrew installed then follow the Homebrew installation instructions. If you have the Developer tools installed you can use the following script in a terminal window:

ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

 

Once you have Homebrew up and running you can use the following commands to install DNVM:

brew tap aspnet/dnx
brew update
brew install dnvm

source dnvm.sh

 

Under the hood, Homebrew cloned the aspnet/homebrew-dnx  repository and installed dnvm using the dnvm.rb script. The dnvm.sh is a bag of functions that need to be sourced, hence the last command executed. Also, it is recommended to check and upgrade to the latest version of DNVM using the following command:

dnvm upgrade

 

DNVM

 

.NET Execution Environment (DNX)

From this point, this post is mostly a copy of my previous .NET Core: Cross platform – Windows post. Which is nice 🙂

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_use_list

 

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.

DNU

Important: If you are not seeing the output above you might be facing an issue with your OSX installation. By the time I’m writing this, there is a know issue regarding a missing dependency on ICU4C. In my case, I fixed it by brewing the dependency and everything worked like a charm again.

brew install icu4c

 

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 dependant 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://brew.sh/

https://github.com/aspnet/homebrew-dnx

https://github.com/aspnet/dnx/issues/2875

Intro to .NET Core

.NET Core: Cross Platform – Windows

 

Summary

WhatIfIToldYouOSX



3 Comments

Leave a Reply