国内用户安装和使用codex

TwoAdmin 2025-10-30 3 10/30

第一步:安装 Codex 命令行工具

首先确保你的电脑有 Node.js 环境 (版本 >= 22) ,然后在终端里安装:

bash
# 使用国内镜像源安装,速度更快
npm install -g @openai/codex --registry=https://registry.npmmirror.com

安装完成后,可以用 codex --version 验证是否成功

 

第二步:配置中转服务和 API Key

这是最关键的一步。你需要一个能访问 OpenAI API 的代理服务地址和 API Key。

  • 如果已经有 API 中转服务地址和 Key,可以直接设置环境变量:

    bash
    export OPENAI_BASE_URL="https://你的中转地址/v1"
    export OPENAI_API_KEY="sk-你的API密钥"
  • 如果没有,可以参考社区推荐,使用像 cc-switch 这类工具自己搭建一个本地网关

配置完成后,在终端输入 codex 就可以开始使用了。你也可以指定模型:

bash
codex --model gpt-5.4

如果你想在 Codex 上使用 DeepSeek、文心一言等阿里云百炼暂未直接支持的模型,可以通过 cc-switch这个开源插件来实现。它的原理是在本地建立一个协议转换的桥梁,让 Codex 可以无缝调用这些国产模型的 API

操作步骤:

  1. 安装 cc-switch 插件:在你的 Codex 环境中安装该插件。具体安装命令请参考其 GitHub 项目页(搜索 farion1231/cc-switch

  2. 获取目标模型的 API Key

    • DeepSeek:访问 DeepSeek 开放平台获取。

    • 文心一言:访问百度智能云千帆平台获取。

    • 智谱 GLM:访问智谱 AI 开放平台获取。

    • Kimi:访问月之暗面 Kimi 开放平台获取

  3. 配置 cc-switch:在插件配置中填入你上一步获取的 API Key 和对应的模型名称。

  4. 安装 cc-switch 非常简单,推荐直接安装图形界面版 (GUI),方便管理和切换国内模型,已适配国内网络环境。

    🖥️ 方案一:安装图形界面版 (GUI) - 强烈推荐

    这是目前最新的 v3.10.3 桌面版,操作直观,支持一键切换 Codex、Claude Code 等工具的 API 供应商。

    1. Windows 用户

    • 推荐:下载 CC-Switch-v3.10.3-Windows.msi 安装包,双击按指引安装

    • 绿色版:下载 ...-Portable.zip,解压后直接运行 CC Switch.exe(免安装)。

    2. macOS 用户

    • 推荐 (Homebrew):打开终端,依次执行以下命令:

      bash
      brew tap farion1231/ccswitch
      brew install --cask cc-switch

      完成后在“启动台”找到图标启动

    • 手动安装:下载 ...-macOS.zip,解压后将 CC Switch.app 拖入“应用程序”文件夹。

      • 提示:首次打开若提示“无法验证开发者”,请去「系统设置」>「隐私与安全性」中点击“仍要打开”。

    3. Linux 用户

    • Debian/Ubuntu:下载 ...-Linux.deb 包,使用命令 sudo dpkg -i <文件名>.deb 安装。

    • 通用 AppImage:下载 ...-AppImage 文件,执行 chmod +x <文件名>.AppImage 赋予权限后运行

在终端启动 Codex

打开你的终端(命令行),直接输入:

bash
codex

Welcome to Codex, OpenAI's command-line coding agent Sign in with ChatGPT to use Codex as part of your paid plan or connect an API key for usage-based billing >

1. Sign in with ChatGPT Usage included with Plus, Pro, Business, and Enterprise plans

2. Sign in with Device Code Sign in from another device with a one-time code

3. Provide your own API key Pay for what you use Press enter to continue

在这个界面按 键选择 3. Provide your own API key,然后按回车:

text
> 3. Provide your own API key
     Pay for what you use

API key configured

Codex will use usage-based billing with your API key.

> You are in /Users/user

Do you trust the contents of this directory? Working with untrusted contents
comes with higher risk of prompt injection. Trusting the directory allows
project-local config, hooks, and exec policies to load.

› 1. Yes, continue
2. No, quit

选择1

注意:如果要正常使用codex,要在cc 选择openai 图标,把国内供应商加进去

- THE END -
Tag:

TwoAdmin

6月09日10:22

最后修改:2026年6月9日
0

非特殊说明,本博所有文章均为博主原创。