Setting up .NET development environment on OSX

Yes we have lived to day where, this week at BUILD 2015 conference, Microsoft made few interesting announcements related to cross platform where they released a preview distribution of “.NET Core” runtime on Linux/Mac and a cross platform code editor “Visual Studio Code”. How cool is that!

In this post, I am going to setup .NET development environment on Mac. Note that I am after plain .NET core and not ASP.NET.

So let’s get started.

Prerequisite
You would need HomeBrew package manager to install .NET version manager, so if you don’t have it already installed, you can paste following script on terminal to install. Further details at

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

Ref screenshot

.NET Version Manager (DNVM)
To get started, we need to setup DNVM using HomeBrew. All you need to do is to install following commands in sequence.

brew tap aspnet/dnx
brew update
brew install dnvm

Ref screenshot

Like suggested, register the command

source dnvm.sh

.NET Core DNX SDK

At this time not all tools are using .NET Core, some of them are still using Mono. Therefore we need to acquire both DNX, Mono and CoreClr. As its mentioned in documentation, mono is the default DNX, so we can acquire it by calling

dnvm upgrade -u 

Next, get the .NET Core DNX

dnvm install latest -r coreclr -u

Ref screenshot

If we now call dnvm list command, we can see the installed DNX.

Visual Studio Code
We will leverage the latest code editor announced by Microsoft. You can use any other editor you like but I am going to use VSCode for this post.

  • Install the Visual Studio Code for OSX.
  • Create a new folder in Finder at your preferred location say “HelloWorldApp”.
  • Run VSCode and open that folder inside editor

project.json

Create a new file project.json to specify dependencies/frameworks.

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

Now it’s time to write code. Create a new .cs file say Main.cs and start writing C# code, you would notice you are getting intellisense, auto completion for even things like snippets but I will keep VSCode features for another blog post.

dnenv_mac_vscode_intellisense_1

dnenv_mac_vscode_intellisense_2

Run it
When you are done writing code, go back to terminal and download packages based on project.json. However one thing to highlight here is that restoring packages tool works on mono and has not moved to .net core. So in order to download packages you need to specify that you want to use mono dnx and then run the restore command.

Mac:HelloWorldApp adilmughal$ dnvm use 1.0.0-beta5-11682 -r mono
Mac:HelloWorldApp adilmughal$ dnu restore

Once you have restored the packages, run the app by switching dnx to coreclr and using run command

Mac:HelloWorldApp adilmughal$ dnvm use 1.0.0-beta5-11682 -r coreclr
Mac:HelloWorldApp adilmughal$ dnx . run
Hello .NET on Mac
For Adil's weblog

Congratulations, you just run C# code compiled by Roslyn compiler, executed by .NET Core CLR on OSX.

UPDATE: coreclr version 1.0.0-beta5-11682 has a bug where when you try to run the code, roslyn throws exception. This issue has been reported on github, as a workaround you can download and use version 1.0.0-beta5-11657 which works fine!

dnvm install 1.0.0-beta5-11657 -r coreclr -u
dnx . run

Hope this helps!