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 を使う場合は次のようにします。
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 ) |
wget https://packages.microsoft.com/config/ubuntu/$repo_version/packages-microsoft-prod.deb -O packages-microsoft-prod.deb |
sudo dpkg -i packages-microsoft-prod.deb |
rm packages-microsoft-prod.deb |
ここまでドキュメントの通りですが、同名で Ubuntu PPA のパッケージがあるため、Microsoft PPA を優先させるために次のようにします。
これをしないと Ubuntu PPA 版のアップデートが起きた場合に上書きされて、dotnet
コマンドは効けど SDK が見えなくなったりします。
(不具合が発生している場合は一度 dotnet*
package を削除:)
Microsoft PPA を優先設定:
sudo sh -c " cat > /etc/apt/preferences.d/dotnet << 'EOF' |
Pin: origin packages.microsoft.com |
sudo sh -c " cat > /etc/apt/preferences.d/aspnet << 'EOF' |
Pin: origin packages.microsoft.com |
dot-net-sdk-7.0
を導入:
sudo apt install dotnet-sdk-7.0 |
これで dotnet --info
が次のようになれば OK です。
Base Path: /usr/share/dotnet/sdk/7.0.302/ |
7.0.302 [/usr/share/dotnet/sdk] |
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: |
DOTNET_ROOT [/usr/lib/dotnet] |
https://aka.ms/dotnet/info |
https://aka.ms/dotnet/download |
ハローワールド:
$ mkdir hello && cd hello |
テンプレート "コンソール アプリ" が正常に作成されました。 |
/home/hiromasa/devel/dotnet/hello/hello.csproj を復元しています: |
/home/hiromasa/devel/dotnet/hello/hello.csproj を復元しました (108 ms)。 |
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/ |
$ 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 でコーディングしてデバッグブレイクしている様子: