Click here to Skip to main content
15,115,261 members
Articles / Programming Languages / C#
Technical Blog
Posted 30 Apr 2015

Tagged as

Stats

34.4K views
9 bookmarked

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

Share

About the Author

Lee P Richardson
Web Developer
United States United States
Lee is a prolific writer, speaker, and video producer on .Net and open source topics. He has published over 100 posts to his personal blog (https://www.leerichardson.com) that have received more than half a million views since 2007. His "Code Hour" YouTube channel (https://youtube.com/leerichardson200) has attracted nearly 1,000 subscribers who have collectively consumed over 5,900 hours of his content. StackOverflow ranks him as a top 2% contributor. He has published 25 articles to CodeProject with an average article rating of 4.96/5. Throughout his 20 year software development consulting career in the DC area he has spoken scores of times at code camps, conferences, and user groups. He created the Siren of Shame (https://sirenofshame.com), and is a Solution Samurai at InfernoRed (http://infernoredtech.com). 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 6:05
Memberjohnywhy24-Sep-16 6:05 
SuggestionNeed a bunch of workarounds to get it working Pin
Ali BaderEddin12-Sep-15 17:50
MemberAli BaderEddin12-Sep-15 17:50 
Good article Lee!

I had to apply the following workarounds to get things to work smoothly for me after creating the project from the scaffold.

NPM throw Error without Sudo[^]

DNVM and Homebrew[^]

Followed by:

dnvm upgrade -r coreclr
dnu restore
dnx run

-- Ali B

http://mycodelog.com

QuestionMinor typo Pin
Member 1178669423-Jun-15 6:16
MemberMember 1178669423-Jun-15 6:16 
BugDNVM in OSX = Mono Pin
smnbss+uk1-May-15 16:43
Membersmnbss+uk1-May-15 16:43 
QuestionTop marks from me Pin
Dr Gadgit1-May-15 11:29
MemberDr Gadgit1-May-15 11: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.