Did you know that Linux ARM32 is an officially supported distro for .NET Core? That's pretty exciting if you're at all into IOT or tinkering with Raspberry Pi or other devices. So whether you've got a Raspberry Pi you're using as a desktop, media center, application server, robot, environment monitor, or something else, you can now build & push your own .NET Core code to it and take more control over it.
If you don't have a Raspberry Pi that is currently in working order that's pretty easy to fix. Let's start setting one up by getting the big "scary" bash stuff out of the way first...
Setting up Your Raspberry Pi
I set up a fresh Raspberry Pi just for playing with .NET Core. I'm running Windows 10 (1903) and connecting to my Raspberry Pi over SSH, these steps will be based on the assumption you're doing the same. Here's what I did:
Download the Latest Raspberry Pi Image
The first thing I did was download the latest Raspbian currently available (Buster). I downloaded the .iso that has the desktop and recommended software, you can choose whichever one you want.
After your download completes, you'll need to write the image to your SD card. On Windows I use
Win32DiskImager you can get a more complete list of image writing tools here.
Post Install Config for .NET Core Readiness
After you've got your Pi running, walk through the initial setup - select a language, keyboard settings, set password, wifi, install updates, etc...
For Debian 9 Systems Microsoft recommends to install the following:
sudo apt-get update sudo apt-get install curl libunwind8 gettext apt-transport-https
Raspbian is based on Debian, "Buster" is Debian 10. So technically these steps aren't for this Buster-based Raspbian system, but they work.
Creating a New .NET Core App for Raspberry Pi
With the Raspberry Pi setup out of the way we can now start making some code to run on it. Create a new console application on the command line, in the directory where you want it to be created:
dotnet new console -n GettingStartedRaspberryPi -o src
That's it. For the first .NET Core app I'm running on the Raspberry Pi I kept it simple and will use the default "Hello World" console app.
Building .NET Core Code to Run on Raspberry Pi
The code you write is the same, but the trick to getting it running on your Pi is how you build it. To build .NET Core code that will run on a Raspberry Pi you need to build for Linux ARM which can be done by running:
dotnet publish -o ./dist -r linux-arm
-r linux-arm option publishes the application code for the Linux ARM runtime, this will create a self-contained deployment that can be run on the Raspberry Pi. Read more about self-contained deployments here.
-o dist will publish to the "dist" folder so now in
./dist you will find the self-contained app to copy to your Raspberry Pi.
Deploying your .NET Core App to Raspberry Pi
How you transfer your built code from your local machine to your Raspberry Pi is largely up to you - you can SCP, rsync, fileshare, flash drive. SCP is quick (and included with Windows 10 now) so I'll use that. From the parent directory of your
scp .\dist\* pi@buster-pi:~/getting-started-rpi-sample/
You'll want to use the "*" instead of "*.*" because the Linux executable has no extension.
Be sure to replace the paths and Raspberry Pi user & machine name to match yours.
Running your .NET Core App on Raspberry Pi
After copying the
dist directory you can now SSH into the Raspberry Pi. Before you can run the application you need to chmod your app so that it has execute permissions via one of two commands:
pi@buster-pi:~/ $ chmod +x ~/getting-started-rpi-sample/GettingStartedRaspberryPi OR pi@buster-pi:~/ $ chmod 755 ~/getting-started-rpi-sample/GettingStartedRaspberryPi
After the permissions are set it is time to run the .NET Core app on the Raspberry Pi!
pi@buster-pi:~ $ ./getting-started-rpi-sample/GettingStartedRaspberryPi Hello World!
Pretty cool! We didn't even need to have the .NET Core framework installed on the Raspberry Pi thanks to building the code as a self-contained app.
I'm really excited about the possibilies this opens up for .NET Core and Raspberry Pi. I think that bringing the .NET stack to the Raspberry Pi will open up a whole new world of possibilities for .NET developers, and that more Raspberry Pi users will get introduced to .NET Core. One thing I'd like to point out is that Raspberry Pi is not "just another environment" for .NET Core to run on. With the availability of the GPIO pins it is much more and in a future post, I'm going to explore interacting with the real world.
Update August 15, 2019
I just posted an article about Single-File self-contained executables with .NET Core 3.0. If you're interested in developing .NET Core apps for the Raspberry Pi I recommend checking it out.