Cross Compile on Windows 11 to Linux ARM64 (Raspberry)

The quickest and easiest way to cross compile NativeAot on Windows to a Raspberry Pi is using PublishAotCross nuget package.

Set <PublishAot> to true and install nuget package PublishAotCross.

<Project Sdk="Microsoft.NET.Sdk.Web">

  <PropertyGroup>
    <TargetFramework>net9.0</TargetFramework>
    <Nullable>enable</Nullable>
    <ImplicitUsings>enable</ImplicitUsings>
    <PublishAot>true</PublishAot>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="PublishAotCross" Version="1.0.0" />
  </ItemGroup>
  
</Project>

Package requires zig, download and extract it to any folder.

Set path to zig (for example here version 0.13.0):

$Env:PATH+=";D:\tools\zig-windows-x86_64-0.13.0"

Then build or publish the app:

dotnet publish -r linux-arm64 /p:StripSymbols=false

To enable stripping symbols follow the documentation of the PublishAotCross package.

Copy files to the rasberry pi, for example using scp:

scp -r DD:\repos\..\WebApplication\bin\Release\net9.0\linux-arm64\publish\* <user>@raspberrypi:/home/<user>/app

Using chmod make the application executable, then execute it. Use sudo when binding to port 80 in case of ASP.NET Core:

sudo WebApplication --urls http://+:80