.NET has the best of breed solutions for all modern workloads in a single , unified development platform
This statement was in a talk given back in June 2021 at devintersection by Scott Hunter.
The Talk had lots of impressive demonstrations, but what I wanted to know was Can we run .Net 6 on a Raspberry Pi ?
Raspberry Pi Hardware
The are of course some caveats, .Net is not going to run on all Raspberry Pi Hardware. Right now, I would reccomended using a Raspberry Pi 4 (4GB), especially if you are going to write code on the Pi rather than just run it.
Raspberry Pi OS
I am going to use Raspian OS 64, at the time of writing its still in beta but the latest version seems to be good with all the testing I have done. I have also manually run apt-get updates to get all the latest updates.
Getting Started
Browse to https://dotnet.microsoft.com/download/dotnet/6.0 you should see the latest sdk preview here. At the time of writing this blog post, SDK 6.0.100-preview.7 was the latest version.
As I have installed a 64 bit OS on my Raspberry Pi. I chose the Arm64 link.
This takes you to the following page https://dotnet.microsoft.com/download/dotnet/thank-you/sdk-6.0.100-preview.7-linux-arm64-binaries where you can copy the direct link and download it onto your pi
Wget https://download.visualstudio.microsoft.com/download/pr/084d5037-7dee-4654-b91a-fe303fa62d74/553744c6fcf2ed1128e40fa9f6cd4516/dotnet-sdk-6.0.100-preview.7.21379.14-linux-arm64.tar.gz
Install .Net 6 preview
You can now run the following
mkdir -p $HOME/dotnet && tar zxf dotnet-sdk-6.0.100-preview.7.21379.14-linux-arm64.tar.gz -C $HOME/dotnet
export DOTNET_ROOT=$HOME/dotnet
export PATH=$PATH:$HOME/dotnet
The export commands will set up a temporary PATH Reference which will remain active until you reboot or shutdown the Pi. In order to preserve this you will need to update your bash shell. More on this later.
Now in a new Terminal window you should be able to run the following
dotnet --info
Congratulations, you have installed the latest .Net 6 on your Raspberry Pi.
Create a Simple Api
Ok so at the command line, Create a folder e.g. DotNetWebPi and go into the folder
mkdir DotNetWebPi
cd DotNetWebPi
type the following
dotnet new web
after a few minutes you will see the following in the command line
Now in the Command line simply run
dotnet watch
make sure you are in the DotNetWebPi ( or the the name of the folder you created)
Now you can go back to your browser and browse to http://localhost:5000
Hot Reload
Running dotnet watch, enabled Hot reload so using an editor like Nano or VsCode you can edit the Code and you changes are automatically re-compiled once saved.
As an example lets change the code to be
var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();
if (app.Environment.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.MapGet("/", () => "Hello World! from Raspberry Pi");
app.Run();
Save the Code in your editor.
Now refresh your Browser
Updating the Bash Shell.
As I mentioned earlier, there are two commands you had to run
export DOTNET_ROOT=$HOME/dotnet
export PATH=$PATH:$HOME/dotnet
if you have closed your terminal window or rebooted your pi. You may have noticed that the Dotnet Commands no longer work. we need to persist the changes to the bash shell so they are permantly configured.
Open your Bash Shell
Paste the following to the bottom of the file
export DOTNET_ROOT=$HOME/dotnet
export PATH=$PATH:$HOME/dotnet
So it looks like we can Run .NET6 on the Raspberry Pi. The example was just using the Asp.net Core Template but you can also create console apps.
References
https://dotnet.microsoft.com/download/dotnet/6.0
https://devblogs.microsoft.com/dotnet/announcing-net-6-preview-7/?WT.mc_id=dotnet-35129-website