Skip to content

Codex CLI 安装教程

本文将教您如何在不同系统(Windows / macOS / Linux)环境下安装并使用 OpenAI Codex CLI。

📋 安装前置环境

在安装 Codex CLI 前,请先确保系统已具备:

必须安装

  • Node.js 18 以上版本(必须安装 npm)
  • 前往 Node.js 官网 下载安装

可选安装

  • Python 3.8 以上版本
  • 某些 Codex 功能需要 Python 支持,建议安装并加入系统 PATH

💻 安装 Codex CLI

Windows / Linux 安装 / macOS 安装

bash
npm install -g @openai/codex

安装完成后可检查版本:

bash
codex --version

🔐 配置 Codex 环境变量

安装完成后,需要通过配置文件设置 Codex CLI:

配置文件设置方法

Codex CLI 使用配置文件进行设置,需要创建两个配置文件:

1. 创建主配置文件

Windows: 在 %USERPROFILE%\.codex\config.toml 文件中添加以下配置:

macOS/Linux: 在 ~/.codex/config.toml 文件中添加以下配置:

toml
model_provider = "crs"
model = "gpt-5"
model_reasoning_effort = "high"
disable_response_storage = true
preferred_auth_method = "apikey"

[model_providers.crs]
name = "crs"
base_url = "https://mycoding.cc/openai"
wire_api = "responses"

2. 创建认证文件

Windows: 在 %USERPROFILE%\.codex\auth.json 文件中配置API密钥:

macOS/Linux: 在 ~/.codex/auth.json 文件中配置API密钥:

json
{
  "OPENAI_API_KEY": "你的API密钥"
}

💡 提示

  • 使用与 Claude Code 相同的 API 密钥即可,格式如 cr_xxxxxxxxxx
  • 如果目录不存在,请先创建 .codex 目录

快速配置脚本

Windows PowerShell

powershell
# 创建配置目录
New-Item -ItemType Directory -Path "$env:USERPROFILE\.codex" -Force

# 创建配置文件
@"
model_provider = "crs"
model = "gpt-5"
model_reasoning_effort = "high"
disable_response_storage = true
preferred_auth_method = "apikey"

[model_providers.crs]
name = "crs"
base_url = "https://mycoding.cc/openai"
wire_api = "responses"
"@ | Out-File -FilePath "$env:USERPROFILE\.codex\config.toml" -Encoding UTF8

# 创建认证文件(请替换你的API密钥)
@"
{
  "OPENAI_API_KEY": "你的API密钥"
}
"@ | Out-File -FilePath "$env:USERPROFILE\.codex\auth.json" -Encoding UTF8

macOS/Linux

bash
# 创建配置目录
mkdir -p ~/.codex

# 创建配置文件
cat > ~/.codex/config.toml << 'EOF'
model_provider = "crs"
model = "gpt-5"
model_reasoning_effort = "high"
disable_response_storage = true
preferred_auth_method = "apikey"

[model_providers.crs]
name = "crs"
base_url = "https://mycoding.cc/openai"
wire_api = "responses"
EOF

# 创建认证文件(请替换你的API密钥)
cat > ~/.codex/auth.json << 'EOF'
{
  "OPENAI_API_KEY": "你的API密钥"
}
EOF

验证配置设置

检查配置文件是否正确创建:

Windows

powershell
Get-Content "$env:USERPROFILE\.codex\config.toml"
Get-Content "$env:USERPROFILE\.codex\auth.json"

macOS/Linux

bash
cat ~/.codex/config.toml
cat ~/.codex/auth.json

✅ 测试 Codex CLI

执行:

bash
codex help

若正常显示指令列表,代表安装成功。

🔧 常见问题

问题解法
npm 找不到安装 Node.js 并勾选 Add to PATH
python3 找不到依照上述步骤建立软连结
权限不足在 macOS / Linux 加上 sudo 执行指令;Windows 以系统管理员模式开启终端机

📚 下一步

安装完成后,您可以:

  1. 快速入门教程 - 学习基本使用方法
  2. 命令参考 - 查看完整命令列表
  3. 使用教程 - 高效使用技巧
  4. 常见问题 - 疑难解答

安装完成!开始享受 Codex CLI 带来的高效编程体验吧!