.NET Core 2.1 SDKをRaspberry Pi 3で動かしてみた

.NET Core SDKRaspberry Piに対応したようなので試してみました。

blogs.msdn.microsoft.com

ラズパイとOS

サポートしているのはRaspberry Pi 2以降で、ARMv6を使ったRaspberry Pi ZeroシリーズはNG。

また、Release NotesにあるSupported OS versionsのLinuxを見ると、Debian 9 / Ubuntu 18.04 のようです。

ARM32 support starts with Debian 9 and Ubuntu 18.04.

なので、

  • Raspberry Pi3 Model B
  • Ubuntu 18.04

でやってみることにしました。

OSインストール

ラズパイ向けのUbuntu 18.04イメージはUbuntu公式からは配布されていないようです。orz
ここに手間をかけたくないなぁ、、、ということで探し回ってみたところ、ここにありました。感謝。

SDFormatterで削除したマイクロSDに、Win32DiskImagerでubuntu-18.04-preinstalled-server-armhf+raspi3.img.xzを書き込んで、ラズパイで起動します。

起動時にサービスがFailしているような、、、気がしましたが、見なかったことにw

無事、OSが動きだしました。

ubuntu@ubuntu:~$ uname -a
Linux ubuntu 4.15.0-1010-raspi2 #11-Ubuntu SMP PREEMPT Tue Apr 24 06:27:17 UTC 2018 armv7l armv7l armv7l GNU/Linux
ubuntu@ubuntu:~$

.NET Core SDKインストール

Installing .NET Core on LinuxのUbuntu 18.04のとおりコマンドを実行します。

ubuntu@ubuntu:~$ wget -qO- https://packages.microsoft.com/keys/microsoft.asc | gpg --dearmor > microsoft.asc.gpg
ubuntu@ubuntu:~$ sudo mv microsoft.asc.gpg /etc/apt/trusted.gpg.d/
ubuntu@ubuntu:~$ wget -q https://packages.microsoft.com/config/ubuntu/18.04/prod.list
ubuntu@ubuntu:~$ sudo mv prod.list /etc/apt/sources.list.d/microsoft-prod.list
ubuntu@ubuntu:~$
ubuntu@ubuntu:~$ sudo apt-get install apt-transport-https
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following NEW packages will be installed:
  apt-transport-https
0 upgraded, 1 newly installed, 0 to remove and 81 not upgraded.
Need to get 1692 B of archives.
After this operation, 152 kB of additional disk space will be used.
Get:1 http://ports.ubuntu.com/ubuntu-ports bionic/universe armhf apt-transport-https all 1.6.1 [1692 B]
Fetched 1692 B in 1s (2880 B/s)
Selecting previously unselected package apt-transport-https.
(Reading database ... 61793 files and directories currently installed.)
Preparing to unpack .../apt-transport-https_1.6.1_all.deb ...
Unpacking apt-transport-https (1.6.1) ...
Setting up apt-transport-https (1.6.1) ...
ubuntu@ubuntu:~$ sudo apt-get update
Get:1 https://packages.microsoft.com/ubuntu/18.04/prod bionic InRelease [2846 B]
Hit:2 http://ppa.launchpad.net/ubuntu-raspi2/ppa-rpi3/ubuntu bionic InRelease
Hit:3 http://ports.ubuntu.com/ubuntu-ports bionic InRelease
Hit:4 http://ports.ubuntu.com/ubuntu-ports bionic-updates InRelease
Get:5 https://packages.microsoft.com/ubuntu/18.04/prod bionic/main amd64 Packages [10.1 kB]
Hit:6 http://ppa.launchpad.net/ubuntu-raspi2/ppa/ubuntu bionic InRelease
Hit:7 http://ports.ubuntu.com/ubuntu-ports bionic-backports InRelease
Hit:8 http://ports.ubuntu.com/ubuntu-ports bionic-security InRelease
Fetched 13.0 kB in 5s (2569 B/s)
Reading package lists... Done
ubuntu@ubuntu:~$ sudo apt-get install dotnet-sdk-2.1
Reading package lists... Done
Building dependency tree
Reading state information... Done
E: Unable to locate package dotnet-sdk-2.1
E: Couldn't find any package by glob 'dotnet-sdk-2.1'
E: Couldn't find any package by regex 'dotnet-sdk-2.1'
ubuntu@ubuntu:~$

E: Unable to locate package dotnet-sdk-2.1と、dotnet-sdk-2.1が見つからないエラーになってしまいました。

ログを見ると気になる表記が。

Get:5 https://packages.microsoft.com/ubuntu/18.04/prod bionic/main amd64 Packages [10.1 kB]

おや?

cat /etc/apt/sources.list.d/microsoft-prod.list
deb [arch=amd64] https://packages.microsoft.com/ubuntu/18.04/prod bionic main

おやおや??

仕組みを理解しないまま、漁ってみるが、、、

f:id:matsujirushix:20180617110406p:plain

f:id:matsujirushix:20180617110518p:plain

arm向けのパッケージが見つからない。

どうやら、リポジトリにアップされていないようなので、、、
バイナリをダウンロードして、Installing .NET Core on LinuxのInstallation from a binary archiveのとおり、解凍とパス設定しました。

ubuntu@ubuntu:~$ wget https://download.microsoft.com/download/8/8/5/88544F33-836A-49A5-8B67-451C24709A8F/dotnet-sdk-2.1.300-linux-arm.tar.gz
ubuntu@ubuntu:~$ mkdir -p $HOME/dotnet && tar zxf dotnet-sdk-2.1.300-linux-arm.tar.gz -C $HOME/dotnet
ubuntu@ubuntu:~$ export PATH=$PATH:$HOME/dotnet

dotnetコマンドが使えるか確認。

ubuntu@ubuntu:~$ dotnet

Usage: dotnet [options]
Usage: dotnet [path-to-application]

Options:
  -h|--help         Display help.
  --info            Display .NET Core information.
  --list-sdks       Display the installed SDKs.
  --list-runtimes   Display the installed runtimes.

path-to-application:
  The path to an application .dll file to execute.
ubuntu@ubuntu:~$ cd
ubuntu@ubuntu:~$ dotnet

Usage: dotnet [options]
Usage: dotnet [path-to-application]

Options:
  -h|--help         Display help.
  --info            Display .NET Core information.
  --list-sdks       Display the installed SDKs.
  --list-runtimes   Display the installed runtimes.

path-to-application:
  The path to an application .dll file to execute.
ubuntu@ubuntu:~$

サンプルプログラムを実行してみる

ここ2. Create your app3. Run your appをやってみます。

ubuntu@ubuntu:~$ dotnet new console -o myApp
The template "Console Application" was created successfully.

Processing post-creation actions...
Running 'dotnet restore' on myApp/myApp.csproj...
  Restoring packages for /home/ubuntu/myApp/myApp.csproj...
  Installing Microsoft.NETCore.DotNetAppHost 2.1.0.
  Installing Microsoft.NETCore.DotNetHostResolver 2.1.0.
  Installing Microsoft.NETCore.DotNetHostPolicy 2.1.0.
  Installing NETStandard.Library 2.0.3.
  Installing Microsoft.NETCore.Targets 2.1.0.
  Installing Microsoft.NETCore.Platforms 2.1.0.
  Installing Microsoft.NETCore.App 2.1.0.
  Generating MSBuild file /home/ubuntu/myApp/obj/myApp.csproj.nuget.g.props.
  Generating MSBuild file /home/ubuntu/myApp/obj/myApp.csproj.nuget.g.targets.
  Restore completed in 15.86 sec for /home/ubuntu/myApp/myApp.csproj.

Restore succeeded.

ubuntu@ubuntu:~$ cd myApp
ubuntu@ubuntu:~/myApp$ cat Program.cs
using System;

namespace myApp
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");
        }
    }
}
ubuntu@ubuntu:~/myApp$ dotnet run
Hello World!
ubuntu@ubuntu:~/myApp$

おぉー
動いた♪

動いたけど、、、起動するまでに結構時間かかるなぁw