05 开发环境搭建:Windows + WSL2 + Chrome + MCP¶
本教程默认在 Windows 上使用 WSL2(Ubuntu) 作为开发环境。原因不是 Windows 不能开发,而是 Claude Code 在 Linux 下的命令生态、Shell 兼容性、脚本执行和工具链体验明显更顺。你仍然可以在 Windows 上用 VS Code 看代码、用 Chrome 看页面,两者不冲突。
1. 目标环境¶
Windows 11(宿主机)
├── Windows Terminal # 终端外观
├── VS Code + Remote-WSL # 编辑器(UI 在 Windows,服务在 WSL)
├── Chrome(可选,两种用法)
│ ├── Windows 上的 Chrome # 带你的登录态,CDP 暴露给 WSL
│ └── WSL 内的 Chrome # 独立调试 profile,适合自动化
└── WSL2 Ubuntu 24.04 # 真正的开发环境
├── Claude Code CLI
├── Git / Go / Node / Python / Docker
├── Figma MCP
└── Chrome MCP / chrome-devtools-mcp
黄金法则:代码放在 WSL 的 Linux 文件系统里(~/projects/),不要放在 /mnt/c/ 或 /mnt/d/。跨文件系统的大量小文件读写会非常慢。
2. 安装 WSL2¶
2.1 一键安装¶
在 管理员权限的 PowerShell 中:
安装完成后重启电脑。首次启动 Ubuntu 时设置用户名和密码。
2.2 检查版本¶
确保:
- WSL 版本是 2;
- Ubuntu 的
VERSION是 2; wsl --update已执行到最新。
2.3 配置 .wslconfig¶
在 Windows 用户目录创建 C:\Users\<你的用户名>\.wslconfig:
[wsl2]
# 根据你的内存调整;不要超过物理内存的 50%-60%
memory=12GB
processors=8
# 与 Windows 共享网络栈,localhost 互通,代理也更容易
networkingMode=mirrored
# 自动回收空闲内存
autoMemoryReclaim=dropcache
# 自动回收磁盘空间(较新版本 WSL 支持)
sparseVhd=true
修改后:
如果你的 Windows/WSL 版本不支持某个选项,删掉对应行即可。
networkingMode=mirrored对“WSL 访问 Windows 上 Chrome 的 9222 端口”非常关键。
2.4 启用 systemd¶
WSL 内编辑 /etc/wsl.conf:
然后:
重新进入 WSL,验证:
可选:如果你发现 Windows 的 PATH 污染了 Linux 命令(例如
find、sort被 Windows 版本覆盖),可以在[interop]中加appendWindowsPath=false。代价是code、explorer.exe等 Windows 命令不能直接调用。遇到code .找不到时:去掉该配置、改用 VS Code 的 Remote-WSL 连接,或手动把 VS Code Remote CLI 加入 PATH。
3. 基础工具链¶
3.1 更新系统¶
sudo apt update && sudo apt upgrade -y
sudo apt install -y \
build-essential git curl wget unzip zip jq \
ripgrep fd-find tree make ca-certificates gnupg lsb-release \
python3 python3-pip python3-venv \
fonts-noto-cjk fonts-wqy-zenhei fonts-wqy-microhei
fd-find 在 Ubuntu 中命令名是 fdfind,可以加别名:
3.2 Git¶
git config --global user.name "Your Name"
git config --global user.email "you@example.com"
git config --global init.defaultBranch main
git config --global core.autocrlf input
git config --global pull.rebase true
SSH key:
ssh-keygen -t ed25519 -C "you@example.com"
cat ~/.ssh/id_ed25519.pub
# 把公钥添加到 GitHub/GitLab
ssh -T git@github.com
3.3 Go¶
不要用 Ubuntu 自带的旧版本,直接装官方最新稳定版:
GO_VERSION=$(curl -fsSL https://go.dev/VERSION?m=text | head -n1)
wget "https://go.dev/dl/${GO_VERSION}.linux-amd64.tar.gz"
sudo rm -rf /usr/local/go
sudo tar -C /usr/local -xzf "${GO_VERSION}.linux-amd64.tar.gz"
echo 'export PATH=$PATH:/usr/local/go/bin:$HOME/go/bin' >> ~/.bashrc
source ~/.bashrc
go version
验证:
go env GOPATH
mkdir -p ~/projects/hello && cd ~/projects/hello
go mod init hello
cat > main.go <<'EOF'
package main
import "fmt"
func main() { fmt.Println("hello, wsl") }
EOF
go run .
3.4 Node.js¶
推荐用 nvm 管理:
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.40.1/install.sh | bash
source ~/.bashrc
nvm install --lts
nvm use --lts
node -v
npm -v
如果 nvm 版本变化,去 GitHub releases 找最新安装脚本。不要用
sudo apt install nodejs的旧版本。
3.5 Python¶
python3 --version
sudo apt install -y python3-pip python3-venv
python3 -m venv ~/.venvs/tools
source ~/.venvs/tools/bin/activate
pip install --upgrade pip
pip install python-docx openpyxl requests
我们用
python-docx生成 Word 验收报告,用openpyxl处理 Excel(如果需要)。
4. Chrome:自动化与人工登录的结合¶
4.1 在 WSL 中安装 Chrome¶
你当前目录已经有安装包:
安装:
cd /home/tianbao/tasks/ai-mentor
sudo apt update
sudo apt install -y ./google-chrome-stable_current_amd64.deb
google-chrome --version
如果依赖缺失:
安装中文字体(否则中文页面可能显示方块):
4.2 启动可调试的 Chrome(推荐:独立 profile)¶
重要:Chrome 136+ 要求远程调试必须使用非默认的 --user-data-dir。使用独立 profile 也避免和你日常浏览器冲突。
mkdir -p ~/.chrome-debug-profile
google-chrome \
--remote-debugging-port=9222 \
--user-data-dir="$HOME/.chrome-debug-profile" \
--no-first-run \
--no-default-browser-check \
--disable-background-networking \
>/tmp/chrome-debug.log 2>&1 &
验证:
应该返回包含 webSocketDebuggerUrl 的 JSON。
如果要在 WSLg 中看到窗口:
Windows 桌面会弹出一个 Linux Chrome 窗口(WSLg)。你可以在这个窗口里 人工登录 需要验证码/2FA 的网站,登录态会保存在 ~/.chrome-debug-profile。
4.3 安全注意¶
--remote-debugging-port=9222相当于把浏览器的控制权暴露给本机。只绑定 localhost,不要暴露到公网。- 不要用这个 profile 登录与工作无关的敏感账号。
- 不要在 CI/共享机器上保留长期登录态。
- 如果以 root 运行需要
--no-sandbox,这会降低安全性,只在 WSL 开发环境使用。
4.4 两种 Chrome 模式¶
| 模式 | 用途 | 优点 | 缺点 |
|---|---|---|---|
| WSL 内 Chrome + 独立 profile | E2E、自动化、截图 | 干净、可控、可复现 | 需要重新登录 |
| Windows 宿主机 Chrome + 9222 | 复用你的登录态、人工登录 | 不用重新登录 | 依赖 WSL 镜像网络、暴露面更大 |
连接 Windows 宿主机 Chrome¶
- Windows 端启动 Chrome(PowerShell):
& "C:\Program Files\Google\Chrome\Application\chrome.exe" `
--remote-debugging-port=9222 `
--user-data-dir="$env:TEMP\chrome-debug-profile"
- 确认
.wslconfig开启了networkingMode=mirrored,然后wsl --shutdown重启。 - 在 WSL 中验证:
如果不通:
- 检查 Windows 防火墙是否允许 9222(仅专用网络/localhost)。
- 检查 Chrome 是否真的用了
--user-data-dir。 - 如果镜像网络不可用,查 Windows 主机 IP:
注意:Chrome 的远程调试端点默认只监听 127.0.0.1。跨 WSL/Windows 访问时,可能需要
--remote-debugging-address=0.0.0.0(仅在你理解风险时使用),或者使用端口转发。镜像网络模式下通常 localhost 即可。
4.5 启动脚本¶
项目里放 scripts/start-chrome-debug.sh:
#!/usr/bin/env bash
set -euo pipefail
PORT="${CHROME_DEBUG_PORT:-9222}"
PROFILE="${CHROME_DEBUG_PROFILE:-$HOME/.chrome-debug-profile}"
HEADLESS="${CHROME_HEADLESS:-0}"
mkdir -p "$PROFILE"
ARGS=(
"--remote-debugging-port=$PORT"
"--user-data-dir=$PROFILE"
"--no-first-run"
"--no-default-browser-check"
"--disable-background-networking"
)
if [ "$HEADLESS" = "1" ]; then
ARGS+=("--headless=new")
fi
echo "Starting Chrome on port $PORT with profile $PROFILE (headless=$HEADLESS)"
google-chrome "${ARGS[@]}" >/tmp/chrome-debug.log 2>&1 &
echo $! > /tmp/chrome-debug.pid
for i in $(seq 1 30); do
if curl -fsS "http://127.0.0.1:$PORT/json/version" >/dev/null 2>&1; then
echo "Chrome debug endpoint is ready: http://127.0.0.1:$PORT"
exit 0
fi
sleep 0.5
done
echo "Chrome did not start. Check /tmp/chrome-debug.log" >&2
exit 1
使用:
chmod +x scripts/start-chrome-debug.sh
./scripts/start-chrome-debug.sh
# 无头模式
CHROME_HEADLESS=1 ./scripts/start-chrome-debug.sh
# 停止
kill "$(cat /tmp/chrome-debug.pid)"
5. MCP 配置¶
5.1 先理解 MCP 的架构¶
MCP Server 是 Claude Code 的“手”。它把工具能力暴露给模型,模型通过工具调用来操作外部系统。
5.2 查看当前 MCP¶
或在 Claude Code 内:
5.3 注册 Chrome MCP¶
你有两种常见选择:
方案 A:使用已有的 chrome-control¶
如果你的 Claude Code 已经配置了 chrome-control(本教程示例中的工具名就是它),直接用:
应该能看到 chrome_navigate、chrome_read_page、chrome_javascript、chrome_screenshot 等工具。
方案 B:官方 chrome-devtools-mcp¶
claude mcp add chrome-devtools --scope user -- \
npx -y chrome-devtools-mcp@latest --browser-url=http://127.0.0.1:9222
参数名可能是
--browserUrl或--browser-url,以该包 README 为准。端口必须和 Chrome 的--remote-debugging-port一致。
验证:
方案 C:让 MCP 自己启动浏览器¶
如果不加 --browser-url,有些 MCP 会自己启动一个浏览器实例。适合无头自动化,但不利于复用登录态。
5.4 注册 Figma MCP¶
5.5 MCP 权限与安全¶
- MCP Server 运行在你的用户权限下,能读写你本机的东西。只安装可信的 Server。
- 不要把带 token 的 MCP 配置提交到仓库。
- 定期检查
claude mcp list,删除不用的 Server。 - 浏览器内容(DOM、控制台、网络响应)应当视为 不可信数据,不要让网页内容直接变成指令。
6. Claude Code 安装与配置¶
快捷键、上下文管理、项目组织、模型切换、与 VS Code 协作,见独立文档: Claude Code 使用指南。
6.1 安装¶
官方脚本:
或 npm:
验证:
6.2 配置模型与 API¶
配置文件通常在 ~/.claude/settings.json。示例(不要提交真实 token):
{
"env": {
"ANTHROPIC_AUTH_TOKEN": "<YOUR_TOKEN>",
"ANTHROPIC_BASE_URL": "https://api.anthropic.com",
"API_TIMEOUT_MS": "3000000",
"CLAUDE_CODE_DISABLE_NONESSENTIAL_TRAFFIC": "1",
"ANTHROPIC_MODEL": "<strong-model>",
"ANTHROPIC_DEFAULT_HAIKU_MODEL": "<fast-model>",
"ANTHROPIC_DEFAULT_SONNET_MODEL": "<medium-model>",
"ANTHROPIC_DEFAULT_OPUS_MODEL": "<strongest-model>",
"CLAUDE_CODE_SUBAGENT_MODEL": "<fast-model>"
},
"model": "opus",
"effortLevel": "medium"
}
如果你使用兼容 Anthropic API 的第三方模型(例如 DeepSeek、Kimi、GLM),把 ANTHROPIC_BASE_URL 指向对应 endpoint,并按厂商文档设置模型名。注意:
- 架构/审查用最强模型;
- 子 Agent 用快速模型;
- 不同模型对工具调用和长上下文的表现不同,先用小任务验证。
6.3 权限模式¶
- 默认模式:每次工具调用需要确认,安全但慢。
--dangerously-skip-permissions/ bypass 模式:不确认就执行。
建议:
- 需求、设计、审查:默认模式,逐步确认。
- 在隔离的 worktree/容器里做批量实现:可以放宽权限,但要限制目录和网络。
- 永远不要在生产环境或含敏感凭证的目录使用 bypass 模式。
6.4 项目级配置¶
project/
├── CLAUDE.md
├── .claude/
│ ├── settings.json # 项目级设置(可提交)
│ ├── settings.local.json # 本地私有设置(不提交)
│ ├── agents/ # 子 Agent 定义
│ └── skills/ # 项目技能
.gitignore 中加入:
7. VS Code Remote-WSL¶
7.1 安装¶
- Windows 端安装 VS Code。
- 安装扩展 Remote - WSL(或 WSL 扩展包)。
7.2 打开项目¶
在 WSL 终端:
VS Code 会:
- 在 WSL 内启动 VS Code Server;
- 文件监听使用 Linux 原生
inotify; - 当 Claude Code 修改文件时,编辑器毫秒级刷新。
7.3 推荐设置¶
{
"editor.formatOnSave": true,
"editor.defaultFormatter": "esbenp.prettier-vscode",
"files.autoSave": "afterDelay",
"files.autoSaveDelay": 1000,
"terminal.integrated.defaultProfile.linux": "bash",
"search.exclude": {
"**/node_modules": true,
"**/dist": true,
"**/.git": true
},
"files.watcherExclude": {
"**/node_modules/**": true,
"**/dist/**": true
}
}
7.4 不要在 /mnt/c 下开发¶
错误:
正确:
如果代码现在在 Windows 盘,复制过来:
然后重新 npm install / go mod download,不要复制 node_modules。
8. Docker¶
8.1 方案 A:WSL 内原生 Docker Engine(推荐给 AI 开发)¶
启用 systemd 后:
# 安装 Docker 官方源
sudo install -m 0755 -d /etc/apt/keyrings
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | \
sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg
sudo chmod a+r /etc/apt/keyrings/docker.gpg
echo "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] \
https://download.docker.com/linux/ubuntu $(. /etc/os-release && echo $VERSION_CODENAME) stable" | \
sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
sudo apt update
sudo apt install -y docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
sudo usermod -aG docker "$USER"
重新登录或 newgrp docker,验证:
8.2 方案 B:Docker Desktop + WSL 集成¶
- Windows 安装 Docker Desktop;
- Settings → Resources → WSL Integration;
- 勾选你的 Ubuntu 发行版;
- 在 WSL 中验证
docker ps。
8.3 测试用容器¶
项目里用 docker compose 起依赖:
# docker-compose.dev.yml
services:
postgres:
image: postgres:16
environment:
POSTGRES_USER: app
POSTGRES_PASSWORD: app
POSTGRES_DB: app
ports:
- "5432:5432"
volumes:
- pgdata:/var/lib/postgresql/data
healthcheck:
test: ["CMD-SHELL", "pg_isready -U app"]
interval: 5s
timeout: 3s
retries: 10
redis:
image: redis:7
ports:
- "6379:6379"
volumes:
pgdata:
9. 网络与代理¶
9.1 代理配置¶
如果 Windows 上有代理(例如 7890 端口),WSL 镜像网络下通常可以直接用 127.0.0.1:
export HTTP_PROXY=http://127.0.0.1:7890
export HTTPS_PROXY=http://127.0.0.1:7890
export ALL_PROXY=http://127.0.0.1:7890
export NO_PROXY=localhost,127.0.0.1,::1
写入 ~/.bashrc 或 ~/.profile。验证:
如果不通:
- 确认代理软件允许局域网连接;
- 确认
.wslconfig的networkingMode=mirrored; - 尝试用 Windows 主机 IP 而不是
127.0.0.1; - 检查
NO_PROXY是否误包含目标域名。
9.2 Git 代理¶
git config --global http.proxy http://127.0.0.1:7890
git config --global https.proxy http://127.0.0.1:7890
取消:
9.3 npm 代理¶
10. AWS CLI(为部署做准备)¶
curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o /tmp/awscliv2.zip
unzip /tmp/awscliv2.zip -d /tmp
sudo /tmp/aws/install
aws --version
配置 SSO(推荐):
aws configure sso
aws sso login --profile my-profile
aws sts get-caller-identity --profile my-profile
详细的 AWS 登录态同步与部署见 09 部署与发布。
11. 终端与多路复用¶
11.1 Windows Terminal¶
- Windows 11 通常自带;
- 已经检测到你的目录里有
Microsoft.WindowsTerminalPreview_1.26.2734.0_x64.zip; - 也可以用 Microsoft Store 安装正式版。
在 Windows Terminal 中直接打开 Ubuntu 标签页。建议:
- 字体:JetBrains Mono / Cascadia Code;
- 配色:One Half Dark / Campbell;
- 开启
profiles.defaults的startingDirectory为~。
11.2 tmux(多 Agent 并行必备)¶
推荐布局:
┌─────────────────────┬─────────────────────┐
│ Agent A: US-001 │ Agent B: US-002 │
│ worktree-a │ worktree-b │
├─────────────────────┼─────────────────────┤
│ 日志/测试 │ 主会话/审查 │
└─────────────────────┴─────────────────────┘
常用命令:
不要在一个 tmux 窗格里同时跑多个会改同一份文件的 Agent。并行必须配合 worktree。
12. 浏览器字体与中文输入(可选)¶
如果在 WSLg 的 Chrome/终端里中文显示方块:
sudo apt install -y fonts-noto-cjk fonts-wqy-zenhei fonts-wqy-microhei language-pack-zh-hans
fc-cache -fv
如果需要在 WSL 的 GUI 应用里输入中文,参考 WSL开发人员使用指南 配置 Fcitx5。如果只是用 VS Code Remote-WSL 写代码,不需要在 WSL 里配输入法——输入由 Windows 端处理。
13. 维护与备份¶
13.1 常用维护命令¶
| 场景 | 命令 | 运行位置 |
|---|---|---|
| 关闭 WSL | wsl --shutdown |
PowerShell |
| 更新 WSL | wsl --update |
PowerShell |
| 压缩虚拟磁盘 | wsl --compact |
PowerShell |
| 查看发行版 | wsl -l -v |
PowerShell |
| 清理 APT | sudo apt autoremove -y && sudo apt clean |
WSL |
| 清理日志 | sudo journalctl --vacuum-time=7d |
WSL |
| 查看磁盘 | df -h、du -sh ~/projects/* |
WSL |
13.2 备份¶
wsl --shutdown
New-Item -ItemType Directory -Force -Path D:\WSL_Backup
wsl --export Ubuntu-24.04 D:\WSL_Backup\ubuntu-2026-10-25.tar
恢复:
13.3 项目级备份¶
- 代码提交到 Git;
.env/ 凭证不进 Git;- Chrome 调试 profile 不进 Git;
- 重要文档
docs/提交 Git; - 数据库定期 dump。
14. 故障排查¶
| 现象 | 可能原因 | 处理 |
|---|---|---|
google-chrome 报 cannot open display |
WSLg 未启用/版本旧 | wsl --update,wsl --shutdown;或使用 --headless=new |
| Chrome 启动即退出 | 以 root 运行/沙箱限制 | 加 --no-sandbox(仅开发);检查 /tmp/chrome-debug.log |
curl 9222 连接被拒 |
端口不一致/Chrome 未启动/profile 冲突 | 检查 --remote-debugging-port;Chrome 136+ 必须用非默认 --user-data-dir |
| WSL 访问不到 Windows 的 9222 | 网络模式不是 mirrored / 防火墙 | 改 .wslconfig,wsl --shutdown;检查防火墙 |
| MCP 显示 disconnected | Server 进程崩溃/命令错误 | 看 claude mcp list;手动运行 Server 命令看报错 |
| npm install 极慢 | 项目在 /mnt/c |
移到 ~/projects,删除 node_modules 重装 |
| 文件改动不触发热更新 | 跨文件系统/inotify 限制 | 移到 Linux 文件系统;调大 fs.inotify.max_user_watches |
| 内存爆满 | WSL 未限制内存 | .wslconfig 设置 memory,wsl --shutdown |
| 代理不通 | 未设环境变量/未镜像网络 | 设 HTTP_PROXY,检查 NO_PROXY,测试 curl -I |
| Git 换行符混乱 | core.autocrlf 设置不当 |
git config --global core.autocrlf input,重新规范化 |
| 文件权限异常 | 在 /mnt/c 下操作 |
移到 Linux 文件系统;不要 chmod 777 了事 |
14.1 调大 inotify 限制¶
cat /proc/sys/fs/inotify/max_user_watches
sudo tee -a /etc/sysctl.conf > /dev/null <<'EOF'
fs.inotify.max_user_watches=524288
fs.inotify.max_user_instances=512
EOF
sudo sysctl -p
15. 环境自检脚本¶
创建 scripts/check-env.sh:
#!/usr/bin/env bash
set -uo pipefail
fail=0
check() {
local name="$1"; shift
if "$@" >/dev/null 2>&1; then
echo "✅ $name"
else
echo "❌ $name"
fail=1
fi
}
check "git" git --version
check "go" go version
check "node" node --version
check "npm" npm --version
check "python3" python3 --version
check "docker" docker --version
check "chrome" google-chrome --version
check "claude" claude --version
check "ripgrep" rg --version
check "jq" jq --version
echo ""
if curl -fsS http://127.0.0.1:9222/json/version >/dev/null 2>&1; then
echo "✅ Chrome debug endpoint (9222)"
else
echo "⚠️ Chrome debug endpoint (9222) not running; start it with scripts/start-chrome-debug.sh"
fi
if claude mcp list 2>/dev/null | grep -qi "connected"; then
echo "✅ MCP servers connected (check /mcp for details)"
else
echo "⚠️ No connected MCP server detected; run 'claude mcp list'"
fi
exit "$fail"
16. 环境完成清单¶
- WSL2 Ubuntu 24.04 正常运行
-
.wslconfig配置了内存、CPU、镜像网络 - systemd 已启用
- Git 配置了用户名、邮箱、SSH key
- Go / Node / Python 可用
- Docker 可用
- Chrome 安装完成,
google-chrome --version正常 - Chrome 调试端口 9222 可访问
-
chrome-control或chrome-devtools-mcp已连接 - Figma MCP 已连接(如果使用)
- Claude Code 已安装并配置模型
- VS Code Remote-WSL 可以打开
~/projects/ - 项目代码在 Linux 文件系统,不在
/mnt/c - 代理(如需要)可用
- AWS CLI 已安装(如果准备部署)
下一章:06 技术设计 —— 把需求、UI、后端选型变成可执行的架构契约。