Running .NET Core on Raspberry PI with Raspbian Stretch

Setting up .NET Core on Raspbery Stretch Linux distribution running on Raspberry PI

Apart from running on Windows and Linux both x86 and x64 architecture, .NET Core is an ideal platform for IOT (Internet of things) projects because it can also run on ARM32 and ARM64. This means you can run it pretty much any device with both RISC or CISC processor architecture.

If you are .NET developer or you are familiar with .NET platform and C# language, this means you can now write applications that run on Raspberry Pi which is based on RISC architecture by writing them in Visual Studio. I happen to have Raspberry Pi 3 and so far all the custom software I was writing for it was either Java or Python. I do not think Java or Python are not a good choice, but if you alredy have great experiance with .NET and C#, it takes some time to get used to a new language and environment including development and deployment process as well. With ability to run .NET Core on Raspbery Pi, you can focus on a logic and your software functionality rather than technical issues.

I guess you already have Raspberry Pi setup and running, but in case you don't I will just quickly go through the basic setup that will allow you to connect from your PC and setup .NET Core environment. In case that you are running RPi as a headless device, meaning you do not have any display attached to it, the following steps may help you setup your device without even have to have any screen attached to it. We also do not want to waste our very limited resources of the RPi on a UI. We will have it most of the time just silently running in background and interacting with it through SSH will be more than enough.

The following are steps that we will cover in this article:

  • Setting up Raspbian operating system on SD card
  • Setting up WiFi and SSH
  • Downloading and installing .NET Core

Now that we have covered some basic intro, let start with the list and see in details steps that will enable us to write and run .NET Core application on Raspberry Pi.

Setting up Raspbian operating system on SD card

Raspberry Pi is a pretty simple deice, it does not even require hard drive too boot operating system from it. Instead it comes with direct support for SD card where you can store lightweight operating system in order to boot device. I guess you already have SD dedicated for your RPi device, but before you inset it into SD slot on RPi you first need to install operating system on it.

There are several distributions of Linux that you can run on Raspberry Pi device. The complete list is on official Raspberry Pi website in downloads section https://www.raspberrypi.org/downloads/. I decided to go with Raspbian for this article since I already use it on my device and it uses aptitude package manages, same as Debian distribution from which it derived from. You won't see much differences between Raspbian in Debian in terms of usage from the command line.

So download the Raspbian Lite option of the Raspbian operating system to your PC and unpack the downloaded archive. Now that you have the operating system image you need to write it to SD card. Install Win32DiskImager which you can download from Sourceforge and select extracted Raspbian image file and you SD card drive letter.

Pi Diskimager Win32

Simply click write and wait for process to finish. This will setup basics for running Raspbian on you Raspberry Pi device. If you have screen and keyboard attached to RPi you can insert SD card and spin it up, but if that is not the case, check the following steps which will allow you to control your Raspberry Pi from your laptop via SSH client.

Setting up WiFi and SSH

Since you probably do not have the spare screen and keyboard to hook up to you Raspberry Pi device, you will have to access it via SSH from your PC, but your device still has no idea which WiFi to use and which credentials to use to connect to it. That is why you first need to add WiFi settings to SD card.

Note

Win32DiskImager will create several partition on SD card but only one (boot) will be accessible from Windows on your PC. You might get prompted to format SD card as Windows is unable to read them. Ignore this and cancel the dialog box. DO NOT format any of the partitions on the SD card as you will have to start the process from the beginning.

To add WiFi settings to RaspberyPi, open SD card on your PC and add file named wpa_supplicant.conf to the root. Open file in text editor and add the following content

ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
network={
    ssid="YOUR_NETWORK_NAME"
    psk="YOUR_PASSWORD"
    key_mgmt=WPA-PSK
}
    

Replace YOUR_NETWORK_NAME with the WiFi network to which you want Raspberry pi to connect and YOUR_PASSWORD with WiFi password value. Save the file and you RPi will pull in these settings once booted.

Note

These WiFi settings are for Raspbian Stretch version. They may deffer depending on version of Raspbian you are installing in your Raspberry Pi device

Connecting you RaspberryPi device to WiFi will not be enough in order for you to access it from your PC. By default SSH server is disabled, so you will have to enable it before you can start accessing it from your Windows PC. To enable SSH on Raspberry Pi, simply add file names ssh.txt to SD card root and you are good to go.

Now you can take out SD card from your PC, plug it into the RaspberryPi and switch it on. You will be able to access Raspberry Pi using any SSH client. The most used one is PuTTY. It's free, you can simply download it and run it on Windows PC.

Putty Rpi

Simply type in the IP address of your Raspberry Pi device assigned too it from your WiFi router and for authentication use username pi and password is raspberry.

Note

Keeping your Raspberry Pi running with default password for pi user is serious security vulnerability especially if you expose your RPi device publicly. Consider changing default password right upon your first login over SSH.

Downloading and installing .NET Core

Now when you have you Raspberry Pi setup and accessible from your Windows PC, you can connect using PuTTY and setup .NET Core framework support.

First thing to do is to download the build for ARM32. The latest version of .NET Core is 2.2 and you can download it using wget from the following URL

pi@raspberrypi:~ $ wget https://download.visualstudio.microsoft.com/download/pr/36bff52b-2cdd-4011-8e92-d00f7537704f/9885ba267b1ef29a1401adc387d9a016/dotnet-sdk-2.2.101-linux-arm.tar.gz

Now you have the .tar.gz package downloaded to your /home/pi folder. Since we might want to have more accounts, for example for different daemons running on the system, it is good practice to extract the package to /bin/dotnet folder, so that is what we are going to do.

pi@raspberrypi:~ $ sudo mkdir -p /bin/dotnet && sudo tar zxf dotnet-sdk-2.2.101-linux-arm.tar.gz -C /bin/dotnet

Now we need to setup the environment variables so that our environment is aware of .NET Core executable.

pi@raspberrypi:~ $ export DOTNET_ROOT=/bin/dotnet 
pi@raspberrypi:~ $ export PATH=$PATH:/bin/dotnet

This will only setup environment variables for the current session. Once you disconnect or you reboot the device, these environment variables will be reset. To make environment variable values permanent, you need to update you ~/.bashrc file.

pi@raspberrypi:~ $ sudo nano ~/.bashrc

Run nano editor as sudo to be able to edit it, scroll to the bottom and paste the following line in a new line at the end of the file export PATH=$PATH:/bin/dotnet

Now your Raspberry Pi is ready to run your .NET Core applications. Double check the the .NET Core version by typing the followoing command

pi@raspberrypi:~ $ dotnet --version
2.2.101

References

Disclaimer

Purpose of the code contained in snippets or available for download in this article is solely for learning and demo purposes. Author will not be held responsible for any failure or damages caused due to any other usage.


About the author

DEJAN STOJANOVIC

Dejan is a passionate Software Architect/Developer. He is highly experienced in .NET programming platform including ASP.NET MVC and WebApi. He likes working on new technologies and exciting challenging projects

CONNECT WITH DEJAN  Loginlinkedin Logintwitter Logingoogleplus Logingoogleplus

JavaScript

read more

SQL/T-SQL

read more

Umbraco CMS

read more

PowerShell

read more

Comments for this article