Tuesday, August 31, 2021

Install .Net Runtime and run .Net apps in Raspberry Pi 4

Here we are continuing the experiments with Raspberry Pi 4. As a .Net developer, what is the meaning if we cannot install .Net into RasPi and run one program?

Please note this post is aiming at installing the .Net runtime, not the SDK. Development and compilation will be done outside of RasPi. Also, this is not aiming to run ASP.Net, just simple .Net console apps only.

Do we really need to install .Net runtime to RasPi?

It depends. If we want to run a self-contained application, there is no need to install the .Net runtime. Follow the below steps as mentioned in Microsoft docs.
  • Publish with the 'linux-arm' option.
  • Copy the folder to RasPi
  • Give execute permission
  • Run the app

It worked like a breeze. But the self-contained publish model includes the runtime and is really big. The above screenshot shows the size is around 80MB for a simple console application. To avoid that, we can install the runtime.

Attempt 1 - Install the apt-get way

When we google, we get the answers that we have to execute some scripts to install .Net runtime. The sudo apt-get is nowhere available. But I decided to give it a try using the Microsoft tutorial targeted to Ubuntu 21.04.
It failed gracefully with a series of messages

 "E: Unable to locate package dotnet-runtime-5.0
E: Couldn't find any package by glob 'dotnet-runtime-5.0'
E: Couldn't find any package by regex 'dotnet-runtime-5.0'"

The screenshot is given below

Let us do the next attempt.

Attempt 2 - Install using the scripts 

This time let's follow the Microsoft link as is https://docs.microsoft.com/en-us/dotnet/iot/deployment. I run the first command and below goes the output.

Wow. It installed the SDK. The above link is not at all talking anything about SDK. It's just talking about deploying the framework-dependent applications only. Let us check what is installed

Microsoft is great. We just asked .Net runtime. But we got SDK and ASPNetCore runtime as well. Now our RasPi is a full-fledged web server.

Once we have the installation ready. Get the published application. Make sure published as Framework-dependent and Target runtime set to Portable. Then run the normal command as below

dotnet "app.dll"

Closing thoughts

It is better to publish as a self-contained app to avoid the installation of .Net. The footprint will be big but it is easy to clean up. There will be new .Net versions releasing every year and their patches. If we keep with those releases, the .Net installation itself may be a big footprint later. (I am yet to check what it means .Net 5 and .Net 6 installed side by side)

Happy IoTing.

No comments: