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:
- brew tap aspnet/dnx (no errors)
- brew install dnvm (no errors)
- source dnvm.sh (no errors)
- 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.
If DNVM is correctly installed, you should see something like this when you enter dnvm in the terminal:
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 :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
yocommand 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:
Trying it again with sudo resolved the problem: sudo npm install -g yo generator-aspnet
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.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:
A quick glance in Finder confirmed the new project files were all there, as expected.
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:
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 ).
But I got some more errors and again the suggestion I try as root:
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.Start_noshell (System.Diagnostics.ProcessStartInfo startInfo, System.Diagnostics.Process process) [0x00000] in
2 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
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!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 `!mono_loader_get_last_error ()' 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!
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.List
1& fds) [0x00000] in <filename unknown> System.IO.KqueueMonitor.Scan (System.String path, Boolean postEvents, System.Collections.Generic.List1& fds) [0x00000] in
Another quick search on this error found this handy post. Solution:
- Stop kestrel (by hitting the Enter key)
- Run export MONO_MANAGED_WATCHER=false
- Restart kestrel with dnx . kestrel
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.
- Before starting: Install X-code, command line tools, and Homebrew
- Install DNVM: brew tap aspnet/dnx brew install dnvm source dnvm.sh export MONO_MANAGED_WATCHER=false dnvm upgrade
3. To save yourself some typing and aggravation later, add source dnvm.sh to ~/.bash_profile, ~/.bashrc or ~/.zshrc and add export MONO_MANAGED_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.