How to Deploy .NET apps to Raspberry Pi A step-by-step guide explaining how to deploy a .NET app to a Raspberry Pi device using Framework-dependant and Self-contained deployment models

How to Deploy .NET apps to Raspberry Pi

If you are a IoT enthusiast, you have certainly already heard of Raspberry Pi. In a nutshell,  Raspberry Pi is a series of small single-board computers (SBCs) that can be used in many areas, such as:  media center, weather monitoring, camera control, 3d printing, AirPrinting, retro-gaming machines, robot control/automation, and so on. The best things about Raspberry Pi are its low cost, modularity (due to the presence of HDMI and USB ports), and open design, which make it the perfect companion for most computer and electronic hobbyists: if you want to see a bunch of typical Raspberry Pi usage scenarios in action, take a look at this coolest Raspberry Pi projects of 2021 post.

Since Raspberry Pi are SBCs, they do have their own operating system: the ideal choice for that is the Raspberry Pi OS, formerly called Raspbian: a Debian-based (32-bit) Linux distribution for download. However, many other third-party OS can be installed, such as: Ubuntu, Windows 10 IoT Core, RISC OS, and so on. As for the software, Raspberry Pi promotes Python and Scratch as the main programming languages, but the platform supports many other languages and frameworks, such as Microsoft .NET stack.

This basically means that, if you're a .NET developer, you can also use Raspberry Pi to deploy your .NET apps. In this post we'll briefly explain how to do that.

.NET Deployment models

Since Raspberry Pi has a Linux-based OS, the deployment of a .NET app is identical to that of any other platform: the app can run as self-contained or framework-dependent, depending of the deployment mode we want to enforce.

Each deployment mode has its own advantages, as explained in this .NET apps publishing overview article: in the following paragraph we'll explain how to deploy a typical .NET app using both strategies.

Framework-dependent deployment

To deploy our app as a framework-dependent app we need to perform the following steps:

  • Ensure SSH is enabled on our Raspberry Pi.
  • Install .NET on the Raspberry Pi using the dotnet-install scripts.

If SSH is not enabled, you might want to refer to the Raspberry Pi documentation to enable it. As for installing .NET, run the following commands from a Bash prompt on the Raspberry Pi (local or SSH):

The above command installs the latest version. If you need a specific version, replace the --channel Current parameter with --version <VERSION>, replacing <VERSION> with the build version you want to install.

Right after that, it's strongly advisable to add a DOTNET_ROOT environment variable and add the .dotnet directory to $PATH with the following commands:

This step isn't strictly required, but will greatly help to simplify path resolution.

Once these steps have been done, we can verify the .NET installation with the following command:

It's important to check that the displayed version corresponds to the version we wanted to install.

Now that .NET is installed on the Raspberry Pi device we can deploy the app using our development computer. This steps might vary depending on the development environment used:

  • From Visual Studio: deploy the app to a local folder, ensuring that Deployment mode is set to Framework-dependent and Target runtime is set to Portable.
  • Using the .NET CLI: use the dotnet publish command.

Once the app is published, we can copy the files from the publish location on the development computer to a new folder on the Raspberry Pi (for example, /MyApp). This steps can be done using a SFTP client, a portable USB device or any other method that allows us to copy the distribution files.

After we did all that, we can run the app on the Raspberry Pi by navigating to the newly-created folder and issuing the following command from the terminal (assuming the name of the app is MyApp and the main entry point is MyApp.dll):

The app should run right off the bat.

Self-contained app deployment

Deploying our app using the self-contained app strategy is arguably simpler in terms of what we need to do, since we don't have to pre-install anything: we just have to ensure that SSH is enabled on your Raspberry Pi, publish the app on our development computer and then copy the distribution bundles to the Raspberry Pi device.

Publishing the app from the development computer requires different tasks depending on the development environment :

  • From Visual Studio: deploy the app to a local folder, ensuring that Deployment mode is set to Self-contained and Target runtime is set to linux-arm.
  • Using the .NET CLI: use the dotnet publish -r linux-arm command.

Again, once the app is published, we can copy the files from the publish location on the development computer to a new folder on the Raspberry Pi (for example, /MyApp). This steps can be done using a SFTP client, a portable USB device or any other method that allows us to copy the distribution files.

Once done, we'll also have to set the execute permissions to the app's executable file name and/or DLL file, depending on the app type. For example, if the name of our app is MyApp, we'll need to execute the following command:

After we did all that, we can open the Raspberry Pi terminal, navigate to the newly-created folder and run the app's executable using the following command (assuming that the name of our app's executable file is MyApp):

Again, the app should run without issues.

Conclusion

That's basically it, at least for now: we hope that this post will help other .NET developers to publish their app on a Raspberry Pi device using one of the two available deployment models: Framework-dependant and Self-contained app.

About Ryan

IT Project Manager, Web Interface Architect and Lead Developer for many high-traffic web sites & services hosted in Italy and Europe. Since 2010 it's also a lead designer for many App and games for Android, iOS and Windows Phone mobile devices for a number of italian companies. Microsoft MVP for Development Technologies since 2018.

View all posts by Ryan

Leave a Reply

Your email address will not be published. Required fields are marked *


The reCAPTCHA verification period has expired. Please reload the page.

This site uses Akismet to reduce spam. Learn how your comment data is processed.