A First Look at Installing ASP.NET 5, Part 2: OS X

This is part 2 of a 3 part series exploring ASP.NET 5 running on Windows, Mac OSX and Linux.

Having tested ASP.NET 5 via Visual Studio and a command line on Windows in our last post, it was time to give it a try on OS X. I used my trusty Macbook Pro running OS X Yosemite v 10.0.3.

Since I have heavily abused my Mac, after going through the installation process I spun up an OS X VM with Parallels so I could repeat it on a clean system just in case my existing configuration somehow affected the process. I only noticed a couple of minor differences which I’ll make a note of below.

To install and run ASP.NET 5 on OS X, I followed the instructions at:   http://docs.asp.net/en/latest/getting-started/installing-on-mac.html

I already had X Code and its command line utilities, Homebrew, and Node JS and NPM installed on my Mac. I went ahead and installed the latest versions of all of these tools on my OS X VM as well.

From the terminal:

  1. brew tap aspnet/dnx  (no errors)
  2. brew install dnvm  (no errors)
  3. source dnvm.sh  (no errors)
  4. dnvm upgrade  (get the latest version – no errors)

Note: add source dnvm.sh  to ~/.bash_profile, ~/.bashrc or ~/.zshrc to avoid having to run ‘ source dnvm.sh’ again.

Screen Shot 2015-06-05 at 1.47.10 PM

If DNVM is correctly installed, you should see something like this when you enter dnvm  in the terminal:

Screen Shot 2015-06-05 at 1.50.01 PM

Next, I followed the steps in Microsoft’s “Your First ASP.NET 5 Application on a Mac” tutorial to set up my basic “Hello World” style solution.

First, I confirmed I had beta 4 version of DNX by running dnvm list  :

Screen Shot 2015-06-09 at 12.45.10 PM
1.0.0-beta4 is the current-somewhat-stable release as of this post
At this point, I diverged from the Microsoft tutorial. They provide sample ASP.NET apps you can clone and try out   but if you want to build your own ASP.NET from scratch it seems you’re kind of out of luck without visual studio.

In the tutorial, they have some placeholder text that says “Coming soon: instructions for getting started with Yeoman.” Then they tell you to clone and try out some of their sample apps at https://github.com/aspnet/home. Intrigued, I read up on Yeoman and decided to give it a try.

On their website, Yeoman is described as follows:

Yeoman helps you to kickstart new projects, prescribing best practices and tools to help you stay productive.

To do so, we provide a generator ecosystem. A generator is basically a plugin that can be run with the yo command to scaffold complete projects or useful parts.

To install Yeoman, you’ll need to install Nodejs and NPM first.

Next, install Yeoman and their asp.net generator:  npm install -g yo generator-aspnet

When I tried this command on my Mac, I received a series of errors: 

Screen Shot 2015-06-05 at 2.07.59 PM

Trying it again with sudo resolved the problem: sudo npm install -g yo generator-aspnet

Screen Shot 2015-06-05 at 2.08.19 PM

SIDEBAR: When I repeated this step on my OS X VM, I was able to run npm install -g yo generator-aspnet  without using sudo . I assume this means when you log into an OS X VM on Parallels you have root access. I’m not sure how this works so if someone is more familiar with this topic please feel free to leave a comment.

Once Yeoman was installed, I used it to scaffold out an ASP.NET 5 app with the ridiculously simple yo aspnet .

This brings up Yeoman’s asp.net generator, a command line tool that provides a list of project templates to choose from. I selected web application.

Screen Shot 2015-06-05 at 2.16.12 PM
Fun Fact: The terminal command ‘yo’ is worth less money than the app Yo, despite being far more useful.
Next I gave my app a name when prompted and hit enter to create the scaffolding, then watched as it created all the necessary files:

Screen Shot 2015-06-05 at 2.23.23 PM

A quick glance in Finder confirmed the new project files were all there, as expected.

Screen Shot 2015-06-05 at 2.26.34 PM

It’s beyond the scope of this post, but for future reference Yeoman’s asp generator provides command line options to add new components, such as models, controllers, etc.

Although originally designed for *nix systems, Yeoman now supports Windows as well, so if you want to use a command line on a Windows box to scaffold new ASP.NET apps you can do so. See here and here for more.

Next, I downloaded and installed Visual Studio Code. I noted that in contrast to the massive full version of Visual Studio, VS code was only a measly 64.7 MB download.

Installing VS Code was as simple as unzipping the downloaded file and dragged the VS Code icon to the applications folder. I then opened VS code and marveled at it’s minimalist interface – so far so good.

I opened the project folder I’d just built with Yeoman and got a message about missing dependencies with a prompt to restore:

Screen Shot 2015-06-18 at 9.51.57 AM

Hitting the ‘Restore’ button next to the message automatically downloaded and tried to install all dependencies from Terminal (as if I’d run dnu restore ).

Screen Shot 2015-06-18 at 9.53.57 AM

But I got some more errors and again the suggestion I try as root: 

Screen Shot 2015-06-18 at 9.54.08 AM

So I tried running sudo dnu restore  from terminal myself and got some more errors: 

System.ComponentModel.Win32Exception: ApplicationName='bower', CommandLine='install', CurrentDirectory='/Users/brianpalladino/Projects/MacAspNet5App', Native error= Cannot find the specified file    at System.Diagnostics.Process.Startnoshell (System.Diagnostics.ProcessStartInfo startInfo, System.Diagnostics.Process process) [0x00000] in :0     at System.Diagnostics.Process.Startcommon (System.Diagnostics.ProcessStartInfo startInfo, System.Diagnostics.Process process) [0x00000] in :0     at System.Diagnostics.Process.Start (System.Diagnostics.ProcessStartInfo startInfo) [0x00000] in :0     at Microsoft.Framework.PackageManager.ScriptExecutor.Execute (Microsoft.Framework.Runtime.Project project, System.String scriptName, System.Func2 getVariable) [0x00000] in <filename unknown>:0     at Microsoft.Framework.PackageManager.RestoreCommand+<RestoreForProject>d__74.MoveNext () [0x00000] in <filename unknown>:0   --- End of stack trace from previous location where exception was thrown ---    at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x00000] in <filename unknown>:0     at System.Runtime.CompilerServices.TaskAwaiter1[System.Boolean].GetResult () [0x00000] in :0     at Microsoft.Framework.PackageManager.RestoreCommand+<>cDisplayClass730+<b0>d.MoveNext () [0x00000] in :0   --- End of stack trace from previous location where exception was thrown ---    at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x00000] in :0     at System.Runtime.CompilerServices.TaskAwaiter.GetResult () [0x00000] in :0     at Microsoft.Framework.PackageManager.RestoreCommand+d_73.MoveNext () [0x00000] in :0   ----------  Restore failed  ApplicationName='bower', CommandLine='install', CurrentDirectory='/Users/brianpalladino/Projects/MacAspNet5App', Native error= Cannot find the specified file

Fortunately, the omniscient demi-God Stack Overflow provided a resolution:

sudo npm install -g bower  sudo npm install -g grunt-cli

I ran these commands and then ran ‘ dnu restore ‘ again. That worked – no more errors!

All hails Stack Overflow!
All hail Stack Overflow!
Then I tried dnx .kestrel to start the server, but got some more errors:

Missing method .ctor in assembly /Users/brianpalladino/.dnx/packages/Microsoft.AspNet.Cryptography.Internal/1.0.0-beta4/lib/dnx451/Microsoft.AspNet.Cryptography.Internal.dll, type System.Runtime.InteropServices.DefaultDllImportSearchPathsAttribute  Can't find custom attr constructor image: /Users/brianpalladino/.dnx/packages/Microsoft.AspNet.Cryptography.Internal/1.0.0-beta4/lib/dnx451/Microsoft.AspNet.Cryptography.Internal.dll mtoken: 0x0a000004  * Assertion at class.c:5607, condition `!monoloadergetlasterror ()' not met

Turns out I needed to upgrade the version of mono on my Mac. I read this post and tried brew unlink mono  (to unlink v 3, the version I had installed) then brew install mono  to get v 4.

I then ran dnu restore  again, just in case changing the version of mono might affect any project dependencies. I didn’t know if this was necessary but I figured it wouldn’t hurt anything.

THEN, I ran dnx . kestrel  again and it started!

Screen Shot 2015-06-18 at 9.58.15 AM

At this point, the nice tutorial I found on the internet told me I should be able to view the app in my browser at http://localhost:5001/. As usual, the internet lied to me. I got the following exception when I tried to go to http://localhost:5001/:

IOException: kqueue() FileSystemWatcher has reached the maximum nunmber of files to watch.  System.IO.KqueueMonitor.Add (System.String path, Boolean postEvents, System.Collections.Generic.List1& fds) [0x00000] in <filename unknown>  System.IO.KqueueMonitor.Scan (System.String path, Boolean postEvents, System.Collections.Generic.List1& fds) [0x00000] in   System.IO.KqueueMonitor.Setup () [0x00000] in   System.IO.KqueueMonitor.DoMonitor () [0x00000] in   … and it goes on and on like that … do people really read these horrible things? ...

Another quick search on this error found this handy post. Solution:

  1. Stop kestrel (by hitting the Enter key)
  2. Run export MONOMANAGEDWATCHER=false
  3. Restart kestrel with dnx . kestrel

Screen Shot 2015-06-09 at 2.05.31 PM
Trying to justify my salary with Hello World apps.
After this I refreshed my browser and… VOILA!

After completing the steps above, I repeated the process on my Parallels OS X VM.

The second time around it all went very smoothly as I could implement the fixes for the various errors I got right from the start.

OS X Installation Process Summarized

Here is the entire process summarized after taking errors into account. You may need to use sudo on a few of these commands depending on whether or not you’re using a VM.

  1. Before starting: Install X-code, command line tools, and Homebrew
  2. Install DNVM: brew tap aspnet/dnx brew install dnvm source dnvm.sh export MONOMANAGEDWATCHER=false dnvm upgrade

  3. To save yourself some typing and aggravation later, add source dnvm.sh  to ~/.bashprofile, ~/.bashrc or ~/.zshrc and add export MONOMANAGED_WATCHER=false  to  ~/.bashrc
  4. Confirm you have the 1.0.0-beta4 version of DNX: dnvm list
  5. Install NodeJs and npm: brew install node
  6. Install Yeoman & Asp.Net Generator: npm install -g yo generator-aspnet  
  7. Install Bower and Grunt: npm install -g bower grunt-cli 
  8. Scaffold an ASP.NET 5 app with Yeoman: yo aspnet 
  9. Go to the project directory and update project references: dnu restore
  10. Start kestrel: dnx . kestrel 
  11. View the new web site in your brower: http://localhost:5001/
  12. To view or change the source code, download and install VS Code, open the newly created project in VS Code and verify there are no errors or missing references, then code away.

There! That wasn’t so bad was it? Now that we’ve conquered OS X, in our next post we’ll try this out on Ubuntu.

brian-palladino

Washington DC area software developer and consultant currently focused on .NET and Ruby tech stacks