山pの楽しいお勉強生活

勉強の成果を垂れ流していきます

WSLのUbuntuを 20.04 から 24.04 に移行したメモ

タイトル
## はじめに

WSLでUbuntuの20.04を使用していましたが、標準サポートが2025/04までということなので24.04に入れ替えます。 バージョンアップもできるようですが、ほぼ開発環境としか使っていないのでどうせなら新規作成してデータを移し替える方法を選びました。

一応 ChatGPT 4oに聞いてみたら以下の回答が返ってきました。 が、裏は取っていないので真偽は不明です。

WSLでは「そのままUbuntuのバージョンアップをする方法(do-release-upgrade)」も一応可能ですが、非推奨です。

なぜバージョンアップは非推奨か
WSLのUbuntuは「軽量・仮想環境」として設計されており、do-release-upgrade は公式に完全なサポート対象ではありません。

WSL特有の設定(systemd まわりや init、ネットワーク)などが壊れることがあります。

WSLの新規インストールは非常に軽いため、「まっさらな22.04や24.04をインストール」→「必要なデータだけ移す」方が安全で確実です。

WSLにUbuntu 24.04をインストール

C:\Users\yamap>wsl -l
Linux 用 Windows サブシステム ディストリビューション:
Ubuntu-20.04 (既定)
docker-desktop
  • Ubuntu 24.04をインストール
C:\Users\yamap>wsl --install -d Ubuntu-24.04
ダウンロード中: Ubuntu 24.04 LTS
インストール中: Ubuntu 24.04 LTS
ディストリビューションが正常にインストールされました。'wsl.exe -d Ubuntu-24.04' を使用して起動できます
  • インストールされていることを確認
C:\Users\yamap>wsl -l
Linux 用 Windows サブシステム ディストリビューション:
Ubuntu-20.04 (既定)
Ubuntu-24.04
docker-desktop

Ubuntu 24.04の設定

  • Ubuntu 24.04に接続
    • ユーザー名とかパスワードを聞かれるので設定
C:\Users\yamap>wsl -d Ubuntu-24.04
Provisioning the new WSL instance Ubuntu-24.04
This might take a while...
Create a default Unix user account: yamap
New password:
Retype new password:
passwd: password updated successfully
To run a command as administrator (user "root"), use "sudo <command>".
See "man sudo_root" for details.

Welcome to Ubuntu 24.04.2 LTS (GNU/Linux 5.15.167.4-microsoft-standard-WSL2 x86_64)

 * Documentation:  https://help.ubuntu.com
 * Management:     https://landscape.canonical.com
 * Support:        https://ubuntu.com/pro

 System information as of Sat May 10 10:34:33 JST 2025

  System load:  0.13                Processes:             34
  Usage of /:   0.1% of 1006.85GB   Users logged in:       0
  Memory usage: 26%                 IPv4 address for eth0: 192.168.61.63
  Swap usage:   0%


This message is shown once a day. To disable it please create the
/home/yamap/.hushlogin file.
yamap@yamap-thinkpad:/mnt/c/Users/yamap$ 
  • ホーム
yamap@yamap-thinkpad:/mnt/c/Users/yamap$ cd ~
yamap@yamap-thinkpad:~$ ls -la
total 28
drwxr-x--- 4 yamap yamap 4096 May 10 10:34 .
drwxr-xr-x 3 root  root  4096 May 10 10:34 ..
-rw-r--r-- 1 yamap yamap  220 May 10 10:34 .bash_logout
-rw-r--r-- 1 yamap yamap 3771 May 10 10:34 .bashrc
drwx------ 2 yamap yamap 4096 May 10 10:34 .cache
drwxr-xr-x 2 yamap yamap 4096 May 10 10:34 .landscape
-rw-r--r-- 1 yamap yamap    0 May 10 10:34 .motd_shown
-rw-r--r-- 1 yamap yamap  807 May 10 10:34 .profile
  • インストール済みの各種ツールをアップデート
yamap@yamap-thinkpad:~$ sudo apt update
[sudo] password for yamap:
Hit:1 http://archive.ubuntu.com/ubuntu noble InRelease
Get:2 http://security.ubuntu.com/ubuntu noble-security InRelease [126 kB]
Get:3 http://archive.ubuntu.com/ubuntu noble-updates InRelease [126 kB]
Get:4 http://archive.ubuntu.com/ubuntu noble-backports InRelease [126 kB]
Get:5 http://security.ubuntu.com/ubuntu noble-security/main amd64 Packages [820 kB]
Get:6 http://archive.ubuntu.com/ubuntu noble/universe amd64 Packages [15.0 MB]
(省略)
Get:55 http://archive.ubuntu.com/ubuntu noble-backports/multiverse amd64 Components [212 B]
Get:56 http://archive.ubuntu.com/ubuntu noble-backports/multiverse amd64 c-n-f Metadata [116 B]
Fetched 34.0 MB in 6s (6158 kB/s)
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
99 packages can be upgraded. Run 'apt list --upgradable' to see them.
yamap@yamap-thinkpad:~$ sudo apt upgrade -y
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
Calculating upgrade... Done
The following packages will be upgraded:
(省略)
Processing triggers for libglib2.0-0t64:amd64 (2.80.0-6ubuntu3.2) ...
Processing triggers for dbus (1.14.10-4ubuntu4.1) ...
Setting up libgtk-3-0t64:amd64 (3.24.41-4ubuntu1.3) ...
Setting up libgtk-3-bin (3.24.41-4ubuntu1.3) ...
Processing triggers for libc-bin (2.39-0ubuntu8.4) ...
yamap@yamap-thinkpad:~$
yamap:~ $ sudo dpkg-reconfigure locales
[sudo] password for yamap:
Generating locales (this might take a while)...
  ja_JP.UTF-8... done

ja_JP.UTF-8 UTF-8 をspaceで選択してOK

  • デフォルトエディタをvimに変更
yamap@yamap-thinkpad:~$ sudo update-alternatives --config editor
There are 4 choices for the alternative editor (providing /usr/bin/editor).

  Selection    Path                Priority   Status
------------------------------------------------------------
* 0            /bin/nano            40        auto mode
  1            /bin/ed             -100       manual mode
  2            /bin/nano            40        manual mode
  3            /usr/bin/vim.basic   30        manual mode
  4            /usr/bin/vim.tiny    15        manual mode

Press <enter> to keep the current choice[*], or type selection number: 3
update-alternatives: using /usr/bin/vim.basic to provide /usr/bin/editor (editor) in manual mode

.bashrc の更新

wget --progress=dot:giga https://raw.githubusercontent.com/git/git/master/contrib/completion/git-completion.bash -O ~/.git-completion.bash
wget --progress=dot:giga https://raw.githubusercontent.com/git/git/master/contrib/completion/git-prompt.sh -O ~/.git-prompt.sh
chmod a+x ~/.git-completion.bash
chmod a+x ~/.git-prompt.sh

※Gitの各種情報表示のためのシェル

vi ~/.bashrc
source ~/.git-completion.bash
source ~/.git-prompt.sh
export PS1='\[\033[1;30m\]\t\[\033[0m\] \[\e]0;\u@\h: \w\a\]${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\[\033[1;30m\]$(__git_ps1)\[\033[0m\] \$ '

function clip() {
  cat - | iconv -t utf-16le | "/mnt/c/Windows/system32/clip.exe"
}
export LANG=ja_JP.UTF-8
export LC_TIME=ja_JP.UTF-8

※ターミナルにGit関連情報の表示したり色付けたり、クリップボードで絵文字の文字化けを修正したり、日本語の設定とか

必要なものをインストール

※Docker内で作業することがほとんどなので少ない。。。

sudo apt update
sudo apt install -y python3 python3-pip python3-venv python-is-python3

※2025/05/10の段階だと3.12.3がインストールされる

  • nvm
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.40.3/install.sh | bash

DNS設定変更

WSLではネットワークによってはDNSが正しく機能しないことがあるため、手動で安定した設定にする

※2025/05/10の時点では有効ではあるが将来はわからない WSLのIssue に最新情報が書き込まれる可能性が高いので参照すること

  • DNS設定の自動生成を停止
    • 設定ファイルを開く( sudo vi /etc/wsl.conf
    • 以下を記載
[network]
generateResolvConf = false
  • DNSを手動設定
    • 自動生成された /etc/resolv.conf を削除
    • 設定ファイルを作成( sudo vi /etc/resolv.conf
    • 以下を記載
nameserver 8.8.8.8

※8.8.8.8 は Google Public DNSサーバー (Public DNS  |  Google for Developers

必要なデータを移行

cd ~
tar czf /mnt/c/Users/yamap/backup-settings.tar.gz .bash_profile .gitconfig .ssh
tar xzf /mnt/c/Users/yamap/backup-settings.tar.gz

WSLのデフォルトを変更

ホストのコマンドプロンプトで以下を実行

  • 設定
wsl --set-default Ubuntu-24.04
  • 確認
wsl -l

Docker Desktopの設定変更

dockerコマンドをWSL内で使用するためにホストのDocker Desktop側で設定変更する

  • 設定 → Resources → WSL Integration → 「Ubuntu-24.04」をON → Apply & restart

Docker Desktopの設定変更