October 23, 2026

如何安装和设置 Codex CLI

Codex CLI 是 OpenAI 推出的开源终端编程智能体。本指南介绍在 macOS、Linux 和 Windows 上安装 Codex CLI 的所有方式,以及如何登录、运行首个会话、进行配置和更新。随后介绍如何在 Chief 中将 Codex 与 Claude Code 搭配运行。

快速安装

平台命令
macOS 和 Linuxcurl -fsSL https://chatgpt.com/codex/install.sh | sh
Windows(PowerShell)powershell -ExecutionPolicy ByPass -c "irm https://chatgpt.com/codex/install.ps1 | iex"
npm(任意平台)npm install -g @openai/codex
Homebrewbrew install --cask codex

然后在项目文件夹中运行 codex,选择 使用 ChatGPT 登录。

以上命令来自 OpenAI 的 Codex README 和文档,已于 2026 年 10 月核对。最新信息请查看 OpenAI 的 Codex 文档。


第 1 步:安装 Codex CLI

macOS 和 Linux

在 macOS 和 Linux 上,推荐使用 OpenAI 的独立安装程序安装 Codex CLI:

curl -fsSL https://chatgpt.com/codex/install.sh | sh

Codex CLI 支持 Apple Silicon 和 Intel 架构的 macOS,以及 x86_64 和 arm64 架构的 Linux。

更喜欢用包管理器?可以使用 Homebrew:

brew install --cask codex

或 npm:

npm install -g @openai/codex

你也可以从 Codex GitHub releases 页面下载适用于你平台的二进制文件,并将其重命名为 codex。

Windows

Codex CLI 可在 Windows 上原生运行。要在 Windows 上安装 Codex CLI,请在 PowerShell 中运行:

powershell -ExecutionPolicy ByPass -c "irm https://chatgpt.com/codex/install.ps1 | iex"

在 Windows 上,Codex 默认使用原生 Windows 沙箱,因此你不需要 WSL 或虚拟机。如果你需要 Linux 原生工具,或者你的工作流本来就在 WSL2 中运行,OpenAI 建议使用 WSL2;这种情况下,请在 WSL 中使用上面的 macOS 和 Linux 命令安装 Codex。

检查安装

codex --version

如果发现异常,codex doctor 会检查你的安装、配置、登录状态和运行时健康状况。

第 2 步:登录

在任意文件夹中运行 codex。首次运行时,它会要求你选择登录方式:

  • 使用 ChatGPT 登录:使用你的 ChatGPT 套餐,Codex 用量包含在你的订阅中。这是 Plus、Pro、Business、Edu 和 Enterprise 套餐的推荐选项。
  • 使用 API 密钥登录:用量计入你的 OpenAI API 账户。

你也可以通过命令行登录:

codex login                                            # browser sign-in
printenv OPENAI_API_KEY | codex login --with-api-key   # API key from stdin
codex login --device-auth                              # device code, for remote or headless machines
codex login status                                     # show the active sign-in
codex logout                                           # clear stored credentials

在服务器或没有浏览器的机器上,OpenAI 推荐使用设备码登录。Codex 会将凭据存储在 ~/.codex/auth.json 或操作系统的凭据存储中。

第 3 步:运行首个会话

打开你的项目并启动 Codex:

cd my-project
codex

用自然语言输入一个任务,例如“为注册表单添加输入校验,并为其编写测试”。以下几个斜杠命令值得第一天就了解:

命令作用
/init创建一个包含 Codex 指令的 AGENTS.md 文件
/model选择模型和推理强度
/permissions选择 Codex 无需询问即可执行的操作
/status显示当前会话配置
/review审查你的更改并找出问题

你可以在 AGENTS.md 中告诉 Codex 你的项目如何运作:如何运行测试、遵循哪些约定、需要避免什么。运行一次 /init,然后编辑生成的结果。

第 4 步:设置权限和默认值

在你设定的范围之外编辑文件或运行命令前,Codex 会先询问你。你可以用 /permissions 按会话更改此设置,也可以带参数启动 Codex:

codex --sandbox workspace-write --ask-for-approval on-request

沙箱模式有 read-only、workspace-write 和 danger-full-access。如需持久的默认设置,请在 ~/.codex/config.toml 中设置相同的选项:

model = "gpt-6.1-sol"
approval_policy = "on-request"
sandbox_mode = "workspace-write"

使用 codex --profile <name> 可在基础配置之上叠加一个命名配置文件,例如一个用于谨慎工作,另一个用于快速试验。

第 5 步:更新 Codex CLI

Codex 更新频繁。最简单的更新方式是:

codex update

或者使用你安装时所用的工具进行更新:

curl -fsSL https://chatgpt.com/codex/install.sh | sh   # standalone installer: rerun it
npm install -g @openai/codex@latest                    # npm
brew upgrade --cask codex                              # Homebrew

更新后运行 codex --version 确认新版本。

实用的 Codex CLI 命令

命令作用
codex启动交互式会话
codex exec "<task>"以非交互方式运行任务,适用于脚本和 CI
codex review以非交互方式运行代码审查
codex resume恢复之前的会话(--last 恢复最近一次)
codex --worktree在新的 git worktree 中运行会话
codex mcp管理 MCP 服务器
codex app启动 Codex 桌面应用

在 Chief 中将 Codex 与 Claude Code 搭配运行

Codex CLI 安装并登录后,就可以在 Chief 中使用了。Chief 是一款 AI 智能体管理器:你设定目标,Chief 将其规划为多个里程碑,并用你选择的智能体执行每个里程碑。

  • Codex 和 Claude Code 在同一个计划中。 为每个项目设置默认智能体,并为任意里程碑选择 Codex 或 Claude Code。各自的优势所在请参阅 Claude Code 与 Codex 对比。
  • 运行你刚安装的 Codex。 Chief 中继可在 macOS、Linux 和 Windows 上运行,它会启动你机器上已登录你自己 ChatGPT 账户的 codex。你的代码留在原处。
  • 相互独立的工作并行运行。 Chief 会同时运行所有依赖已完成的里程碑,并对照完成标准检查每个结果。
  • 在 Codex 中与 Chief 对话。 通过 codex mcp 添加 Chief 的 MCP 服务器,无需离开会话就能询问 Chief 有什么事需要你。

用 Chief 运行 Codex。免费开始 →

了解更多搭配用法:Chief + Codex。


常见问题

如何安装 Codex CLI?

在 macOS 和 Linux 上,运行 curl -fsSL https://chatgpt.com/codex/install.sh | sh。在 Windows 上,运行 PowerShell 安装程序。你也可以使用 npm install -g @openai/codex 或 brew install --cask codex。然后运行 codex 并登录。

Codex CLI 能在 Windows 上使用吗?

可以。Codex CLI 借助 Windows 沙箱在 Windows 上原生运行,因此不需要 WSL。如果你的工具是 Linux 原生的,仍然可以使用 WSL2。

如何更新 Codex CLI?

运行 codex update,或者使用你安装时所用的工具更新:重新运行安装脚本、npm install -g @openai/codex@latest 或 brew upgrade --cask codex。

Codex CLI 免费吗?

Codex CLI 基于 Apache-2.0 许可证开源。使用它需要包含 Codex 的 ChatGPT 套餐,或按 token 计费的 OpenAI API 密钥。当前套餐请查看 OpenAI 的定价页面。

安装 Codex CLI 需要 Node.js 吗?

不需要。独立安装程序和 Homebrew 都不需要 Node.js。只有选择用 npm 安装时才需要 Node.js。

可以同时使用 Codex CLI 和 Claude Code 吗?

可以。为它们各自分配独立的分支或 git worktree,避免编辑相同的文件;或者让 Chief 规划工作,并将每个里程碑交给你选择的智能体。

More articles

AI Agent Orchestration Guide

What AI agent orchestration is, how multi-agent orchestration works for Claude Code and Codex, and a comparison of orchestration approaches. Free guide.

What Is Agentic Engineering?

Agentic engineering means directing AI coding agents that write and test code while you set goals and review results. How it differs from agentic coding.

AI Code Review at Scale

What AI code review is, the main types of AI code review tools, and five workflows for reviewing AI-generated code from several coding agents at once.

Claude Code and Codex Pricing

Claude Code pricing from $20/mo (Pro) to $200/mo (Max 20x), Codex pricing from ChatGPT Free to Pro, usage limits and a comparison table, checked Oct 2026.

From Single Agent to Multi-Agent

Already using one AI coding agent? Here's how to scale to multiple instances running in parallel — the concurrency patterns, worktree isolation, and task decomposition strategies that make it work.

Vibe Coding at Scale

Vibe coding with one agent is fun. Vibe coding with five is chaos — unless you have orchestration. Here's how to scale the vibe without losing the quality.

Why AI Agents Need an Orchestration Layer

Running AI coding agents in terminal tabs works for two. Five signs you've outgrown it, tabs vs an orchestration layer, and what to look for in one.

Multi-Provider AI Orchestration

Claude Code, Codex, Cursor, Aider — each excels at different work. Here's how to combine providers into one coordinated workflow and why cross-provider review catches bugs single-provider setups miss.

Use Chief From Your AI Assistant

Add Chief's remote MCP server to Claude, Claude Code or Codex. Ask what's waiting on you, catch up on what shipped and propose milestones from chat.

Claude Code vs Codex: When to Use Each

Codex vs Claude Code: when each fits, a comparison table and FAQ. Claude Code vs Codex isn't either-or; Chief runs both on your projects.

Claude Code vs Cursor: When to Use Each

Cursor vs Claude Code: when each fits, a comparison table and FAQ. Claude Code vs Cursor isn't either-or; keep Cursor and let Chief run Claude Code.

How to Run Multiple Claude Code Agents in Parallel

Run multiple Claude Code agents in parallel, step by step: worktrees, background agents, subagents and review. Then let Chief orchestrate them for you.

Git Worktrees for AI Coding Agents

How to use git worktrees with Claude Code and Codex: one per agent, parallel runs, .env files, merging and cleanup. Every command, step by step.

How to Run Claude Code From Your Phone

Run Claude Code from your phone: Remote Control, cloud sessions in the Claude mobile app, or SSH and tmux, step by step. Plus approving agents in Discord.

The Best AI Coding Agents in 2026

The best AI coding agents of 2026: Claude Code, Codex, Cursor, Copilot and more agentic AI coding tools compared, and how to run several with Chief.

Subagents vs Parallel Agents

What is a subagent? How Claude Code subagents and Codex subagents work, how they differ from parallel agents, and when to use each one.

Ready to manage your agents?

_Codex 是 OpenAI 的商标。Claude Code 是 Anthropic 的商标。Chief 是一款独立产品,与上述两家公司均无关联,也未获得其赞助或背书。_