简繁EN
← ToolsTools · Guide

Installing Codex from Scratch (with cc-switch for API Setup)

Install Codex, then sign in with ChatGPT or use cc-switch to set up and switch API providers. Includes common problems and fixes.

Date
Reading4 min read
Tags#Codex#cc-switch

Codex is OpenAI’s AI coding assistant for the terminal. There are two ways to sign in:

  • ChatGPT account: the easiest option if you have a paid ChatGPT plan. Usage counts toward your plan’s limits.
  • API key: an official OpenAI key or a third-party compatible service. In this case, use cc-switch to manage it. You add and switch providers with a few clicks instead of editing config files by hand.

cc-switch is free and open source. Its only official sources are ccswitch.io and farion1231/cc-switch on GitHub. Fake sites have been asking for money lately; don’t pay anyone for it.

What you need

  • A Mac, Windows or Linux computer. Codex now installs directly on Windows; you no longer need WSL.
  • A paid ChatGPT account, or a working API key

1. Install Codex

The official install script is the recommended way, and you don’t need Node.js first.

macOS and Linux:

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

Windows (PowerShell):

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

When it finishes, open a new terminal window and check that it worked (you’ll see a version number):

codex --version

Other ways to install

On a Mac you can also use Homebrew:

brew install --cask codex

If you prefer npm (Node.js must be installed first):

npm install -g @openai/codex

2. Sign in with your ChatGPT account

codex login

Your browser opens; sign in to ChatGPT and approve access. Running codex for the first time also offers to sign you in. To check whether you’re signed in:

codex login status

If you sign in this way, skip steps 3 to 5 and go straight to step 6.

3. For an API key: install cc-switch

macOS (Homebrew recommended):

brew install --cask cc-switch

To update later:

brew upgrade --cask cc-switch

You can also download the .dmg from the Releases page and double-click to install.

Windows: download the .msi (installer) or -Portable.zip (no install needed) from the Releases page.

Linux: download the .deb, .rpm or .AppImage from the Releases page.

4. Set up your API in cc-switch

  1. Open cc-switch. On first launch it imports your existing settings as a card named default; if you have none, ignore it.
  2. Choose Codex at the top, then click the + button in the toolbar to add a provider.
  3. Pick your provider from the presets. The name and endpoint are filled in for you.
  4. Enter your API key and save.
  5. Back on the main screen, select the card and click Enable.

Provider not in the presets? Choose a custom setup and enter your API key and endpoint (base URL). cc-switch writes them to ~/.codex/config.toml. It only changes the endpoint, key and model, keeps the rest of your settings, and backs up the file before changing it.

Relay service reports a format error? Codex now only supports OpenAI’s Responses API format. Services that only support the older Chat Completions format show a “routing required” badge on their card. Turn on local routing under cc-switch’s Settings → Routing → Local Routing to use them.

5. Check that it works

Unlike Claude Code, Codex doesn’t pick up a switch while it’s running. After enabling a new card, quit any open Codex session, open a new terminal and run:

codex exec "hello"

If you get a normal reply, installation and API setup are done.

6. Start using it

Go to your project folder and start an interactive session:

cd your-project
codex

Ask a single question without entering interactive mode:

codex exec "explain the structure of this project"

Inside Codex, the commands you’ll use most are /model (switch model and reasoning effort), /permissions (choose which actions don’t need your approval) and /new (start a new conversation). Model names change as new versions come out, so picking from the /model list is easiest. Run /init once at the start of a project to create an AGENTS.md file, the Codex equivalent of Claude Code’s CLAUDE.md. For more, see A Codex Command Cheat Sheet.

7. Switch providers

When one provider runs out of credit, select another card in the cc-switch main window or its menu bar (system tray) icon, click Enable, then reopen Codex. No config files to edit.

Common problems

Problem Fix
command not found: codex The install folder isn’t on your PATH. Open a new terminal and try again; if that doesn’t help, rerun the install command from step 1
Not sure what’s wrong Run the built-in checker, codex doctor. It checks your installation, settings and sign-in
Still using the old settings after switching Quit Codex and open it again. If that doesn’t help, check for environment variables you set yourself: run echo $OPENAI_API_KEY. If it prints anything, delete the matching export line from ~/.zshrc and reopen the terminal
Relay service reports a format error Turn on local routing in cc-switch (Settings → Routing → Local Routing)
How to update Codex Run codex update, or rerun the install command from step 1
Can’t connect from mainland China Set a proxy: export HTTPS_PROXY=http://your-proxy-address