2
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Claude CodeのためのWSLイメージを作成し、サンドボックス環境として運用する方法

Posted at

はじめに

Claude Codeがギョーカイを席巻しています。
でも、Claude CodeはWindowsに対応していないそうです。差別か?MSが嫌いなのか?
しょうがないのでWSLに入れるべきなんですが、WSLはいろいろとクセが強く、Microsoft Learnの言うことを聞きながらLinuxを触るという謎の体験をしなければならないため、標準的な環境の整え方を紹介します。

前提

以下の環境での導入を行います

Windows: 11 Pro 24H2
CPU: x64

WSLがインストールされていない場合、Microsoft Learnを参照してインストールしてください。

WSL2導入手順

WSLがすでに入っていて、ubuntu 24.04LTSのイメージがすでに入っていることが多いかと思いますが、後々並列化するときに重たいイメージを取り扱うのは嫌なので、新しいディストリを作成します。

まず、wslを最新の状態にします。

wsl --update
#更新プログラムを確認しています。
#Linux 用 Windows サブシステムの最新バージョンは既にインストールされています。

powershell独特のcurlであるInvoke-WebRequestで、Ubuntu24.04のイメージを取得します。

$tar="$env:TEMP\ubuntu-noble-wsl-amd64-wsl.rootfs.tar.gz"
Invoke-WebRequest `
  -Uri https://cloud-images.ubuntu.com/wsl/releases/24.04/current/ubuntu-noble-wsl-amd64-wsl.rootfs.tar.gz `
  -OutFile $tar

今回使うためのUbuntu24.04に名前を付け、展開先フォルダを作成します。

$dist="Ubuntu24ClaudeCode"
New-Item -ItemType Directory -Path C:\WSL\$dist

#    ディレクトリ: C:\WSL
#
#
#Mode                 LastWriteTime         Length Name
#----                 -------------         ------ ----
#d-----        2025/05/31     21:59                Ubuntu24ClaudeCode

wsl --importで新しいディストリを作ります。

wsl --import $dist C:\WSL\$dist $tar --version 2
#この操作を正しく終了しました。

ルートユーザーで新しいWSL環境に入れます。

wsl -d $dist

ユーザー作成をしますが、一応ClaudeCodeはsudoを利用しないことが推奨されているので、sudo権限の付与は行いません。

USERNAME=claudecode
adduser "${USERNAME}"

ログインユーザーをClaudeCode用のユーザーに設定します。
ここでdockerなど、systemd系を使いたい場合はroot権限のままsystemdを動かしておきます。

※dockerについて、WSLではhostがわdocker desktopを利用する方法を推奨しています。

tee /etc/wsl.conf <<'EOF'
[boot]
systemd=true
[user]
default=claudecode #ユーザー名直打ち
EOF
#[boot]
#systemd=true
#[user]
#default=claudecode

wslを再起動し、作成したユーザーでログインします。

wsl --terminate $dist
#この操作を正しく終了しました。
wsl -d $dist
#これで非rootユーザーのclaudecodeでログインできます

変なところ(windowsのユーザーディレクトリ)にいるのでWSLのユーザーディレクトリに移動します。
WSLからWindowsディレクトリを操作するとパフォーマンスの問題が発生することが知られているので、基本的にWSL側のディレクトリに移動することが推奨されます。

cd ~

具体的にはファイルシステム操作におけるオーバーヘッドがものすごく悪く、往年のHDDみたいな操作感になります。ほぼすべてが重たくなります。

sudo権限を使いたくなった場合、以下の方法でrootから入れます。

wsl -d $dist -u root

WSL内の環境構築

npmのインストール

まず、ClaudeCodeを入れるためにnpmを入れます。
ほぼ公式のMicrosoft Learnでcurlからのインストールが推奨されており、それに従って行います。

pwd
#/home/claudecode

curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/master/install.sh | bash
#  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
#                                 Dload  Upload   Total   Spent    Left  Speed
#100 16631  100 16631    0     0  61056      0 --:--:-- --:--:-- --:--:-- 61143
#=> Downloading nvm from git to '/home/claudecode/.nvm'
#=> Cloning into '/home/claudecode/.nvm'...
#remote: Enumerating objects: 382, done.
#remote: Counting objects: 100% (382/382), done.
#remote: Compressing objects: 100% (325/325), done.
#remote: Total 382 (delta 43), reused 179 (delta 29), pack-reused 0 (from 0)
#Receiving objects: 100% (382/382), 385.06 KiB | 5.92 MiB/s, done.
#Resolving deltas: 100% (43/43), done.
#* (HEAD detached at FETCH_HEAD)
#  master
#=> Compressing and cleaning up git repository
#
#=> Appending nvm source string to /home/claudecode/.bashrc
#=> Appending bash_completion source string to /home/claudecode/.bashrc
#=> Close and reopen your terminal to start using nvm or run the following to use it now:
#
#export NVM_DIR="$HOME/.nvm"
#[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"  # This loads nvm
#[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"  # This loads nvm #bash_completion

export NVM_DIR="$HOME/.nvm"
exit

入りなおします。

wsl -d $dist

nvmが使えるようになったので、通常の手順でnpmを入れます。

command -v nvm
#nvm

nvm install --lts
#Installing latest LTS version.
#Downloading and installing node v22.16.0...
#Downloading https://nodejs.org/dist/v22.16.0/node-v22.16.0-linux-x64.tar.xz...
############################################################################################################# 100.0%
#Computing checksum with sha256sum
#Checksums matched!
#Now using node v22.16.0 (npm v10.9.2)

nvm ls
#->     v22.16.0

npm --version
#10.9.2

ClaudeCodeを入れる

claudecodeを入れます。

npm install -g @anthropic-ai/claude-code
#changed 3 packages in 2s
#
#2 packages are looking for funding
#  run `npm fund` for details

これでやっとClaude Codeが起動できます。

claude

ClaudeCodeをサンドボックス環境として運用する

新規環境の構築

諸々の環境設定が整った後、ClaudeCodeを自由に暴れまわれさせたいことがあるかもしれません。
WSLは一応サンドボックスとしても使えるので、ClaudeCodeが自由に使え、何かがあってもやり直しがきく環境として提供できます。
ここでは、そんなときに役立つWSLイメージのテンプレート的運用について紹介します。

まず、該当WSLを落とします。

wsl --terminate $dist
#この操作を正しく終了しました。

続けて、出力用のディレクトリを作成します。

New-Item -ItemType Directory -Path C:\wsl-images\

#    ディレクトリ: C:\

#Mode                 LastWriteTime         Length Name
#----                 -------------         ------ ----
#d-----        2025/05/31     22:52                wsl-images

tarイメージの形でチェックポイントとしてのwslイメージを書き出します。
ここで書きだした環境がVMイメージのように運用可能になります。

wsl --export $dist C:\wsl-images\ubuntu2404-template.tar

以下のようなpowershell実行ファイルを作成し、適当な場所にlaunch-claude.ps1などの名前で作成します。

param(
    [string]$Template  = "C:\wsl-images\ubuntu2404-template.tar",
    [string]$StoreRoot = "C:\WSL\sandboxes",
    [string]$User      = "claudecode",
    [ValidateSet(1,2)]
    [int]   $Version   = 2
)

# --- 1. タイムスタンプ採番で一意なディストロ名・保存先を決定 ----------------
$stamp   = Get-Date -Format "yyyyMMdd-HHmmss"
$dist    = "sandbox-$stamp"
$store   = Join-Path $StoreRoot $dist
Write-Host "`n[+] distro  : $dist"
Write-Host "[+] template : $Template"
Write-Host "[+] store    : $store (WSL v$Version)`n"

# --- 2. 保存先フォルダを作成 ---------------------------------------------------
New-Item -ItemType Directory -Path $store -Force | Out-Null

# --- 3. テンプレートからインポート -------------------------------------------
wsl --import $dist $store $Template --version $Version
if ($LASTEXITCODE) { throw "wsl --import failed ($LASTEXITCODE)" }

# --- 4. 既定ユーザー作成と /etc/wsl.conf 設定(root で実行) ---------------
$cmdAddUser   = "id -u $User >/dev/null 2>&1 || adduser --disabled-password --gecos '' $User"
$cmdConf      = "printf '[user]\ndefault=%s\n' $User > /etc/wsl.conf"

wsl -d $dist -u root -- bash -c "$cmdAddUser"
wsl -d $dist -u root -- bash -c "$cmdConf"

# --- 5. 反映のため一度停止 -----------------------------------------------------
wsl --terminate $dist

# --- 6. 通常ユーザーでログインして終了 ----------------------------------------
wsl -d $dist --cd ~

上記のps1ファイルを実行することで、以下のような形で新しいclaudecode用のwsl環境を作成することができ、Github CLIやNode、Python等の開発環境をそのまま移植したサンドボックス環境を構築できます。

[+] distro  : sandbox-20250531-230448
[+] template : C:\wsl-images\ubuntu2404-template.tar
[+] store    : C:\WSL\sandboxes\sandbox-20250531-230448 (WSL v2)

#この操作を正しく終了しました。
#この操作を正しく終了しました。
claudecode@<hostname>:~$

CLIで実行する場合、ユーザー名やディストロ名を変更して担当区分を明示することも可能です。

.\launch-claude.ps1 -User "claudecode-frontend"
claudecode-frontend@<hostname>:~$

作成した環境への入り方

作成した環境は、wsl -l -vで確認できます。

#wsl -l -v
#  NAME                       STATE           VERSION
#* Ubuntu-20.04               Running         2
#  sandbox-20250531-230701    Running         2

不要になった環境はwsl --unregisterで削除できます。

wsl --unregister sandbox-20250531-230701
#登録解除。
#この操作を正しく終了しました。

おわりに(宣伝)

Vibe Codingをフル活用して物流業界向けの業務系画像処理システムを構築する仲間を探しています!!!!
Twitter(X)のDMからお待ちしております!!!

keywords: Python, Rust, TypeScript, 画像検索, 画像分類, Azure

2
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?