Skip to main content

Ubuntu-24.04

前提条件
  • Windows Terminalをインストール済
  • WSLのverが1.0.0以上
  • ユーザー名は任意の名前でOK(以下ではユーザー名をgomaとしてます)

インストール前の準備

wsl関連のフォルダ作成

info

wsl関連のフォルダ作成済の場合、以下手順は不要です。

wsl関連のフォルダ作成

以下コマンドを実施します。

powershell
New-Item $env:USERPROFILE/winenv/wsl/exe/ubuntu2404 -ItemType Directory; New-Item $env:USERPROFILE/winenv/wsl/ubuntu2404 -ItemType Directory

インストール

Ubuntu-24.04をインストール

tip

wslディストリビューションがインストール済の場合は以下コマンドで事前に削除します。

powershell
wsl --unregister Ubuntu-24.04

インストールコマンドを実施します。
(インストールコマンドを実行すると、Ubuntu 24.04 LTSのインストール&ダウンロードを実施します)

powershell
wsl --install Ubuntu-24.04

インストールコマンド実施後、以下コマンドを実行後します。

powershell
wsl -d Ubuntu-24.04

ユーザー名パスワードを入力します。

Provisioning the new WSL instance Ubuntu-24.04
This might take a while...
Create a default Unix user account: goma # ユーザー名を入力
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.1 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 Sun Feb 16 17:39:12 JST 2025

System load: 0.31 Processes: 31
Usage of /: 0.1% of 1006.85GB Users logged in: 0
Memory usage: 20% IPv4 address for eth0: 172.29.153.29
Swap usage: 0%


This message is shown once a day. To disable it please create the
/home/goma/.hushlogin file.
appxをダウンロード & 解凍 & Ubuntu起動(wsl --installが上手く行かない場合)

Ubuntu2404-240425.AppxBundleを以下コマンドで$env:USERPROFILE/winenv/wsl/exeにダウンロードします。

powershell
Invoke-WebRequest -Uri https://wslstorestorage.blob.core.windows.net/wslblob/Ubuntu2404-240425.AppxBundle -OutFile $env:USERPROFILE/winenv/wsl/exe

Ubuntu2404-240425.AppxBundleを以下コマンドで解凍します。

powershell
Expand-Archive -Path $env:USERPROFILE/winenv/wsl/exe/Ubuntu2404-240425.AppxBundle -DestinationPath $env:USERPROFILE/winenv/wsl/exe/Ubuntu2404-240425

Ubuntu_2404.0.5.0_x64.appxを以下コマンドでに解凍します。

powershell
Expand-Archive -Path $env:USERPROFILE/winenv/wsl/exe/Ubuntu2404-240425/Ubuntu_2404.0.5.0_x64.appx -DestinationPath $env:USERPROFILE/winenv/wsl/exe/ubuntu2404

ubuntu2404.exeを起動します。

powershell
.$env:USERPROFILE\winenv\wsl\exe\ubuntu2404\ubuntu2404.exe

ubuntu2404.exeを起動後、ユーザー名パスワードを入力します。

wsl.confの設定

以下を実行します。
ユーザーパスワードの入力が必要です

[user] - defaultはリストア後にrootになる問題の対策
[interop] - appendWindowsPathはWSLのコマンド保管が遅い事象の対策
[boot] - systemdはWSLでsystemdを使用するのに必要な設定

USERNAME=`whoami` && cat << EOS | sudo tee /etc/wsl.conf
[user]
default=$USERNAME
[interop]
appendWindowsPath=false
[boot]
systemd=true
EOS

wslディストリビューションを抜けます。

exit

wslディストリビューションを停止します。
(systemdを適用するためにはwslを停止(再起動)する必要があります)

powershell
wsl -t Ubuntu-24.04

ansibleインストール

wslにログインします(ユーザー名はgoma)

powershell
wsl -d Ubuntu-24.04 -u goma --cd ~/

以下を実行します。
ユーザーパスワードの入力が必要です

  • pipをインストール
  • venvをインストール
  • venvを活性化(pipのver23.0からvenv上で実行しないとエラーとなるためです。)
  • ansibleをインストール(併せてdockerpasslibもインストール)
  • ~/.bashrcsource ~/.venv/bin/activateを追記
sudo apt update && \
sudo apt install -y python3-pip && \
sudo apt install -y python3-venv && \
source ~/.profile && \
python3 -m venv ~/.venv && \
source ~/.venv/bin/activate && \
pip install ansible && \
pip install docker && \
pip install passlib && \
echo "source ~/.venv/bin/activate " >> ~/.bashrc

バージョン確認を実施します

ansibleのバージョン確認
ansible-community --version
ansible-coreのバージョン確認
ansible --version

wslディストリビューションを抜けます

exit
エクスポート(ansibleインストール済)

エクスポート前にUbuntu-24.04ディストリビューションが起動している場合は停止させます

powershell
wsl -t Ubuntu-24.04

ansibleをインストール済のwslをエクスポートします

powershell
wsl --export Ubuntu-24.04 $env:USERPROFILE/winenv/wsl/ubuntu2404/ansible_test.tar
インポート(ansibleインストール済)
tip

wslディストリビューション(Ubuntu-24.04)がインストールされている場合は、アンインストールします

powershell
wsl --unregister Ubuntu-24.04

Ubuntu-24.04(ansible_test)をインポートします

powershell
wsl --import Ubuntu-24.04 $env:USERPROFILE/winenv/wsl/ubuntu2404/ $env:USERPROFILE/winenv/wsl/ubuntu2404/ansible_test.tar

playbook実行

Ubuntu-24.04を起動します

powershell
wsl -d Ubuntu-24.04 -u goma --cd ~/

setup-wslenvgit cloneで取得します

git clone https://github.com/gmnk616/setup-wslenv.git && cd ./setup-wslenv/ubuntu2404

プレイブック実行します
ユーザーパスワードの入力が必要です

ansible-playbook -i inventories/hosts.ini -l noble playbook.yaml --diff --ask-become-pass

プレイブック実行後、setup-wslenvを削除してwslから抜けます

rm -rf ~/setup-wslenv && exit

wsl再起動の為、wslを停止します

powershell
wsl -t Ubuntu-24.04

再度ログインします

powershell
wsl -d Ubuntu-24.04 -u goma --cd ~/

再度ログイン後、wslディストリビューションを抜けます

exit

エクスポート

Ubuntu-24.04をエクスポート

エクスポート前にUbuntu-24.04ディストリビューションが起動している場合は停止させます

powershell
wsl -t Ubuntu-24.04

プレイブック実行済のwslをエクスポートします

powershell
wsl --export Ubuntu-24.04 $env:USERPROFILE/winenv/wsl/ubuntu2404/ubuntu24_04.tar

インポート

Ubuntu-24.04をインポート

tip

wslディストリビューションがインストール済の場合は以下コマンドで事前に削除します。

powershell
wsl --unregister Ubuntu-24.04

Ubuntu-24.04をインポートします

powershell
wsl --import Ubuntu-24.04 $env:USERPROFILE/winenv/wsl/ubuntu2404/ $env:USERPROFILE/winenv/wsl/ubuntu2404/ubuntu24_04.tar

Ubuntu-24.04をデフォルトディストリビューションに設定します

powershell
wsl --set-default Ubuntu-24.04