How to run ASP.NET 5 Beta 3 or GoLang on a Raspberry Pi 2
Hanselminutes and CodeNewbie are teaming up to produce two new podcast episodes a week for the month of March, featuring incredible makers in the hardware space. There will be new content every weekday in March, so bookmark http://www.marchisformakers.com and join us!
- CodeNewbie is the most supportive community of programmers and people learning to code. Tune in every week for stories about people on their coding journey.
- Subscribe via RSS or on iTunes. Published Mondays.
- Hanselminutes: Fresh Air for Developers is a weekly talk show that brings interesting people together to talk about the web, culture, education, technology and more.
- Subscribe via RSS or on iTunes. Published Thursdays.
Our hashtag for this event is #MarchIsForMakers and YOU, Dear Reader, can help us out by being our Street Team.
Share our website http://marchisformakers.com!
...with friends, kids, teachers, family, anyone you think my be benefit from learning about hardware and today's maker culture.
This is just Day 1! At the end of March we'll have as many as 10 great podcasts, amazing guests, Live Google Hangouts, blog posts, Twitter Chats on Wednesdays, and a huge collection of links and projects for you to explore.
How to run ASP.NET Beta 3 running on a Raspberry Pi 2
I love Raspberry Pi, the tiny $35 computer. I've gone through 6 at least count. Have they died? Not at all! They've been gifted forward. Right now I've got one running my 3D Printer, one running as a media center, and a Raspberry Pi 2 that my kids are using as their primary computer. There's so many Raspberry Pi projects - How can you not love a tiny computer?
This actually a dual tutorial/how-to. I've been so impressed with the Raspberry Pi 2 I've wanted to see how far one can take it. It's still a modest little machine, but it's definitely twice as fast or more in single-tasking and perhaps 6x faster in multitasking in my experience than the previous Raspberry Pi.
Basic Raspberry Pi set up
I use the Raspbian Operating System image for my Raspberry Pi 2. It's a Debian Wheezy image for techies, that's a Unix for non-techies.
I use this hardware (these are Amazon links) that I put together myself, although you can get a kit that includes memory, power, wifi, cables, case, etc.
- 32 gig Micro SD Card
- Edimax Wi-Fi USB Adapter (amazing!)
- Microsoft Arc Wireless Keyboard although any USB Keyboard will work
- Microsoft USB Wireless Mouse although, again, any USB mouse will work
- A 2.1A (You need at least 1A) USB Adapter - I've also had luck with Samsung phone USB power
You can get the disk image and follow the setup instructions here. I also added TightVNC so I could remote into my Raspberry Pi from my desktop. This also allowed me to run it "headless" without a monitor, but it's up to you.
You can see me VNC'ed into my Raspberry Pi 2 here. Of course, you can always connect it to your monitor or TV.
I wanted to see how hard it would be to run .NET on this Raspberry Pi. Depending on how deep you want to go, it's not hard at all.
Running ASP.NET on a Raspberry Pi 2
NOTE/DISCLAIMER: This is a point in time. It's a beta/daily build of an early thing. I'm sure this will get down to a few simple lines in the future, so don't panic thinking that ASP.NET on Linux will suck. It's early.
Frist, you can get an old (3 years old) version of the open source Mono runtime with the stable standard repositories
sudo apt-get mono-complete
And this will get you version 3.2.8. You can do basic stuff. Make a HelloWorld.cs and run it with
gmsc HelloWorld.cs
mono HelloWorld.exe
Debian likes to be very stable, I'm told, so if you want to get a very NEW version of Mono like version 3.10 (that's "Three Point Ten") and as such, run things like ASP.NET 5, you'll need to do a little more work.
UPDATE and IMPORTANT NOTE: There's two options here. Build Mono from source, or use a custom repository from the Mono folks.
Option 1: Install Mono from The Mono Project's repositories
The Mono Project has their own repository for Debian distributions like Raspbian Wheezy. If you've put on an early mono, you'll want to sudo apt-get remove mono-complete first to tidy up.
Per their instructions from the Mono site, you'll then
sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 3FA7E0328081BFF6A14DA29AA6A19B38D3D831EF
echo "deb http://download.mono-project.com/repo/debian wheezy main" | sudo tee /etc/apt/sources.list.d/mono-xamarin.list
sudo apt-get update && apt-get upgrade
sudo apt-get install mono-complete
And you should be all set!
Option 2: Build Mono from Source (slow and advanced and of questionable value)
You could git clone the Mono repository and build it. I found this process slow, so I downloaded the source from an archive instead, then unzipped it into a folder.
mkdir ~/monosource
cd ~/monosource
wget http://download.mono-project.com/sources/mono/mono-3.10.0.tar.bz2
tar -xjvf mono-3.10.0.tar.bz2
Then here's a bunch of prerequisites I needed, plus htop because I like it.
sudo apt-get install build-essential autoconf automake binutils libtool libglib2.0-dev libxrender-dev libfontconfig1-dev libpng12-dev libgif-dev libjpeg8-dev libtiff5-dev libexif-dev gettext libcairo2-dev htop
Then start the build. This too an hour or so, and is dependent on the speed of your Pi AND the speed of your SD card. It should be a Class 10 if possible.
./configure --prefix=/usr/local; sudo SKIP_AOT=true make; sudo SKIP_AOT=true make install;
Note this is three lines in one. Do a sudo reboot when you're done. If you can run mono -V and see version 3.10.0 then you're in a good place.
Adding ASP.NET 5
You can add ASP.NET 5 at this point by downloading the samples directly from Github and running the "kvminstall.sh" to setup the ASP.NET runtime manager.
mkdir ~/sources/aspnet5
cd ~/sources/aspnet5
git clone git://github.com/aspnet/home.git
sh ~/sources/aspnet5/kvminstall.sh
source ~/.k/kvm/kvm.sh
kvm upgrade
Then per this GitHub issue you need to tell your system about the SSL certs for NuGet to restore correctly.
CERTMGR=/usr/local/bin/certmgr
sudo $CERTMGR -ssl -m https://go.microsoft.com
sudo $CERTMGR -ssl -m https://nugetgallery.blob.core.windows.net
sudo $CERTMGR -ssl -m https://nuget.org
mozroots --import --machine --sync
Then go to one of the samples like ~/sources/aspnet5/home/samples/HelloMvc and run "kpm restore." Note this uses about 400 megs of RAM for a minutes so you'll want a newer Raspberry Pi.
NOTE: Make sure the sample version in project.json match your local runtime version. I needed to update version strings to beta3 to match what "kvm list" said. I'm sure this will get fixed soon.
The "Kestrel" web server uses libuv, an HTTP library. Here is how to build libuv. I found this on Punit Ganshani's blog, which I'm actually wishing I'd found earlier in this blog post. ;)
sudo apt-get install gyp
wget http://dist.libuv.org/dist/v1.0.0-rc1/libuv-v1.0.0-rc1.tar.gz
tar -xvf libuv-v1.0.0-rc1.tar.gz
cd libuv-v1.0.0-rc1/
./gyp_uv.py -f make -Duv_library=shared_library
make -C out
sudo cp out/Debug/lib.target/libuv.so /usr/lib/libuv.so.1.0.0-rc1
sudo ln -s libuv.so.1.0.0-rc1 /usr/lib/libuv.so.1 Then run
Then run "k kestrel" and hit the port mentioned in the project.json.
How to run Go on a Raspberry Pi 2
I wanted to Go running as well. Go has fewer dependencies but no official ARM builds. The Raspberry Pi 2 is an ARMv5. However, a very kind gentleman named Dave Cheney has been building and hosting his own unofficial ARM tarballs for Go. You take the instructions from the GoLang site and his links and you're all set on your Raspberry Pi or Pi 2.
Here's what I did for a Raspberry Pi 2.
wget http://dave.cheney.net/paste/go1.4.2.linux-arm~multiarch-armv7-1.tar.gz
sudo tar -C /usr/local -xzf go1.4.2.linux-arm~multiarch-armv7-1.tar.gz
Then, add it to your path, or .profile, or whatever.
export PATH=$PATH:/usr/local/go/bin
Make a HelloGo.go, compile and run.
Personally, I'd love to see "dotnet" be as easy to get running on Linux as Go.
package main
import "fmt"
func main() {
fmt.Println("Hello Go on Raspberry Pi 2")
}
The Raspberry Pi is just a little joy. It's a lot of fun and has a lot of potential. Definitely pick up some for the kids (yourself.)
Don't forget, visit http://marchisformakers.com, tell your friends and tweet us at #MarchIsForMakers!
Related Links
- Raspberry Pi at Amazon
- Many Raspberry Pi projects - How can you not love a tiny computer?
- Top 10 Raspberry Pi Myths and Truths
- The new Raspberry Pi 2 will run Windows 10 and run Universal Apps
About Scott
Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.
About Newsletter
Why did you build Mono from source and not use their repos (see here)?
I just installed Mono 3.12 on my Pi2 running Raspbian that way and it was much quicker (and easier IMO) then building it from source (as I did it that way in the past)...
Hope to see you at //build/ this year (again)?
Christoph - Likely my own ignorance! When was that setup? I didn't realize that also worked on the pi. I will try and update my post.
Amazing post, thank you for sharing..
I am trying to make this thing work for about two days by now and i am still stucked at:
System.InvalidatOperationException: No service for type 'Microsoft.Framework.Runtime.IServiceManifest' has been registered.
Can somebody please take whole raspbian / arch linux image with working asp.net 5 and upload it somewhere on google disk or anything similar? I would be very greatful for this! :)
Scott in your guide there's the step:
sh ~/sources/aspnet5/kvminstall.sh
where the script is cloned to:
sh ~/sources/aspnet5/home/kvminstall.sh
and the kre directory isn't quite right, it should be:
source ~/.k/kvm/kvm.sh
I didn't manage to get any further though, tried changing the version numbers to beta3 in the project.json but still get:
System.NotImplementedException: The requested feature is not implemented.
at Microsoft.Framework.PackageManager.PackageUtilities+<OpenNuspecStreamFromNupkgAsync>d__1.MoveNext () [0x00000] in <filename unknown>:0
When running 'kpm restore' on any of the samples.
Works now, thanks for the guide!
Tempted to try doing it again with Docker now... :)
Can you please upload your working image of your sd card somewhere to web?
Jon - Baby steps, but you're right, Docker for Raspberry Pi is a very reasonable solution! I'll see what I can do.
What are you trying to get that error, perhaps we can help?
http://dave.cheney.net/paste/go1.4.2.linux-arm~multiarch-armv7-1.tar.gz
I tried to run kpm restore on another repository: https://github.com/davidfowl/HelloWorldVNext and it finished OK. But when i try to run project by "k web" or "k kestrel" ill get an exception:
System.InvalidOperationException: No service for type 'Microsoft.Framework.Runtime.IServiceManifest' has been registered.
I also tried to increase size of swap file and then run kpm restore, but with no good result. I think asp.net 5 should work also on older raspberies, am i correct?
Thank you for helping me :)
Thanks so much for this. Bookmarking and ordering my Raspberry Pi as we speak. Also super excited about #MarchisForMakers.
Christina
1) sh ~/sources/aspnet5/kvminstall.sh -> sh ~/sources/aspnet5/home/kvminstall.sh
2) CERTMGR=/usr/local/bin/certmgr -> CERTMGR=/usr/bin/certmgr
3) mozroots --import --machine --sync -> sudo mozroots --import --machine --sync
And the biggest change:
before running ./gyp_uv.py -f make -Duv_library=shared_library , I had to execute this command:
git clone https://chromium.googlesource.com/external/gyp.git build/gyp
Docker is one of those tools that pays to take the few hours to learn it.
Harold - I hope so!
jodegreef - Ah! I ended up doing sudo apt-get install gyp. I'll add that.
I'm not a Docker expert, but it looks like it's not ready for Pi yet.
I will sure buy PI2, but i would feel sorry for old raspberies, they should also experienced hosting asp.net, at least once in their lives :))
I now have ASP.NET 5 Beta 3 running on my Pi Model B (512 MB RAM).
To accomplish this, I followed your instructions, with some slight differences (compiled Mono 3.12, libuv 1.4.2 and used dd/mkswap/swapon to add some extra swap space for 'kpm restore').
Attempting to use the Debian Mono repository on the Model B does not work (because the packages require the ARMv7 arch). I really recommend nobody try this - it wasn't fun to remove/purge partially installed packages when the uninstallation scripts won't run.
The mozroots command would also fail "Couldn't retrieve the file using the supplied information" so I downloaded certdata.txt and hosted it locally on IIS, and changed my hosts file so mozroots would request the file from my local Windows box. I'm guessing it times-out too prematurely for a slow Pi.
It seems to start-up HelloMvc fast enough (10 seconds max), and the page on :5004 is served up in under 300ms.
Thanks again. My Raspberry Pi Model B just got a little more awesome! :-)
In Startup.cs, after changing "app.UseMvc();" to:
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
The /Views/Home/Index.cshtml page on :5004 is served up in under 40ms - which is much better than the 300ms I reported above (which was for the welcome page).
That's pretty awesome on a Pi Model B - even if HelloMvc is just a debug build.
Screenshot @ http://i.imgur.com/UsuIrMp.png
I find the concept fascinating - if there's ever a good reason to have .NET run on Linux, iOT and small devices are surely a major one!
But... I wonder whether this will be a viable thing to do in the future. The issue I see is that there will need to be an eco system that supports all the cool things you can do with the hardware and you'll need some sort of low level interface to make that happen. How will that work if you're running .NET on Linux and Linux hardware? How do you hook into the underlying hardware and/or will there even be a .NET eco-system alongside the Node's, Pythons and C++'s? A lot more needs to happen than just 'support .NET on 'nix', no?
On my Pi B it takes FOREVER to load Kestrel (I think I'm using ASP.Net beta 2) after booting (5+ minutes) although once the site is up and built, refreshing the page is rather fast. I wonder if that's my own fault for building Mono from source (which took 6+ hours, so your 6x speed number is anecdotally accurate!)
One really annoying issue that I encountered when setting kestrel to start at boot (in an init.d script) is that since 'k' waits for Console.ReadLine (aka stdin) it can close prematurely if started in the background (specifically, I think a SIGTTIN signal is sent to the process when it tries to ReadLine which is unhandled and terminates kestrel). I was finally able to fix it using a shell script from a related issue that spawns a fifo for kestrel to listen on.
I'm absolutely amazed at how far .Net has come lately now that it can run web pages from a RasPi.
System.IO.FileNotFoundException: Could not load file or assembly 'Microsoft.Framework.ConfigurationModel.IConfiguration, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies.
File name: 'Microsoft.Framework.ConfigurationModel.IConfiguration, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null'
at (wrapper managed-to-native) System.Reflection.MonoMethod:InternalInvoke (System.Reflection.MonoMethod,object,object[],System.Exception&)
at System.Reflection.MonoMethod.Invoke (System.Object obj, BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) [0x00000] in <filename unknown>:0
Few deviations:
Thanks for putting this together,
Mark
I currently have two, one running a security camera (I was going to write my own but why re-create the wheel, I used apt-get motion and fswebcam and then wrote .net programs to polish off the captures like I want them and roll them up into something that's easy and quick to consume). On my Windows box I've used AForge to interpret what's on the images to figure out which ones maybe more significant (like, if it can find a face). I haven't yet tried an AForge PCL port I found that doesn't use the Windows API but it is in my near future to try.
Oh, and the other is running RetroPie with EmulationStation. EmulationStation is in my opinion a very elegant looking shell. In fact I like it better than most actual game stations (it wraps the RetroPie project together). I have this one also setup to be a make shift security system if I want to turn it on, I just leave a web-cam plugged into it. The EmulationStation code is available on GitHub also (using it I was able to load up a Commodore 64 app from the shell that I wrote when I was 7 back in 1985 which does nothing but make me smile with nostalgia).
My only complaints I have had with Linux is general is setting up Wifi can be a headache and different between every install as to what gets it working (throw something in like having multiple routers and it gets touchier and touchier, although not impossible by any means, clearly got it working after tweaking configs over an hour).
I love reading your posts Scott. Best to you!
Now it's time to learn some ASP.NET!
sudo apt-get update && sudo apt-get upgrade
Your example above leaves sudo off the second command.
Andrew
Keep up the great work; loving all the new ASP.NET 5 MVC 6 stuff coming out of the teams not to mention all the IoT work :D
I get this:
pi@raspberrypi ~ $ sudo apt-get install mono-complete
Reading package lists... Done
Building dependency tree
Reading state information... Done
Some packages could not be installed. This may mean that you have
requested an impossible situation or if you are using the unstable
distribution that some required packages have not yet been created
or been moved out of Incoming.
The following information may help to resolve the situation:
The following packages have unmet dependencies:
mono-complete : Depends: mono-devel (= 4.0.1-0xamarin5) but it is not going to be installed
E: Unable to correct problems, you have held broken packages.
pi@raspberrypi ~ $
Please update guide, as I would really like to get i working :-)
If you got this problem, you have an old version of Mono on your system. Remove everthing that Looks like Mono with sudo apt-get remove mono... where "..." is the suffix which you find when you press "Tab".
Comments are closed.