WSL2 + Windows Terminal 開発環境セットアップガイド
Claude Code を中心とした WSL2 Ubuntu 開発環境を構築する手順。
前提条件
- Windows 11 + WSL2 (Ubuntu 24.04)
- Windows Terminal インストール済み
Part 1: Windows 側の準備
PowerShell を 管理者として 開いて実行する。
Hack Nerd Font Mono
ターミナルのアイコン表示に必要なフォント。
winget install --id NerdFonts.Hack --accept-source-agreements --accept-package-agreements
インストール後、Windows Terminal の設定でフォントを変更する。
設定 → プロファイル → Ubuntu → 外観 → フォント フェイス → Hack Nerd Font Mono
Part 2: 必須ツールのインストール
Windows Terminal で Ubuntu を開いて実行する。
システムパッケージ
sudo apt update && sudo apt install -y \
build-essential git curl unzip wslu jq xclip
| パッケージ | 用途 |
|---|---|
build-essential |
C/C++ コンパイラ(ネイティブモジュールのビルド用) |
git |
バージョン管理 |
curl |
HTTP クライアント |
unzip |
アーカイブ展開 |
wslu |
WSL ユーティリティ(wslview でブラウザを開く) |
jq |
JSON 処理 |
xclip |
クリップボード操作 |
xclip ラッパー(Claude Code 画像ペースト対応)
WSLg のクリップボードに画像がない場合、Windows 側のクリップボードから取得するラッパー。Claude Code で画像をペーストする際に必要。
mkdir -p ~/.local/bin
cat > ~/.local/bin/xclip << 'EOF'
#!/bin/bash
# xclip wrapper: WSLg にない画像を Windows clipboard から透過的に取得する
# 本物の xclip は /usr/bin/xclip
# TARGETS チェック(Claude Code が画像の有無を確認する)
if [[ "$*" == *"-t TARGETS"* && "$*" == *"-o"* ]]; then
result=$(/usr/bin/xclip "$@" 2>/dev/null)
if echo "$result" | grep -q "image/"; then
echo "$result"
else
has_image=$(powershell.exe -NoProfile -Command '
Add-Type -AssemblyName System.Windows.Forms
if ([System.Windows.Forms.Clipboard]::ContainsImage()) { Write-Output "yes" }
' 2>/dev/null | tr -d '\r')
if [ "$has_image" = "yes" ]; then
echo -e "image/png\nTARGETS"
else
/usr/bin/xclip "$@" 2>/dev/null
fi
fi
# 画像データ取得
elif [[ "$*" == *"-t image/"* && "$*" == *"-o"* ]]; then
data=$(/usr/bin/xclip "$@" 2>/dev/null)
if [ -n "$data" ]; then
echo -n "$data"
else
powershell.exe -NoProfile -Command '
Add-Type -AssemblyName System.Windows.Forms
$img = [System.Windows.Forms.Clipboard]::GetImage()
if ($img) {
$ms = New-Object System.IO.MemoryStream
$img.Save($ms, [System.Drawing.Imaging.ImageFormat]::Png)
[Console]::OpenStandardOutput().Write($ms.ToArray(), 0, $ms.Length)
}' 2>/dev/null
fi
# それ以外は本物の xclip に委譲
else
/usr/bin/xclip "$@"
fi
EOF
chmod +x ~/.local/bin/xclip
.bashrc の PATH で ~/.local/bin を /usr/bin より先に配置すること。
NVM & Node.js
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.40.4/install.sh | bash
source ~/.nvm/nvm.sh
nvm install --lts
.bashrc に追加:
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"
uv(Python パッケージマネージャー)
curl -LsSf https://astral.sh/uv/install.sh | sh
.bashrc に追加:
[ -f "$HOME/.local/bin/env" ] && . "$HOME/.local/bin/env"
GitHub CLI
sudo mkdir -p -m 755 /etc/apt/keyrings
wget -qO- https://cli.github.com/packages/githubcli-archive-keyring.gpg \
| sudo tee /etc/apt/keyrings/githubcli-archive-keyring.gpg >/dev/null
sudo chmod go+r /etc/apt/keyrings/githubcli-archive-keyring.gpg
echo "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/githubcli-archive-keyring.gpg] https://cli.github.com/packages stable main" \
| sudo tee /etc/apt/sources.list.d/github-cli-stable.list >/dev/null
sudo apt update && sudo apt install -y gh
Claude Code
curl -fsSL https://claude.ai/install.sh | bash
Doppler(シークレット管理)
sudo apt-get install -y apt-transport-https ca-certificates curl gnupg
curl -sLf --retry 3 --tlsv1.2 --proto "=https" \
'https://packages.doppler.com/public/cli/gpg.DE2A7741A397C129.key' \
| sudo gpg --dearmor -o /usr/share/keyrings/doppler-archive-keyring.gpg
echo "deb [signed-by=/usr/share/keyrings/doppler-archive-keyring.gpg] https://packages.doppler.com/public/cli/deb/debian any-version main" \
| sudo tee /etc/apt/sources.list.d/doppler-cli.list >/dev/null
sudo apt-get update && sudo apt-get install -y doppler
Part 3: 初期設定(手動で行う作業)
SSH 鍵の生成
ssh-keygen -t ed25519 -C "your-email@example.com"
GitHub に公開鍵を登録
cat ~/.ssh/id_ed25519.pub
# 出力を https://github.com/settings/keys に登録
Git ユーザー設定
git config --global user.name "Your Name"
git config --global user.email "your-email@example.com"
GitHub CLI ログイン
gh auth login
Claude Code ログイン
claude login
Claude Code キーバインド設定
Alt+V で画像ペーストできるようにする(WSL2 では Ctrl+V がターミナルのペーストと競合するため)。
mkdir -p ~/.claude
cat > ~/.claude/keybindings.json << 'EOF'
{
"$schema": "https://www.schemastore.org/claude-code-keybindings.json",
"bindings": [
{
"context": "Chat",
"bindings": {
"alt+v": "chat:imagePaste"
}
}
]
}
EOF
.bashrc
以下は完全な .bashrc テンプレート。オプションツールの部分はインストールしたものだけ残す。
# =============================================================
# ~/.bashrc — WSL2 Ubuntu configuration
# =============================================================
# --- 非インタラクティブなら即終了 ---
case $- in *i*) ;; *) return ;; esac
# --- 基本設定 & 履歴 ---
HISTCONTROL=ignoreboth
HISTSIZE=1000
HISTFILESIZE=2000
shopt -s histappend checkwinsize
# --- 環境変数 & パス ---
export PATH="$HOME/.local/bin:/usr/local/bin:$PATH"
export GPG_TTY=$(tty)
export BROWSER="wslview"
# NVM
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"
# uv (Python)
[ -f "$HOME/.local/bin/env" ] && . "$HOME/.local/bin/env"
# LS_COLORS
eval "$(dircolors -b)"
# --- SSH Agent ---
export SSH_AUTH_SOCK="$XDG_RUNTIME_DIR/ssh-agent.socket"
ssh-add -l &>/dev/null
_ssh=$?
if [ $_ssh -eq 2 ]; then
eval "$(ssh-agent -a "$SSH_AUTH_SOCK" -s)" >/dev/null
_ssh=1
fi
if [ $_ssh -eq 1 ]; then
ssh-add ~/.ssh/id_ed25519 2>/dev/null
fi
unset _ssh
# --- エイリアス ---
alias ..='cd ..'
# [optional] bat
alias cat='batcat'
# [optional] eza
if [ -x "$(command -v eza)" ]; then
alias ls='eza --icons --git --group-directories-first'
alias ll='eza -lbhF --icons --git --group-directories-first'
alias la='eza -labhF --icons --git --group-directories-first'
alias lt='eza --tree --icons --level=2'
fi
# [optional] fd-find
if [ -x "$(command -v fdfind)" ]; then
alias fd='fdfind'
alias f='fdfind'
fi
# [optional] yazi
alias y='yazi'
# --- メンテナンス関数 ---
# システム更新 (sudo 必要)
update() {
echo "--- Updating OS Packages ---"
sudo apt update && sudo apt upgrade -y
# [optional] Starship
if command -v starship &>/dev/null; then
echo "--- Updating Starship ---"
(
cd $(mktemp -d)
curl -sL -o starship.tar.gz \
"https://github.com/starship/starship/releases/latest/download/starship-x86_64-unknown-linux-musl.tar.gz"
tar -xzf starship.tar.gz
sudo mv starship /usr/local/bin/
)
fi
echo "--- Updating Node.js (NVM) ---"
nvm install --lts && nvm use --lts
npm update -g
echo "--- Updating Claude Code ---"
claude update
echo "--- Updating uv ---"
uv self update
# [optional] Yazi & Glow
if command -v yazi &>/dev/null || command -v glow &>/dev/null; then
echo "--- Updating Yazi & Glow ---"
(
cd $(mktemp -d)
if command -v yazi &>/dev/null; then
VERSION=$(curl -sI https://github.com/sxyazi/yazi/releases/latest | grep -i "location:" | grep -Po 'v\K[0-9.]+')
curl -sLO "https://github.com/sxyazi/yazi/releases/download/v${VERSION}/yazi-x86_64-unknown-linux-musl.zip"
unzip -q yazi-x86_64-unknown-linux-musl.zip
install -m 755 yazi-x86_64-unknown-linux-musl/yazi "$HOME/.local/bin/"
fi
if command -v glow &>/dev/null; then
VERSION=$(curl -sI https://github.com/charmbracelet/glow/releases/latest | grep -i "location:" | grep -Po 'v\K[0-9.]+')
curl -sLO "https://github.com/charmbracelet/glow/releases/download/v${VERSION}/glow_${VERSION}_Linux_x86_64.tar.gz"
tar -xzf "glow_${VERSION}_Linux_x86_64.tar.gz"
install -m 755 glow "$HOME/.local/bin/"
fi
)
fi
echo "--- Update Complete ---"
}
# --- プロンプト & ウェルカム ---
# [optional] Starship
[[ -x $(command -v starship) ]] && eval "$(starship init bash)"
# [optional] fastfetch
if [[ "$SHLVL" -eq 1 ]] && command -v fastfetch &>/dev/null; then
fastfetch
printf " \033[1;36m Welcome, %s\033[0m\n\n" "$USER"
fi
シェルの再起動
exec bash
Part 4: オプションツール
必須ではないが、入れると便利なツール。好みに合わせて選択する。
bat — シンタックスハイライト付き cat
ファイルの中身を色付きで表示する。cat の上位互換。
sudo apt install -y bat
コマンド名は batcat。.bashrc に追加:
alias cat='batcat'
fd-find — 高速ファイル検索
find の代替。直感的な構文で高速にファイルを探せる。
sudo apt install -y fd-find
コマンド名は fdfind。.bashrc に追加:
alias fd='fdfind'
alias f='fdfind'
eza — モダンな ls
Git ステータス表示やアイコン付きでディレクトリを一覧表示する ls 代替。
sudo apt install -y eza
.bashrc に追加:
alias ls='eza --icons --git --group-directories-first'
alias ll='eza -lbhF --icons --git --group-directories-first'
alias la='eza -labhF --icons --git --group-directories-first'
alias lt='eza --tree --icons --level=2'
glow — ターミナル Markdown ビューアー
Markdown ファイルをターミナル内で整形表示する。
VERSION=$(curl -sI https://github.com/charmbracelet/glow/releases/latest \
| grep -i "location:" | grep -Po 'v\K[0-9.]+')
curl -sL "https://github.com/charmbracelet/glow/releases/download/v${VERSION}/glow_${VERSION}_Linux_x86_64.tar.gz" \
| tar -xz -C /tmp
install -m 755 "/tmp/glow_${VERSION}_Linux_x86_64/glow" ~/.local/bin/
rm -rf "/tmp/glow_${VERSION}_Linux_x86_64"
win32yank — Neovim クリップボード連携
WSL2 の Neovim でヤンク(コピー)したテキストを Windows クリップボードに送る。Neovim を使わないなら不要。
curl -sL -o /tmp/win32yank.zip \
https://github.com/equalsraf/win32yank/releases/latest/download/win32yank-x64.zip
unzip -q /tmp/win32yank.zip -d /tmp
sudo cp /tmp/win32yank.exe /usr/local/bin/
sudo chmod +x /usr/local/bin/win32yank.exe
rm -f /tmp/win32yank.zip /tmp/win32yank.exe /tmp/LICENSE
libsixel-bin — ターミナル画像表示
ターミナル内に画像を直接表示する。対応ターミナルが必要。
sudo apt install -y libsixel-bin
starship — カスタマイズ可能なプロンプト
Git ブランチやステータスをプロンプトに表示するシェルテーマ。
curl -sL "https://github.com/starship/starship/releases/latest/download/starship-x86_64-unknown-linux-musl.tar.gz" \
| tar -xz -C /tmp
sudo mv /tmp/starship /usr/local/bin/
.bashrc に追加:
eval "$(starship init bash)"
設定例(~/.config/starship.toml):
format = """
$os\
$username\
$directory\
$git_branch\
$git_status\
$character
"""
command_timeout = 1000
[os]
disabled = false
style = "bold white"
format = "[$symbol]($style) "
[os.symbols]
Ubuntu = ""
Windows = ""
[username]
show_always = true
style_user = "bold yellow"
format = "[$user]($style) in "
[directory]
style = "bold cyan"
truncation_length = 3
[character]
success_symbol = "[➜](bold green)"
error_symbol = "[➜](bold red)"
yazi — ターミナルファイルマネージャー
Vim ライクなキーバインドでファイルを操作できる TUI ファイルマネージャー。
VERSION=$(curl -sI https://github.com/sxyazi/yazi/releases/latest \
| grep -i "location:" | grep -Po 'v\K[0-9.]+')
curl -sL "https://github.com/sxyazi/yazi/releases/download/v${VERSION}/yazi-x86_64-unknown-linux-musl.zip" \
-o /tmp/yazi.zip
unzip -q /tmp/yazi.zip -d /tmp
install -m 755 /tmp/yazi-x86_64-unknown-linux-musl/yazi ~/.local/bin/
rm -rf /tmp/yazi.zip /tmp/yazi-x86_64-unknown-linux-musl
.bashrc に追加:
alias y='yazi'
fastfetch — システム情報表示
ターミナル起動時に OS、カーネル、メモリなどのシステム情報をアスキーアート付きで表示する。
sudo add-apt-repository -y ppa:zhangsongcui3371/fastfetch
sudo apt update && sudo apt install -y fastfetch
.bashrc に追加すると起動時に表示される:
if [[ "$SHLVL" -eq 1 ]] && command -v fastfetch &>/dev/null; then
fastfetch
fi