Click here to Skip to main content
15,867,895 members
Articles / Programming Languages / C#

.NET on Mac for the OS X n00b without Mono via Visual Studio Code

Rate me:
Please Sign up or sign in to vote.
4.79/5 (5 votes)
30 Apr 2015CPOL5 min read 37.2K   9   5
.NET on Mac for the OS X n00b without Mono via Visual Studio Code

Those who know me could have placed large sums of money on me being the last person in the world to get a Mac. I love C#, Visual Studio, even Windows 8, and in general most things that Microsoft puts out.

Before I lose half my audience: last week, I broke down and picked up a 13" MacBook Pro. I even brought it with me as my primary PC during this week's Microsoft Build conference.

Fortunately, today, thanks to some amazing work from the folks at Microsoft, I successfully brought over some C# love from the Windows world and successfully got .NET running on OS X without paying $1,000 for a Xamarin License.

I'll get to the step-by-step instructions for how to build .NET on OS X with the newly announced Visual Studio Code and a lot of command line foo. But first a little background.

Where Did Things Go So Terribly Wrong?

I've actually been using a Mac for the last three months at work. With extreme displeasure, I was given and expected to be productive with a Macbook Pro on a large 'all-JavaScript-all-the-time' project. Despite cursing OS X daily, nay hourly, I eventually grew to become marginally productive in it.

But when I moved on from my project and I needed to return the company MacBook, I wasn't quite ready to return to my Hackintosh ways on my current side project (a Siren of Shame Mac client). So to the astonishment of all who know me, I picked up a reasonably priced MacBook Pro.

But a funny thing happened. After using Yosemite for a while, I realized the new OS addressed several of my biggest complaints from the previous work-provided Mavericks OS (no OneNote, and crappy multi-monitor support). I also discovered BetterSnapTool which is a must-have if you're accustomed to Windows.

So when I needed a primary computer to take with me to the Microsoft Build, the MacBook was a natural choice for its fantastic form factor and superior battery life. And at this point, I have to admit this little thing is growing on me (I'm even writing this article on it now).

Visual Studio For Mac?!

So I may not do it myself any time soon, but I can see why someone might want to live on a Mac but still code Visual Studio and C#. And I guess Microsoft realizes that too! To date, there have been some options. Mono has always been one, although it's not always 100% compatible with the .NET Framework, and for a hefty price Xamarin can get you a nice Cocoa UI with C#.

But recently Microsoft announced that ASP.NET 5 would run natively on Linux and Mac (via kestrel). And then yesterday, Microsoft announced a new cross platform IDE known as "Visual Studio Code". Since I had the Mac and some time I figured I'd give running .NET natively without Mono using Visual Studio Code a try. Turns out it was super easy and only took about 15 minutes.

Touching Story Bro, Show Me The Code

To do it, first off download and install the Visual Studio Code IDE they announced today. Don't get your hopes up, it really doesn't hold a candle to Visual Studio or even Xamarin Studio for that matter just yet. The important thing is it's super fast and lightweight, it runs on Windows, Mac, and Linux, and presumably Microsoft is going to grow this thing into something awesome.

Image 1

Next, install the ".NET for Mac and Linux" otherwise known as the .NET Execution Environment (DNX). To do that, first install homebrew, which is like Chocolatey for PCs. It makes installing and uninstalling apps super easy. To get it, run the following from a terminal to:

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

Next download and install the .NET Verison Manager (DNVM) via Homebrew, which will automatically install DNX:

brew tap aspnet/dnx/
brew update
brew install dnvm

If that worked, you should be able to run DNVM by doing:

source dnvm.sh
dnvm

Which should get you something like this:

Image 2

Yeoman

In Visual Studio, when you File -> New Project, you get a ton of project types to choose from that start you down the path of success with a lot of boilerplate code. That doesn't happen on a Mac with Visual Studio Code (it doesn't even have a File -> New Project option), but fortunately the Yeoman project will accomplish the same thing.

To install Yeoman, first install node if you haven't already:

brew install node

Then install Yeoman.

npm install -g yo

Next, you need to install the .NET templates from the OmniSharp/generator-aspnet project. To do that:

npm install -g generator-aspnet

At this point, you're finally ready to do the equivalent of File -> New Project. Change directories to your dev directory (e.g. cd ~/dev) and then:

yo aspnet

It will guide you through a wizard and then generate you a hello world app.

Image 3

If you select a Console App, you can run it by simply doing:

dnx . run

And if you're lucky, you'll get something like this:

Image 4

At this point, if you open up Visual Studio Code, you're liable to get a lot of errors and a brief message like "There Are Unresolved Dependencies From '/Project.Json'. Please Execute the restore command." If this happens, run...

dnu restore

...to download the project's dependencies. And you should be good to go.

Modifying this process for an ASP.NET project is only slightly more involved as you need to set up kestrel.

Conclusion

If you find yourself with on OSX either because you love the ecosystem, because it was forced on you, or perhaps like me you find yourself maybe kinda liking it just a little, yet you also love the C# language and those awesome features you can't get anywhere else like LINQ, and async/await, then there's some good news. With a little extra work and thanks to some amazing work from Microsoft, it's now possible to have your cake and eat it too.

Please post in the comments or hit me up on twitter if you found this post useful or if, more likely, I got something wrong.

This article was originally posted at http://www.leerichardson.com/feeds/posts/default

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
United States United States
Lee is a Microsoft MVP and a prolific writer, speaker, and youtuber on .Net and open source topics. Lee is a Solution Samurai at InfernoRed ( http://infernoredtech.com). When not coding he enjoys running, mountain biking, smoking brisket, electronics, 3D printing, and woodworking. He is active on twitter where you can reach him @lprichar ( https://twitter.com/lprichar).

Comments and Discussions

 
QuestionVisual Basic too? Pin
johnywhy24-Sep-16 5:05
johnywhy24-Sep-16 5:05 
Exciting post!

- Has the recipe changed/improved in the year and a half since you wrote it?
- Can this recipe support Visual Basic on Mac?
- Is it true that the ".Net" your recipe provides is actually Mono? (another comment seems to suggest that). Or is it .Net Core? Or...?

thx!
Johny Why
Why not?


modified 24-Sep-16 11:20am.

SuggestionNeed a bunch of workarounds to get it working Pin
Ali BaderEddin12-Sep-15 16:50
Ali BaderEddin12-Sep-15 16:50 
QuestionMinor typo Pin
Member 1178669423-Jun-15 5:16
Member 1178669423-Jun-15 5:16 
BugDNVM in OSX = Mono Pin
smnbss+uk1-May-15 15:43
smnbss+uk1-May-15 15:43 
QuestionTop marks from me Pin
Dr Gadgit1-May-15 10:29
Dr Gadgit1-May-15 10:29 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.