Windows 11 下安装并配置 WSL + Claude Code + OpenAI Codex

Windows 11 下安装并配置 WSL + Claude Code + OpenAI Codex

目标与原则

  1. Windows 11 下安装并配置 WSL 作为独立 Linux 开发环境
  2. 安装并配置好开发工具 Claude Code / OpenAI Codex

1. WSL 的安装和配置

按 Win + R 打开运行窗口
输入 optionalfeatures 或 控制面板\程序\启用或关闭 Windows 功能
在”启用或关闭 Windows 功能”窗口中,勾选以下选项:

  1. 适用于 Linux 的 Windows 子系统 (WSL)
  2. 虚拟机平台 (如果可用)
    Windows PowerShell(管理员)执行:
1
2
3
4
5
6
7
8
9
10
# 设置默认版本为 WSL2
wsl --set-default-version 2
# 设置默认版本为 WSL2
wsl --list --online
# 安装 Debian
wsl --install -d Debian
# 或者安装 Ubuntu
wsl --install -d Ubuntu
# 安装后确认版本
wsl --list --verbose

2. WSL 中进行用户配置

如果首次进入发行版后是 root 用户,需要创建普通用户:

1
2
3
4
# 创建新用户(替换 your_username 为你的用户名)
adduser your_username
# 将用户添加到 sudo 组
usermod -aG sudo your_username

编辑 WSL 配置文件:

1
sudo nano /etc/wsl.conf

在 /etc/wsl.conf 中添加以下内容:

1
2
[user]
default=your_username

保存后,在 Windows 中重启 WSL 即可生效:

1
wsl --shutdown

3. WSL 中为 Debian/Ubuntu 系统备份本地源更换国内镜像源

确认发行版与代号:

1
2
cat /etc/os-release
lsb_release -a 2>/dev/null

备份本地源配置:

Debian/Ubuntu通用

1
sudo cp /etc/apt/sources.list /etc/apt/sources.list.bak

配置国内镜像源(阿里云源):

A. Debian(把 bookworm 改成你的代号)

1
2
3
4
5
sudo tee /etc/apt/sources.list > /dev/null <<'EOF'
deb http://mirrors.aliyun.com/debian/ bookworm main contrib non-free non-free-firmware
deb http://mirrors.aliyun.com/debian/ bookworm-updates main contrib non-free non-free-firmware
deb http://mirrors.aliyun.com/debian-security/ bookworm-security main contrib non-free non-free-firmware
EOF

B. Ubuntu(把 jammy 改成你的代号)

1
2
3
4
5
6
sudo tee /etc/apt/sources.list > /dev/null <<'EOF'
deb http://mirrors.aliyun.com/ubuntu/ jammy main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ jammy-updates main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ jammy-backports main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ jammy-security main restricted universe multiverse
EOF

更新软件包:

1
2
sudo apt update
sudo apt upgrade -y

测试网络(可选):

1
sudo apt install apache2 -y

4. Claude Code 的安装

安装 Node.js:

1
2
3
# 下载并安装 Node.js LTS 版本(Ubuntu/Debian)
curl -fsSL https://deb.nodesource.com/setup_lts.x | sudo bash -
sudo apt-get install -y nodejs

验证安装:

1
2
node --version
npm --version

安装 Claude Code:

1
npm install -g @anthropic-ai/claude-code

使用 Claude Code:

1
2
3
4
# 验证安装
claude --version
# 使用
claude

5. OpenAI Codex 的安装

安装 OpenAI Codex:

1
npm i -g @openai/codex

使用 OpenAI Codex:

1
2
3
4
# 验证安装
codex --version
# 使用
codex

6. 常用 WSL 命令

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# 查看可安装的发行版
wsl --list --online
# 查看已安装的发行版
wsl --list --verbose
# 设置默认发行版
wsl --set-default Ubuntu
# 更新 WSL 内核
wsl --update
# 卸载发行版
wsl --unregister Ubuntu
# 创建一个名为 my-project 的目录
mkdir my-project
# 进入一个名为 my-project 的目录
cd my-project