CLI 安装
4.1 方式一:npm 全局安装(全系统通用,推荐)
Section titled “4.1 方式一:npm 全局安装(全系统通用,推荐)”这是官方推荐、兼容性最强、最适合新手的安装方式,Windows、macOS、Linux 全系统通用。
# 1. 全局安装最新稳定版npm install -g @openai/codex
# 2. 若需安装指定稳定版(避坑首选,0.91.0 无已知核心 bug)
# 3. 后续更新到最新版本npm install -g @openai/codex@latest
# 4. 版本降级(如遇到 bug 时使用)npm install -g @openai/codex@指定版本号查看仓库所有可安装版本
Section titled “查看仓库所有可安装版本”npm view @openai/codex versions# 更新npm update -g @openai/codex
# 卸载npm uninstall -g @openai/codex⚠️ 版本避坑提示
Section titled “⚠️ 版本避坑提示”CSDN 教程明确警告:0.92 版本存在已知的「粘贴内容丢失」核心 bug,严禁使用。请优先选择 0.91.0 版本,或 0.93.0 及以上的稳定版本。
4.2 方式二:Homebrew 安装(macOS/Linux 专属)
Section titled “4.2 方式二:Homebrew 安装(macOS/Linux 专属)”macOS 或 Linux 用户若已安装 Homebrew,可通过以下命令快速安装。编程指北指出 Homebrew 已同步最新版:
# 1. 先更新 Homebrew 本地源brew update
# 2. 安装 Codex CLIbrew install --cask codex注:部分教程写的是
brew install codex,实际命令以 Homebrew 官方 formula 为准。如--cask不适用,可尝试brew install codex。
4.3 方式三:winget 安装(Windows)
Section titled “4.3 方式三:winget 安装(Windows)”Windows 用户可用 winget(需 PowerShell 管理员模式):
winget install OpenAI.Codex若 winget 找不到包,回退到 npm 全局安装方式。
4.4 方式四:从源码编译(Rust 用户)
Section titled “4.4 方式四:从源码编译(Rust 用户)”Codex CLI 用 Rust 编写,可从 github.com/openai/codex 克隆源码自行编译。需要先安装 Rust 工具链(rustup)。这种方式适合想跟踪最新提交或参与贡献的开发者,普通用户不需要。
4.5 安装验证(两步确认,无需网络)
Section titled “4.5 安装验证(两步确认,无需网络)”安装完成后,执行以下两条命令,无需网络、无需账号即可确认安装成功:
第一步:版本检查
Section titled “第一步:版本检查”codex --version✅ 成功示例输出:
0.91.0第二步:基础功能验证
Section titled “第二步:基础功能验证”codex --help✅ 成功标志:终端输出 Codex CLI 的命令说明、参数列表、功能介绍等完整帮助内容。
4.6 安装高频坑速解
Section titled “4.6 安装高频坑速解”问题 1:执行 codex 命令提示 command not found
Section titled “问题 1:执行 codex 命令提示 command not found”核心原因:npm 全局安装路径未添加到系统环境变量。
分系统解决方案:
Windows:
- 执行
npm config get prefix查看 npm 全局安装路径 - 将输出的路径添加到系统「用户环境变量」的
Path中 - 重启 PowerShell/终端,重新执行命令
macOS/Linux:
- 执行
npm config get prefix查看 npm 全局安装路径 - 编辑终端配置文件(
~/.zshrc或~/.bashrc),添加:Terminal window export PATH="npm全局安装路径/bin:$PATH" - 执行
source ~/.zshrc重载环境变量
问题 2:安装时提示权限不足 / EACCES 错误
Section titled “问题 2:安装时提示权限不足 / EACCES 错误”参见上一章 3.4 节配置自定义全局目录。临时方案:
sudo npm install -g @openai/codex但不推荐长期用 sudo。
问题 3:安装成功后,执行命令提示版本不兼容 / 功能异常
Section titled “问题 3:安装成功后,执行命令提示版本不兼容 / 功能异常”优先降级到 0.91.0 稳定版(经过社区验证无核心 bug):
问题 4:Windows 用户专属避坑
Section titled “问题 4:Windows 用户专属避坑”- 优先使用 PowerShell(管理员模式)执行所有命令,避免 CMD 的兼容性问题
- 若计划长期使用,强烈推荐配置 WSL2 环境
- 若安装后重启终端仍无法识别命令,需重启电脑完成环境变量刷新
- CSDN《Codex CLI 教程(一)安装指南》——npm 安装命令、版本避坑、command not found 解决
- 编程指北 csguide.cn——Homebrew 安装与 30 秒安装流程
- FastGPTPlus——Windows WSL2 说明
- OpenAI 官方仓库 github.com/openai/codex