PageSourceSearch

https://csguide.cn/assets/codex-install-tutorial.html-B2adPBzj.js

js csguide.cn collected 2026-10-08 05:23:09 UTC 19,128 bytes, 8 lines download raw bytes

1import{_ as i,c as t,e as s,o as a}from"./app-BGx5hZDv.js";const n={};function l(r,e){return a(),t("div",null,[...e[0]||(e[0]=[s(`<p>大家好,今天给大家分享一下 OpenAI Codex 和 Codex CLI 安装使用教程,全部是基于 2025-09 最新官方与社区资料整理,适用于 Windows / macOS / Linux 全平台,手把手带你完成「OpenAI Codex CLI」的安装、配置与首次运行。全程约 5-10 分钟,照着做即可。</p><figure><img src="https://cdn.how2cs.cn/gzh/2025-09-18-161133.png" alt="codex 入口" tabindex="0" loading="lazy"><figcaption>codex 入口</figcaption></figure><p>一周前 OpenAI 突然宣布:<strong>ChatGPT Plus 用户无需 API Key,直接登录就能用 Codex CLI</strong>,我立刻冲了回去。</p><p>并且全网都在说这次 openai 编码能力提升很大,所以安装了一波 Codex Cli,结果越用越上头,于是有了这篇「踩坑 + 对比」流水账,方便大家初次使用。</p><p><strong>但是先说目前 Codex 和 Claude Code 一样需要付费才能使用,如果你还是免费用户,可以试试这个一键升级工具:</strong></p><p><strong>国内自助升级 ChatGPT Plus 工具</strong>: <a href="https://getgpt.pro/i/csguide-codex1" target="_blank" rel="noopener noreferrer">getgpt.pro</a> , <strong>详细教程</strong> <a href="https://csguide.cn/private/chatgpt-plus-tutorial.html" target="_blank" rel="noopener noreferrer">国内升级 ChatGPT 手把手教程</a></p><p>如果你还没 ChatGPT 账户,可以去用谷歌邮箱、微软邮箱等注册一个,或者直接在这里几块钱购买一个成品账号登录都可以:</p><p><a href="https://chatgptzh.com.cn/gpt" target="_blank" rel="noopener noreferrer">https://chatgptzh.com.cn/gpt</a></p><figure><img src="https://cdn.how2cs.cn/gzh/2025-09-18-161255.png" alt=" codex 使用场景" tabindex="0" loading="lazy"><figcaption> codex 使用场景</figcaption></figure><p>另外,最近codex 新用户较多,建了一个 codex 的交流群,大家可以互相交流经验:</p><figure><img src="https://cdn.how2cs.cn/gzh/2025-09-23-162203.jpg" alt="" tabindex="0" loading="lazy"><figcaption></figcaption></figure><hr><h2 id="一、安装前准备" tabindex="-1"><a class="header-anchor" href="#一、安装前准备"><span>一、安装前准备</span></a></h2><ol><li><strong>Node.js ≥ 18</strong>(必须)<br> 终端执行 <code>node -v</code> 检查,若低于 18 或提示未找到,先去 <a href="https://nodejs.org" target="_blank" rel="noopener noreferrer">nodejs.org</a> 下载 LTS 版并安装。</li><li><strong>Git ≥ 2.0</strong>(建议)<br> 用于后续拉取示例项目,执行 <code>git --version</code> 确认。</li><li><strong>可用网络</strong><br> 国内若遇到 registry 超时,可临时切换 npm 镜像:<br><code>npm config set registry https://registry.npmmirror.com</code></li></ol><hr><h2 id="二、一键安装-codex-cli" tabindex="-1"><a class="header-anchor" href="#二、一键安装-codex-cli"><span>二、一键安装 Codex CLI</span></a></h2><h3 id="_1-通用方式-npm-全局" tabindex="-1"><a class="header-anchor" href="#_1-通用方式-npm-全局"><span>① 通用方式(npm 全局)</span></a></h3><div class="language-bash line-numbers-mode" data-highlighter="shiki" data-ext="bash" data-title="bash" style="--shiki-light:#383A42;--shiki-dark:#abb2bf;--shiki-light-bg:#FAFAFA;--shiki-dark-bg:#282c34;"><pre class="shiki shiki-themes one-light one-dark-pro vp-code"><code><span class="line"><span style="--shiki-light:#4078F2;--shiki-dark:#61AFEF;">npm</span><span style="--shiki-light:#50A14F;--shiki-dark:#98C379;"> install</span><span style="--shiki-light:#986801;--shiki-dark:#D19A66;"> -g</span><span style="--shiki-light:#50A14F;--shiki-dark:#98C379;"> @openai/codex</span></span></code></pre><div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0;"><div class="line-number"></div></div></div><ul><li>macOS/Linux 若报 EACCES,加 sudo 重新执行即可。</li><li>Windows 请使用「<strong>管理员 PowerShell</strong>」。</li></ul><h3 id="_2-备用方式-包管理器" tabindex="-1"><a class="header-anchor" href="#_2-备用方式-包管理器"><span>② 备用方式(包管理器)</span></a></h3><table><thead><tr><th>
1系统</th><th>命令</th></tr></thead><tbody><tr><td>macOS + Homebrew</td><td><code>brew install codex</code></td></tr><tr><td>Windows + Chocolatey</td><td><code>choco install codex-cli</code></td></tr><tr><td>Linux 二进制</td><td>下载 <a href="https://github.com/openai/codex-cli/releases" target="_blank" rel="noopener noreferrer">GitHub Release</a> 赋予可执行权限后放 <code>/usr/local/bin</code>。</td></tr></tbody></table><h3 id="_3-验证" tabindex="-1"><a class="header-anchor" href="#_3-验证"><span>③ 验证</span></a></h3><div class="language-bash line-numbers-mode" data-highlighter="shiki" data-ext="bash" data-title="bash" style="--shiki-light:#383A42;--shiki-dark:#abb2bf;--shiki-light-bg:#FAFAFA;--shiki-dark-bg:#282c34;"><pre class="shiki shiki-themes one-light one-dark-pro vp-code"><code><span class="line"><span style="--shiki-light:#4078F2;--shiki-dark:#61AFEF;">codex</span><span style="--shiki-light:#986801;--shiki-dark:#D19A66;"> --version</span></span>
2<span class="line"><span style="--shiki-light:#A0A1A7;--shiki-light-font-style:italic;--shiki-dark:#7F848E;--shiki-dark-font-style:italic;"># 输出版本号即安装成功</span></span></code></pre><div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0;"><div class="line-number"></div><div class="line-number"></div></div></div><hr><h2 id="三、配置-api-密钥" tabindex="-1"><a class="header-anchor" href="#三、配置-api-密钥"><span>三、配置 API 密钥</span></a></h2><p>Codex 需要 OpenAI 兼容格式的密钥,支持官方 key 或第三方中转。</p><ol><li><p><strong>拿到密钥</strong><br> • 官方:OpenAI 后台生成 <code>sk-xxx</code>(需绑定付款方式)。<br> • 国内中转:尽量避免使用这些平台,大多数都是降智的号且隐私得不到保证,尽量自己的号或者官网的 key</p></li><li><p><strong>写入环境变量</strong>(任选其一)</p><ul><li><strong>临时</strong>(当前窗口有效)<div class="language-bash line-numbers-mode" data-highlighter="shiki" data-ext="bash" data-title="bash" style="--shiki-light:#383A42;--shiki-dark:#abb2bf;--shiki-light-bg:#FAFAFA;--shiki-dark-bg:#282c34;"><pre class="shiki shiki-themes one-light one-dark-pro vp-code"><code><span class="line"><span style="--shiki-light:#A0A1A7;--shiki-light-font-style:italic;--shiki-dark:#7F848E;--shiki-dark-font-style:italic;"># macOS / Linux</span></span>
3<span class="line"><span style="--shiki-light:#A626A4;--shiki-dark:#C678DD;">export</span><span style="--shiki-light:#E45649;--shiki-dark:#E06C75;"> OPENAI_API_KEY</span><span style="--shiki-light:#383A42;--shiki-dark:#56B6C2;">=</span><span style="--shiki-light:#50A14F;--shiki-dark:#98C379;">&quot;sk-你的key&quot;</span></span>
4<span class="line"><span style="--shiki-light:#A0A1A7;--shiki-light-font-style:italic;--shiki-dark:#7F848E;--shiki-dark-font-style:italic;"># Windows PowerShell</span></span>
5<span class="line"><span style="--shiki-light:#E45649;--shiki-dark:#E06C75;">$env</span><span style="--shiki-light:#383A42;--shiki-dark:#ABB2BF;">:</span><span style="--shiki-light:#E45649;--shiki-dark:#E06C75;">OPENAI_API_KEY</span><span style="--shiki-light:#383A42;--shiki-dark:#56B6C2;">=</span><span style="--shiki-light:#50A14F;--shiki-dark:#98C379;">&quot;sk-你的key&quot;</span></span></code></pre><div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0;"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div></li><li><strong>永久</strong>(推荐)<br> 把上面 <code>export</code> 语句追加到 <code>~/.zshrc</code> 或 <code>~/.bashrc</code>,重开终端即可。</li></ul></li><li><p><strong>(可选)修改默认模型</strong><br> 新建文件 <code>~/.codex/config.toml</code>:</p><div class="language-toml line-numbers-mode" data-highlighter="shiki" data-ext="toml" data-title="toml" style="--shiki-light:#383A42;--shiki-dark:#abb2bf;--shiki-light-bg:#FAFAFA;--shiki-dark-bg:#282c34;"><pre class="shiki shiki-themes one-light one-dark-pro vp-code"><code><span class="line"><span style="--shiki-light:#E45649;--shiki-dark:#E06C75;">model</span><span style="--shiki-light:#383A42;--shiki-dark:#ABB2BF;">
5 = </span><span style="--shiki-light:#50A14F;--shiki-dark:#98C379;">&quot;gpt-3.5-turbo-instruct&quot;</span><span style="--shiki-light:#A0A1A7;--shiki-light-font-style:italic;--shiki-dark:#7F848E;--shiki-dark-font-style:italic;">   # 或 gpt-4o-codex</span></span></code></pre><div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0;"><div class="line-number"></div></div></div></li></ol><hr><h3 id="四、首次运行" tabindex="-1"><a class="header-anchor" href="#四、首次运行"><span>四、首次运行</span></a></h3><div class="language-bash line-numbers-mode" data-highlighter="shiki" data-ext="bash" data-title="bash" style="--shiki-light:#383A42;--shiki-dark:#abb2bf;--shiki-light-bg:#FAFAFA;--shiki-dark-bg:#282c34;"><pre class="shiki shiki-themes one-light one-dark-pro vp-code"><code><span class="line"><span style="--shiki-light:#4078F2;--shiki-dark:#61AFEF;">codex</span></span></code></pre><div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0;"><div class="line-number"></div></div></div><ol><li>若弹出浏览器认证,按提示登录 ChatGPT 账号(Plus/Pro 可用)。</li><li>认证完成后回到终端,出现 <code>&gt;</code> 提示符即进入交互模式。</li></ol><p><strong>快速体验</strong></p><div class="language-bash line-numbers-mode" data-highlighter="shiki" data-ext="bash" data-title="bash" style="--shiki-light:#383A42;--shiki-dark:#abb2bf;--shiki-light-bg:#FAFAFA;--shiki-dark-bg:#282c34;"><pre class="shiki shiki-themes one-light one-dark-pro vp-code"><code><span class="line"><span style="--shiki-light:#4078F2;--shiki-dark:#61AFEF;">codex</span><span style="--shiki-light:#50A14F;--shiki-dark:#98C379;"> &quot;用 Python 写一段快速排序,并加测试&quot;</span></span></code></pre><div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0;"><div class="line-number"></div></div></div><p>Codex 会流式输出代码,完成后自动停止。</p><hr><h3 id="五、常见问题速查" tabindex="-1"><a class="header-anchor" href="#五、常见问题速查"><span>五、常见问题速查</span></a></h3><table><thead><tr><th>现象</th><th>解决</th></tr></thead><tbody><tr><td><code>command not found</code></td><td>npm 全局目录未加入 PATH,可执行 <code>npm bin -g</code> 查看并把路径加到系统变量。</td></tr><tr><td><code>403 / No active subscription</code></td><td>免费账号无法使用 Codex,需升级 Plus/Pro,或改用支持中转的 key。</td></tr><tr><td>流式输出乱码/断流</td><td>检查终端是否支持 UTF-8;国内网络建议加代理或中转站。</td></tr></tbody></table><hr><h3 id="六、卸载-更新" tabindex="-1"><a class="header-anchor" href="#六、卸载-更新"><span>六、卸载 / 更新</span></a></h3><div class="language-bash line-numbers-mode" data-highlighter="shiki" data-ext="bash" data-title="bash" style="--shiki-light:#383A42;--shiki-dark:#abb2bf;--shiki-light-bg:#FAFAFA;--shiki-dark-bg:#282c34;"><pre class="shiki shiki-themes one-light one-dark-pro vp-code"><code><span class="line"><span style="--shiki-light:#A0A1A7;--shiki-light-font-style:italic;--shiki-dark:#7F848E;--shiki-dark-font-style:italic;"># 更新</span></span>
6<span class="line"><span style="--shiki-light:#4078F2;--shiki-dark:#61AFEF;">npm</span><span style="--shiki-light:#50A14F;--shiki-dark:#98C379;"> update</span><span style="--shiki-light:#986801;--shiki-dark:#D19A66;"> -g</span><span style="--shiki-light:#50A14F;--shiki-dark:#98C379;"> @openai/codex</span></span>
7<span class="line"><span style="--shiki-light:#A0A1A7;--shiki-light-font-style:italic;--shiki-dark:#7F848E;--shiki-dark-font-style:italic;"># 卸载</span></span>
8<span class="line"><span style="--shiki-light:#4078F2;--shiki-dark:#61AFEF;">npm</span><span style="--shiki-light:#50A14F;--shiki-dark:#98C379;"> uninstall</span><span style="--shiki-light:#986801;--shiki-dark:#D19A66;"> -g</span><span style="--shiki-light:#50A14F;--shiki-dark:#98C379;"> @openai/codex</span></span></code></pre><div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0;"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><hr><p>恭喜!到此你已经拥有「命令行里的 AI 编程搭档」。<br> 下一步可以:</p><ul><li><p>在 VS Code 装同名插件,实现「边写边生成」; <img src="https://cdn.how2cs.cn/gzh/2025-09-18-161625.png" alt="Codex" loading="lazy"></p></li><li><p>把 <code>codex</code> 写进 Makefile,一键自动补全单元测试;</p></li><li><p>阅读官方 <a href="https://github.com/openai/codex-cli/blob/main/docs/AGENTS.md" target="_blank" rel="noopener noreferrer">AGENTS.md</a> 规范,让 Codex 更懂你的项目结构。</p></li></ul>`,43)])])}const h=i(n,[["render",l]]),o=JSON.parse('{"path":"/private/codex-install-tutorial.html","title":"最新 OpenAI Codex CLI 国内安装配置教程(GPT-5模型、付费升级、全平台配置详解)","lang":"zh-CN","frontmatter":{"title":"最新 OpenAI Codex CLI 国内安装配置教程(GPT-5模型、付费升级、全平台配置详解)","date":"2025-09-24T00:00:00.000Z","updated":"2025-09-24T00:00:00.000Z","tags":["Codex","Codex CLI","OpenAI","GPT-5","ChatGPT Plus","终端工具","编程助手","代码生成","CLI","国内安装","付费升级","教程"],"description":"最全 Codex CLI(OpenAI 终端编程助手)2025年国内安装与使用教程,涵盖 GPT-5 模型、ChatGPT Plus 付费升级、全平台(Windows/macOS/Linux)配置、GitHub 连接、常见问题与真实体验对比,助你快速上手 Codex CLI。","editLink":false,"head":[["meta",{"name":"keywords","content":"Codex, Codex CLI, OpenAI Codex, GPT-5, ChatGPT Plus, 终端编程, 代码助手, 编程工具, 代码生成, CLI 安装, 国内使用, ChatGPT Plus 升级, Codex 教程, Codex CLI 配置, OpenAI 终端助手, GPT5 安装, 付费教程, GitHub 连接, Codex CLI 体验, Codex CLI 对比, Codex CLI 常见问题"}],["link",{"rel":"canonical","href":"https://csguide.cn/private/codex-install-tutorial.html"}],["meta",{"property":"og:url","content":"https://csguide.cn/private/codex-install-tutorial.html"}],["meta",{"property":"og:site_name","content":"编程指北-计算机学习指南"}],["meta",{"property":"og:title","content":"最新 OpenAI Codex CLI 国内安装配置教程(GPT-5模型、付费升级、全平台配置详解)"}],["meta",{"property":"og:description","content":"最全 Codex CLI(OpenAI 终端编程助手)2025年国内安装与使用教程,涵盖 GPT-5 模型、ChatGPT Plus 付费升级、全平台(Windows/macOS/Linux)配置、GitHub 连接、常见问题与真实体验对比,助你快速上手 Codex CLI。"}],["meta",{"property":"og:type","content":"article"}],["meta",{"property":"og:image","content":"https://cdn.how2cs.cn/gzh/2025-09-18-161133.png"}],["meta",{"property":"og:locale","content":"zh-CN"}
8],["meta",{"property":"article:tag","content":"Codex"}],["meta",{"property":"article:tag","content":"Codex CLI"}],["meta",{"property":"article:tag","content":"OpenAI"}],["meta",{"property":"article:tag","content":"GPT-5"}],["meta",{"property":"article:tag","content":"ChatGPT Plus"}],["meta",{"property":"article:tag","content":"终端工具"}],["meta",{"property":"article:tag","content":"编程助手"}],["meta",{"property":"article:tag","content":"代码生成"}],["meta",{"property":"article:tag","content":"CLI"}],["meta",{"property":"article:tag","content":"国内安装"}],["meta",{"property":"article:tag","content":"付费升级"}],["meta",{"property":"article:tag","content":"教程"}],["meta",{"property":"article:published_time","content":"2025-09-24T00:00:00.000Z"}],["meta",{"property":"article:modified_time","content":"2025-09-24T00:00:00.000Z"}],["script",{"type":"application/ld+json"},"{\\"@context\\":\\"https://schema.org\\",\\"@type\\":\\"Article\\",\\"headline\\":\\"最新 OpenAI Codex CLI 国内安装配置教程(GPT-5模型、付费升级、全平台配置详解)\\",\\"image\\":[\\"https://cdn.how2cs.cn/gzh/2025-09-18-161133.png\\",\\"https://cdn.how2cs.cn/gzh/2025-09-18-161255.png\\",\\"https://cdn.how2cs.cn/gzh/2025-09-23-162203.jpg\\",\\"https://cdn.how2cs.cn/gzh/2025-09-18-161625.png\\"],\\"datePublished\\":\\"2025-09-24T00:00:00.000Z\\",\\"dateModified\\":\\"2025-09-24T00:00:00.000Z\\",\\"author\\":[{\\"@type\\":\\"Person\\",\\"name\\":\\"编程指北\\",\\"url\\":\\"https://csguide.cn\\"}],\\"url\\":\\"https://csguide.cn/private/codex-install-tutorial.html\\",\\"description\\":\\"最全 Codex CLI(OpenAI 终端编程助手)2025年国内安装与使用教程,涵盖 GPT-5 模型、ChatGPT Plus 付费升级、全平台(Windows/macOS/Linux)配置、GitHub 连接、常见问题与真实体验对比,助你快速上手 Codex CLI。\\",\\"mainEntityOfPage\\":{\\"@type\\":\\"WebPage\\",\\"@id\\":\\"https://csguide.cn/private/codex-install-tutorial.html\\"},\\"publisher\\":{\\"@type\\":\\"Organization\\",\\"name\\":\\"编程指北\\",\\"url\\":\\"https://csguide.cn\\",\\"logo\\":{\\"@type\\":\\"ImageObject\\",\\"url\\":\\"https://csguide.cn/logo.png\\"}}}"]]},"headers":[{"level":2,"title":"一、安装前准备","slug":"一、安装前准备","link":"#一、安装前准备","children":[]},{"level":2,"title":"二、一键安装 Codex CLI","slug":"二、一键安装-codex-cli","link":"#二、一键安装-codex-cli","children":[{"level":3,"title":"① 通用方式(npm 全局)","slug":"_1-通用方式-npm-全局","link":"#_1-通用方式-npm-全局","children":[]},{"level":3,"title":"② 备用方式(包管理器)","slug":"_2-备用方式-包管理器","link":"#_2-备用方式-包管理器","children":[]},{"level":3,"title":"③ 验证","slug":"_3-验证","link":"#_3-验证","children":[]}]},{"level":2,"title":"三、配置 API 密钥","slug":"三、配置-api-密钥","link":"#三、配置-api-密钥","children":[{"level":3,"title":"四、首次运行","slug":"四、首次运行","link":"#四、首次运行","children":[]},{"level":3,"title":"五、常见问题速查","slug":"五、常见问题速查","link":"#五、常见问题速查","children":[]},{"level":3,"title":"六、卸载 / 更新","slug":"六、卸载-更新","link":"#六、卸载-更新","children":[]}]}],"git":{},"readingTime":{"minutes":4.01,"words":1203},"filePathRelative":"private/codex-install-tutorial.md","localizedDate":"2025年9月24日"}');export{h as comp,o as data};

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.