⬇️

.NET 8.0 を WSL2 Ubuntu にインストールする

2023/12/19に公開

まえがき

.NET 8.0 を Ubuntu 22.04 にインストールしようとしたところ、パッケージマネージャが8.0にまだ対応していない[1]ので、Microsoft パッケージ リポジトリを参照させる方法、またはMicrosoftで用意されているインストールスクリプトを使用してインストールします。

【追記】
最初はインストールスクリプトを使用したインストール方法でやっていたんですが、VSCodeが認識しなかったりしたので、Microsoft パッケージ リポジトリを追加する方法で再インストールしたら、VSCodeも認識するようになったので、一応やり方を追記します。
いろいろインストールしているので、すんなりいく人は行くと思います😫

環境

WSL Ubuntu 22.04

事前準備

Ubuntu のバージョン確認

$ cat /etc/issue
Ubuntu 22.04.3 LTS \n \l

以前の.NET のアンインストール

$ dotnet --version
7.0.114
$ sudo apt-get remove dotnet-sdk-7.0
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following packages were automatically installed and are no longer required:
  aspnetcore-runtime-7.0 aspnetcore-targeting-pack-7.0 dotnet-apphost-pack-7.0 dotnet-host-7.0 dotnet-hostfxr-7.0 dotnet-runtime-7.0 dotnet-targeting-pack-7.0 dotnet-templates-7.0
  liblttng-ust-common1 liblttng-ust-ctl5 liblttng-ust1 netstandard-targeting-pack-2.1-7.0
Use 'sudo apt autoremove' to remove them.
The following packages will be REMOVED:
  dotnet-sdk-7.0
0 upgraded, 0 newly installed, 1 to remove and 53 not upgraded.
After this operation, 312 MB disk space will be freed.
Do you want to continue? [Y/n] y
(Reading database ... 97199 files and directories currently installed.)
Removing dotnet-sdk-7.0 (7.0.114-0ubuntu1~22.04.1) ...

必要なライブラリのインストール

$ sudo apt install libc6 &&
sudo apt install libgcc1 &&
sudo apt install libgcc-s1 &&
sudo apt install libgssapi-krb5-2 &&
sudo apt install libicu70 &&
sudo apt install liblttng-ust1 &&
sudo apt install libssl3 &&
sudo apt install libstdc++6 &&
sudo apt install libunwind8 &&
sudo apt install zlib1g
# 必要があれば掃除
$ sudo apt autoremove

Microsoft パッケージ リポジトリ を参照するインストール

Microsoft パッケージ リポジトリには、ご利用のバージョンの Ubuntu で現在サポートされている、または以前サポートされていたすべての .NET バージョンが含まれています。 ご利用のバージョンの Ubuntu で .NET パッケージが提供されている場合は、Ubuntu パッケージの優先順位を下げて、Microsoft リポジトリを使用する必要があります。 パッケージの優先順位を下げる方法については、「Linux ディストリビューションで提供されていないバージョンの .NET が必要です」をご覧ください。

Ubuntuのパッケージリポジトリに追加されたら、こっちのリポジトリは参照しないようにするか、優先度を下げたりする必要があるようです。たぶん。

Microsoft パッケージ リポジトリを登録する

手順は以下に書いてあるコマンドを実行する感じ
https://learn.microsoft.com/ja-jp/dotnet/core/install/linux-ubuntu#register-the-microsoft-package-repository

$ curl -o packages-microsoft-prod.deb https://packages.microsoft.com/config/ubuntu/22.04/packages-microsoft-prod.deb
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  3692  100  3692    0     0  13541      0 --:--:-- --:--:-- --:--:-- 13573

$ sudo dpkg -i packages-microsoft-prod.deb
(Reading database ... 93766 files and directories currently installed.)
Preparing to unpack packages-microsoft-prod.deb ...
Unpacking packages-microsoft-prod (1.0-ubuntu22.04.1) over (1.0-ubuntu20.04.1) ...
Setting up packages-microsoft-prod (1.0-ubuntu22.04.1) ...

Configuration file '/etc/apt/sources.list.d/microsoft-prod.list'
 ==> Deleted (by you or by a script) since installation.
 ==> Package distributor has shipped an updated version.
   What would you like to do about it ?  Your options are:
    Y or I  : install the package maintainer's version
    N or O  : keep your currently-installed version
      D     : show the differences between the versions
      Z     : start a shell to examine the situation
 The default action is to keep your current version.
*** microsoft-prod.list (Y/I/N/O/D/Z) [default=N] ? y
Installing new version of config file /etc/apt/sources.list.d/microsoft-prod.list ...

$ rm packages-microsoft-prod.deb
$ sudo apt update

インストール

$ sudo apt install dotnet-sdk-8.0
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following additional packages will be installed:
  aspnetcore-runtime-8.0 aspnetcore-targeting-pack-8.0 dotnet-apphost-pack-8.0 dotnet-host dotnet-hostfxr-8.0
  dotnet-runtime-8.0 dotnet-runtime-deps-8.0 dotnet-targeting-pack-8.0 netstandard-targeting-pack-2.1
The following NEW packages will be installed:
  aspnetcore-runtime-8.0 aspnetcore-targeting-pack-8.0 dotnet-apphost-pack-8.0 dotnet-host dotnet-hostfxr-8.0
  dotnet-runtime-8.0 dotnet-runtime-deps-8.0 dotnet-sdk-8.0 dotnet-targeting-pack-8.0 netstandard-targeting-pack-2.1
0 upgraded, 10 newly installed, 0 to remove and 51 not upgraded.
Need to get 143 MB of archives.
After this operation, 580 MB of additional disk space will be used.
Do you want to continue? [Y/n] y
Get:1 https://packages.microsoft.com/ubuntu/22.04/prod jammy/main amd64 dotnet-host amd64 8.0.0-1 [37.7 kB]
Get:2 http://archive.ubuntu.com/ubuntu jammy-updates/universe amd64 netstandard-targeting-pack-2.1 amd64 6.0.125-0ubuntu1~22.04.1 [1398 kB]
Get:3 https://packages.microsoft.com/ubuntu/22.04/prod jammy/main amd64 dotnet-hostfxr-8.0 amd64 8.0.0-1 [108 kB]
Get:4 https://packages.microsoft.com/ubuntu/22.04/prod jammy/main amd64 dotnet-runtime-deps-8.0 amd64 8.0.0-1 [2890 B]
Get:5 https://packages.microsoft.com/ubuntu/22.04/prod jammy/main amd64 dotnet-runtime-8.0 amd64 8.0.0-1 [23.1 MB]
Get:6 https://packages.microsoft.com/ubuntu/22.04/prod jammy/main amd64 aspnetcore-runtime-8.0 amd64 8.0.0-1 [7658 kB]
Get:7 https://packages.microsoft.com/ubuntu/22.04/prod jammy/main amd64 dotnet-targeting-pack-8.0 amd64 8.0.0-1 [2801 kB]
Get:8 https://packages.microsoft.com/ubuntu/22.04/prod jammy/main amd64 aspnetcore-targeting-pack-8.0 amd64 8.0.0-1 [1906 kB]
Get:9 https://packages.microsoft.com/ubuntu/22.04/prod jammy/main amd64 dotnet-apphost-pack-8.0 amd64 8.0.0-1 [3508 kB]
Get:10 https://packages.microsoft.com/ubuntu/22.04/prod jammy/main amd64 dotnet-sdk-8.0 amd64 8.0.100-1 [102 MB]
Fetched 143 MB in 4s (34.5 MB/s)
Selecting previously unselected package dotnet-host.
(Reading database ... 93766 files and directories currently installed.)
Preparing to unpack .../0-dotnet-host_8.0.0-1_amd64.deb ...
Unpacking dotnet-host (8.0.0-1) ...
Selecting previously unselected package dotnet-hostfxr-8.0.
Preparing to unpack .../1-dotnet-hostfxr-8.0_8.0.0-1_amd64.deb ...
Unpacking dotnet-hostfxr-8.0 (8.0.0-1) ...
Selecting previously unselected package dotnet-runtime-deps-8.0.
Preparing to unpack .../2-dotnet-runtime-deps-8.0_8.0.0-1_amd64.deb ...
Unpacking dotnet-runtime-deps-8.0 (8.0.0-1) ...
Selecting previously unselected package dotnet-runtime-8.0.
Preparing to unpack .../3-dotnet-runtime-8.0_8.0.0-1_amd64.deb ...
Unpacking dotnet-runtime-8.0 (8.0.0-1) ...
Selecting previously unselected package aspnetcore-runtime-8.0.
Preparing to unpack .../4-aspnetcore-runtime-8.0_8.0.0-1_amd64.deb ...
Unpacking aspnetcore-runtime-8.0 (8.0.0-1) ...
Selecting previously unselected package dotnet-targeting-pack-8.0.
Preparing to unpack .../5-dotnet-targeting-pack-8.0_8.0.0-1_amd64.deb ...
Unpacking dotnet-targeting-pack-8.0 (8.0.0-1) ...
Selecting previously unselected package aspnetcore-targeting-pack-8.0.
Preparing to unpack .../6-aspnetcore-targeting-pack-8.0_8.0.0-1_amd64.deb ...
Unpacking aspnetcore-targeting-pack-8.0 (8.0.0-1) ...
Selecting previously unselected package dotnet-apphost-pack-8.0.
Preparing to unpack .../7-dotnet-apphost-pack-8.0_8.0.0-1_amd64.deb ...
Unpacking dotnet-apphost-pack-8.0 (8.0.0-1) ...
Selecting previously unselected package netstandard-targeting-pack-2.1.
Preparing to unpack .../8-netstandard-targeting-pack-2.1_6.0.125-0ubuntu1~22.04.1_amd64.deb ...
Unpacking netstandard-targeting-pack-2.1 (6.0.125-0ubuntu1~22.04.1) ...
Selecting previously unselected package dotnet-sdk-8.0.
Preparing to unpack .../9-dotnet-sdk-8.0_8.0.100-1_amd64.deb ...
Unpacking dotnet-sdk-8.0 (8.0.100-1) ...
Setting up dotnet-host (8.0.0-1) ...
Setting up dotnet-targeting-pack-8.0 (8.0.0-1) ...
Setting up netstandard-targeting-pack-2.1 (6.0.125-0ubuntu1~22.04.1) ...
Setting up aspnetcore-targeting-pack-8.0 (8.0.0-1) ...
Setting up dotnet-apphost-pack-8.0 (8.0.0-1) ...
Setting up dotnet-runtime-deps-8.0 (8.0.0-1) ...
Setting up dotnet-hostfxr-8.0 (8.0.0-1) ...
Setting up dotnet-runtime-8.0 (8.0.0-1) ...
Setting up aspnetcore-runtime-8.0 (8.0.0-1) ...
Setting up dotnet-sdk-8.0 (8.0.100-1) ...

$ dotnet --version
8.0.100

できました。

スクリプトを使用したインストール

手順としては以下にある通りです。
https://learn.microsoft.com/ja-jp/dotnet/core/install/linux-scripted-manual#scripted-install

wget を使用してスクリプトをダウンロード

$ wget https://dot.net/v1/dotnet-install.sh -O dotnet-install.sh
--2023-12-19 11:39:20--  https://dot.net/v1/dotnet-install.sh
Resolving dot.net (dot.net)... 20.76.201.171, 20.112.250.133, 20.236.44.162, ...
Connecting to dot.net (dot.net)|20.76.201.171|:443... connected.
HTTP request sent, awaiting response... 301 Moved Permanently
Location: https://dotnet.microsoft.com/download/dotnet/scripts/v1/dotnet-install.sh [following]
--2023-12-19 11:39:21--  https://dotnet.microsoft.com/download/dotnet/scripts/v1/dotnet-install.sh
Resolving dotnet.microsoft.com (dotnet.microsoft.com)... 13.107.253.46, 13.107.226.46, 2620:1ec:29:1::46, ...
Connecting to dotnet.microsoft.com (dotnet.microsoft.com)|13.107.253.46|:443... connected.
HTTP request sent, awaiting response... 200 OK
Cookie coming from dotnet.microsoft.com attempted to set domain to dotnetwebsite.azurewebsites.net
Cookie coming from dotnet.microsoft.com attempted to set domain to dotnetwebsite.azurewebsites.net
Length: 61896 (60K) [application/x-sh]
Saving to: ‘dotnet-install.sh’

dotnet-install.sh                                 100%[===========================================================================================================>]  60.45K   276KB/s    in 0.2s    

2023-12-19 11:39:22 (276 KB/s) - ‘dotnet-install.sh’ saved [61896/61896]

実行権限をつける

$ chmod +x ./dotnet-install.sh

インストール

2023/12/19 時点では最新版は8系なので、latestでインストールします。

$ ./dotnet-install.sh --version latest
dotnet-install: Attempting to download using aka.ms link https://dotnetcli.azureedge.net/dotnet/Sdk/8.0.100/dotnet-sdk-8.0.100-linux-x64.tar.gz
dotnet-install: Remote file https://dotnetcli.azureedge.net/dotnet/Sdk/8.0.100/dotnet-sdk-8.0.100-linux-x64.tar.gz size is 214395068 bytes.
dotnet-install: Extracting zip from https://dotnetcli.azureedge.net/dotnet/Sdk/8.0.100/dotnet-sdk-8.0.100-linux-x64.tar.gz
dotnet-install: Downloaded file size is 214395068 bytes.
dotnet-install: The remote and local file sizes are equal.
dotnet-install: Installed version is 8.0.100
dotnet-install: Adding to current process PATH: `/home/********/.dotnet`. Note: This change will be visible only when sourcing script.
dotnet-install: Note that the script does not resolve dependencies during installation.
dotnet-install: To check the list of dependencies, go to https://learn.microsoft.com/dotnet/core/install, select your operating system and check the "Dependencies" section.
dotnet-install: Installation finished successfully.

環境変数をシステム全体に設定する

このままだと dotnet cli のパスが通ってないので、~/.bashrc に パスを通します。

$ vi ~/.bashrc

末尾に追加

~/.bashrc
export DOTNET_ROOT=$HOME/.dotnet
export PATH=$PATH:$DOTNET_ROOT:$DOTNET_ROOT/tools

反映

$ source ~/.bashrc

確認

$ dotnet --version
8.0.100
脚注
  1. https://learn.microsoft.com/ja-jp/dotnet/core/install/linux-ubuntu-2204#supported-versions ↩︎

Discussion