Ubuntu 22.04 LTS で Microsoft PPA 版の dotnet-sdk-7.0 を導入するメモ

Ubuntu 上で .NET Core を使いたい場合は、Microsoft PPA から dotnet-sdk-7.0 パッケージを導入すると便利です。

Ubuntu に .NET SDK または .NET ランタイムをインストールする

この記事では、Ubuntu に .NET をインストールする方法について説明します。 Microsoft パッケージ リポジトリには、Ubuntu で現在または以前にサポートされていたすべてのバージョンの .NET が含まれています。 Ubuntu 22.04 以降では、Ubuntu パッケージ フィードで一部のバージョンの .NET を使用できます。 使用可能なバージョンの詳細については、「 サポートされているディストリビューション 」セクションを参照してください。

Ubuntu PPA 上にもパッケージが用意されていますが、Microsoft PPA を使う場合は次のようにします。

# Get Ubuntu version
declare repo_version=$(if command -v lsb_release &> /dev/null; then lsb_release -r -s; else grep -oP '(?<=^VERSION_ID=).+' /etc/os-release | tr -d '"'; fi)

# Download Microsoft signing key and repository
wget https://packages.microsoft.com/config/ubuntu/$repo_version/packages-microsoft-prod.deb -O packages-microsoft-prod.deb

# Install Microsoft signing key and repository
sudo dpkg -i packages-microsoft-prod.deb

# Clean up
rm packages-microsoft-prod.deb

# Update packages
sudo apt update

ここまでドキュメントの通りですが、同名で Ubuntu PPA のパッケージがあるため、Microsoft PPA を優先させるために次のようにします。

これをしないと Ubuntu PPA 版のアップデートが起きた場合に上書きされて、dotnet コマンドは効けど SDK が見えなくなったりします。

(不具合が発生している場合は一度 dotnet* package を削除:)

sudo apt purge dotnet*
sudo apt autoremove

Microsoft PPA を優先設定:

sudo sh -c "cat > /etc/apt/preferences.d/dotnet <<'EOF'
Package: dotnet*
Pin: origin packages.microsoft.com
Pin-Priority: 1001
EOF"
sudo sh -c "cat > /etc/apt/preferences.d/aspnet <<'EOF'
Package: aspnet*
Pin: origin packages.microsoft.com
Pin-Priority: 1001
EOF"

dot-net-sdk-7.0 を導入:

sudo apt update
sudo apt install dotnet-sdk-7.0

これで dotnet --info が次のようになれば OK です。

$ dotnet --info
.NET SDK:
 Version:   7.0.302
 Commit:    990cf98a27

ランタイム環境:
 OS Name:     ubuntu
 OS Version:  22.04
 OS Platform: Linux
 RID:         ubuntu.22.04-x64
 Base Path:   /usr/share/dotnet/sdk/7.0.302/

Host:
  Version:      7.0.5
  Architecture: x64
  Commit:       8042d61b17

.NET SDKs installed:
  7.0.302 [/usr/share/dotnet/sdk]

.NET runtimes installed:
  Microsoft.AspNetCore.App 7.0.5 [/usr/share/dotnet/shared/Microsoft.AspNetCore.App]
  Microsoft.NETCore.App 7.0.5 [/usr/share/dotnet/shared/Microsoft.NETCore.App]

Other architectures found:
  None

Environment variables:
  DOTNET_ROOT       [/usr/lib/dotnet]

global.json file:
  Not found

Learn more:
  https://aka.ms/dotnet/info

Download .NET:
  https://aka.ms/dotnet/download

ハローワールド:

$ mkdir hello && cd hello
$ dotnet new console
テンプレート "コンソール アプリ" が正常に作成されました。

作成後の操作を処理しています...
/home/hiromasa/devel/dotnet/hello/hello.csproj を復元しています:
  復元対象のプロジェクトを決定しています...
  /home/hiromasa/devel/dotnet/hello/hello.csproj を復元しました (108 ms)。
正常に復元されました。
$ ls -laF
合計 20
drwxrwxr-x 3 hiromasa hiromasa 4096  5月 28 19:00 ./
drwxrwxr-x 5 hiromasa hiromasa 4096  5月 28 18:59 ../
-rw-rw-r-- 1 hiromasa hiromasa  105  5月 28 19:00 Program.cs
-rw-rw-r-- 1 hiromasa hiromasa  249  5月 28 19:00 hello.csproj
drwxrwxr-x 2 hiromasa hiromasa 4096  5月 28 19:00 obj/
$ dotnet run
Hello, World!
$ file obj/Debug/net7.0/hello.dll
obj/Debug/net7.0/hello.dll: PE32 executable (console) Intel 80386 Mono/.Net assembly, for MS Windows

VS Code の C# Extention でコーディングしてデバッグブレイクしている様子:

コメントを残す