Windows + WSL2 安装 Codex 操作笔记
环境:Windows 11 + WSL2 + Ubuntu 24.04
目标:在 WSL2 Ubuntu 中安装和运行 Codex CLI,Windows 只负责 VS Code 图形界面。
一、最终推荐工作方式
以后日常使用流程:
bash
cd ~/workspace/你的项目
code .
codex
含义:
- 项目文件放在 Ubuntu / WSL2 里
- Windows 上的 VS Code 只是查看和编辑文件
- Codex 实际运行在 Ubuntu 命令行中
- 代码生成、文件修改、项目校验都在 WSL2 里完成
二、检查 WSL2 是否正常
在 Windows PowerShell 执行:
powershell
wsl -l -v
正常结果类似:
text
NAME STATE VERSION
Ubuntu-24.04 Running 2
如果不是 VERSION 2,执行:
powershell
wsl --set-version Ubuntu-24.04 2
进入 Ubuntu:
powershell
wsl -d Ubuntu-24.04
三、WSL2 本地代理提示说明
启动 Ubuntu 时可能出现:
text
wsl: 检测到 localhost 代理配置,但未镜像到 WSL。
NAT 模式下的 WSL 不支持 localhost 代理。
这不是严重报错,意思是:
Windows 里的代理地址一般是:
text
127.0.0.1:7890
但在 WSL2 NAT 模式下,Ubuntu 里的 127.0.0.1 指的是 Ubuntu 自己,不是 Windows,所以代理不能自动复用。
四、WSL2 代理推荐配置方式
方式一:开启 mirrored 网络模式
在 Windows PowerShell 执行:
powershell
notepad $env:USERPROFILE\.wslconfig
写入:
ini
[wsl2]
networkingMode=mirrored
dnsTunneling=true
autoProxy=true
firewall=true
保存后执行:
powershell
wsl --shutdown
wsl -d Ubuntu-24.04
测试网络:
bash
curl -I https://chatgpt.com
方式二:手动给 WSL 设置代理
先在 Windows 代理软件里打开:
text
允许局域网连接 / Allow LAN
然后在 Ubuntu 里执行:
bash
hostip=$(ip route | awk '/default/ {print $3}')
export http_proxy="http://$hostip:7890"
export https_proxy="http://$hostip:7890"
export HTTP_PROXY="$http_proxy"
export HTTPS_PROXY="$https_proxy"
测试:
bash
curl -I https://chatgpt.com
看到类似:
text
HTTP/1.1 200 Connection established
说明 WSL 已经能通过代理访问外网。
如果后面出现:
text
HTTP/2 403
cf-mitigated: challenge
server: cloudflare
这通常是 Cloudflare 拦截命令行访问网页首页,不代表代理失败。
五、安装基础依赖
在 Ubuntu 里执行:
bash
sudo apt update
sudo apt upgrade -y
sudo apt install -y curl git ca-certificates build-essential unzip zip jq bubblewrap
六、创建 Codex 工作目录
建议统一把项目放在:
bash
~/workspace
创建测试项目:
bash
mkdir -p ~/workspace/codex-demo
cd ~/workspace/codex-demo
git init
不建议直接在这些目录运行 Codex:
bash
/mnt/c/Users/你的用户名/Desktop
/mnt/d/公司资料
/mnt/d/投标文件
/mnt/d/微信文件
正式文件建议复制副本到 WSL 工作区再让 Codex 操作。
七、安装 Codex CLI
你本次使用的是 standalone installer 安装方式:
bash
curl -fsSL https://chatgpt.com/codex/install.sh | sh
安装后检查版本:
bash
which codex
codex --version
当前结果:
text
/home/muxia/.local/bin/codex
codex-cli 0.142.0
这说明 Codex 安装在当前 Ubuntu 用户目录下,不是 npm 全局安装。
八、启动 Codex
进入项目目录:
bash
cd ~/workspace/codex-demo
codex
首次启动按提示登录 ChatGPT 账号。
测试命令:
text
查看当前文件夹下的文件
九、VS Code 配置 WSL
在 Ubuntu 项目目录里执行:
bash
code .
这会打开 Windows 上的 VS Code,但实际编辑的是 WSL2 里的文件。
VS Code 左下角应显示类似:
text
WSL: Ubuntu-24.04
十、VS Code 设置 Codex 使用 WSL
在 VS Code 中按:
text
Ctrl + Shift + P
搜索:
text
Preferences: Open User Settings (JSON)
加入:
json
{
"chatgpt.runCodexInWindowsSubsystemForLinux": true
}
如果已有其他配置,注意加逗号。
十一、Codex 配置 GPT-5.5 high
配置文件位置:
bash
~/.codex/config.toml
编辑:
bash
nano ~/.codex/config.toml
当前推荐配置:
toml
model = "gpt-5.5"
model_reasoning_effort = "high"
[projects."/mnt/d/soft/coding/workspace/codex-demo"]
trust_level = "trusted"
[projects."/home/muxia"]
trust_level = "trusted"
[tui]
status_line = [
"model-with-reasoning",
"context-remaining",
"current-dir",
"git-branch",
"five-hour-limit",
"weekly-limit",
"codex-version"
]
[tui.model_availability_nux]
"gpt-5.5" = 1
保存方式:
text
Ctrl + O
回车
Ctrl + X
十二、底部状态栏说明
配置成功后,Codex 底部会显示类似:
text
gpt-5.5 high · Context 99% left · /mnt/d/soft/coding/workspace/codex-demo · master · 5h 98% left · weekly 98% ...
含义:
gpt-5.5 high:当前模型和推理强度Context 99% left:当前会话上下文剩余 99%/mnt/d/.../codex-demo:当前工作目录master:当前 Git 分支5h 98% left:5 小时窗口额度还剩约 98%weekly 98% left:周额度还剩约 98%
十三、Codex 常用命令
进入 Codex 后可输入:
text
/model
切换模型和推理强度。
text
/status
查看当前模型、权限、目录和 token 使用情况。
text
/usage
查看使用量。
text
/usage daily
/usage weekly
/usage cumulative
查看每日、每周、累计使用量。
text
/statusline
手动配置底部状态栏。
text
/debug-config
检查配置文件是否生效。
text
/quit
退出 Codex。
十四、Codex 升级方式
因为当前是通过:
bash
curl -fsSL https://chatgpt.com/codex/install.sh | sh
安装的,所以升级也是重新执行这条命令:
bash
curl -fsSL https://chatgpt.com/codex/install.sh | sh
升级后检查:
bash
which codex
codex --version
正常路径应仍然是:
text
/home/muxia/.local/bin/codex
不建议使用:
bash
npm install -g @openai/codex@latest
否则可能出现 npm 版和 standalone 版混用,导致路径混乱。
十五、创建升级快捷命令
可以创建一个别名:
bash
echo 'alias codex-upgrade="curl -fsSL https://chatgpt.com/codex/install.sh | sh && codex --version"' >> ~/.bashrc
source ~/.bashrc
以后升级只需要:
bash
codex-upgrade
十六、检查 Codex 有没有更新
查看本地版本:
bash
codex --version
启动 Codex 时,如果有更新,通常会提示:
text
Update available!
也可以用 GitHub latest release 检查:
bash
curl -sI https://github.com/openai/codex/releases/latest | grep -i location
十七、建议的日常使用流程
打开 Ubuntu:
powershell
wsl -d Ubuntu-24.04
进入项目:
bash
cd ~/workspace/你的项目
打开 VS Code:
bash
code .
启动 Codex:
bash
codex
重要修改前先提交 Git:
bash
git add .
git commit -m "before codex changes"
查看 Codex 改了什么:
bash
git diff
不满意可以回滚:
bash
git restore .
十八、最终理解
Windows 负责:
text
VS Code、WPS、Word、浏览器、办公软件
Ubuntu / WSL2 负责:
text
Codex、Git、Python、Node、Java、AI Agent、项目运行和文件生成
最佳实践:
text
Windows 看文件
Ubuntu 跑命令
Codex 操作 WSL2 项目
正式文件先复制副本再处理