Skip to content

Install and Configure Codex

Codex is a local AI programming assistant provided by OpenAI that offers intelligent code completion, code explanation, and error fixing capabilities in your local environment. As a local AI coding tool within the OpenAI ecosystem, Codex supports two authentication methods: ChatGPT account and API Key.

  • OpenAI Ecosystem Integration: If you’re already using ChatGPT or other OpenAI services, Codex integrates seamlessly
  • Simple Authentication Preference: Supports direct login with ChatGPT account, no separate API Key configuration needed
  • International Network Environment: Best experience in network environments with direct access to OpenAI services
  • Hagicode Multi-Model Support: As one of Hagicode’s executor options, supports multi-model switching

You can easily install Codex via NPM. Make sure you have Node.js installed.

Terminal window
npm install -g @openai/codex

After installation, verify the installation with:

Terminal window
codex --version

Codex supports two authentication methods: ChatGPT account login (recommended) or API Key.

This is the simplest method, requiring no separate API Key configuration.

Terminal window
codex login

After running this command, Codex will open a browser to guide you through the ChatGPT account login process.

If you prefer to use API Key for authentication, follow these steps.

  1. Visit OpenAI API Keys page
  2. Create a new API Key
  3. Set environment variable:
Terminal window
export OPENAI_API_KEY="your-api-key-here"

Or add the following to your Shell configuration file (like ~/.bashrc or ~/.zshrc):

Terminal window
echo 'export OPENAI_API_KEY="your-api-key-here"' >> ~/.bashrc
source ~/.bashrc

After completing installation and authentication configuration, you can start using Codex:

Terminal window
codex

On first run, Codex will guide you through initial configuration, including:

  • Selecting default model
  • Configuring proxy settings (if needed)
  • Setting preference options

Here are the common commands for Codex:

Terminal window
# Start Codex interactive mode
codex
# View Codex version information
codex --version
# View help information
codex --help
# Login with ChatGPT account
codex login
# Logout
codex logout
# View current configuration
codex config
# Run a one-time query
codex ask "How to implement a quicksort algorithm?"

Codex needs to access OpenAI services, so there are certain network environment requirements:

  • International Network: Use directly without additional configuration
  • Domestic Network: Requires proxy configuration or network optimization services

If you’re using Codex in a domestic network environment, it’s recommended to:

  1. Configure system-level proxy
  2. Set HTTP/HTTPS proxy environment variables:
Terminal window
export HTTP_PROXY="http://proxy.example.com:8080"
export HTTPS_PROXY="http://proxy.example.com:8080"
  1. Or set proxy in Codex configuration file

Codex uses OpenAI’s API services with the following billing methods:

  • ChatGPT Plus Subscription: If you have a ChatGPT Plus subscription, you can use Codex without additional payment
  • API Pay-as-you-go: When using API Key, billing is based on actual usage. For specific rates, please refer to the OpenAI Pricing page

💡 Tip: For more detailed information, please visit the Codex Official Documentation.