Skip to main content

OpenAI Codex CLI Complete Tutorial

Installation, Configuration and Usage Guide - Support for Direct API Connection in China
📖 Contents Covered:This article provides detailed instructions on how to install and configure OpenAI Codex CLI, combined with GPT Proto Aggregation API, which can be used directly in China without configuring a proxy. Supports Windows/Linux/Mac multi-platform, and can be configured with third-party IDEs such as VS Code.

1. Codex CLI Introduction and System Requirements

What is OpenAI Codex?

OpenAI Codex is an open-source command-line tool (CLI) that serves as a lightweight coding agent, capable of reading, modifying, and running code in the terminal. Codex is based on GPT models and is specifically optimized for code generation and understanding.

System Requirements


2. Codex CLI Installation Tutorial

Installation Steps (Using Ubuntu as Example)

Step 1: Update System Packages
Step 2: Add NodeSource Repository (Node.js 22) Visit Node.js official website for the latest version information.
Step 3: Install Node.js and npm
Step 4: Verify Installation
Step 5: Install Codex CLI Install Codex CLI globally via npm:
Step 6: Verify Installation
🎉 Congratulations! Codex CLI installation is complete!

macOS Installation Method

macOS users are recommended to use Homebrew for installation: 1. Install Homebrew (if not installed)
2. Install Node.js
3. Install Codex CLI

Windows (WSL) Installation Method

Windows users need to install WSL first, then follow the Ubuntu installation steps. 1. Install WSL Open PowerShell as administrator and run:
For detailed instructions, refer to Microsoft WSL Installation Documentation 2. Continue After Restarting After restarting, follow the Ubuntu installation steps above to complete Codex CLI installation.

3. Codex CLI Configuration Tutorial

Configure API Key

1. Get API Key
Recommended: Use GPT Proto Aggregation API (Direct Connection in China)Visit https://gptproto.com to get API Key
  • ✅ Direct connection in China, no proxy needed
  • ✅ Supports multiple OpenAI models
  • ✅ Affordable pricing, pay-as-you-go
2. Temporary Configuration (Valid for Current Session)
3. Permanent Configuration (Recommended) Edit shell configuration file:
Add the following content:
Make configuration effective:

Configure config.toml

Default configuration file path: ~/.codex/config.toml Official configuration documentation: Codex Configuration Documentation
Configuration Explanation:
  • model: Model name to use (e.g., gpt-4, gpt-3.5-turbo, etc.)
  • model_provider: Model provider configuration
  • base_url: API base address (using GPT Proto Aggregation API)
  • env_key: Environment variable name
  • wire_api: API type (chat or completions)
Basic Configuration Options

4. IDE Integration Configuration

VS Code Integration

For third-party IDEs (such as VS Code), you need to install the corresponding Codex plugin. Installation Steps
  1. Search for “OpenAI Codex” in VS Code extension store
  2. Install the official extension
  3. Configure Codex CLI path in settings
  4. Configure API Key (using the environment variable above)

Other IDE Support


5. Getting Started with Codex CLI

Initialize Project

Common Command Examples

1. Basic Prompt
2. Specify Model
3. Specify Operation Mode
4. Interactive Mode

Common Interactive Commands

Usage Scenario Examples

Scenario 1: Code Generation
Scenario 2: Code Review
Scenario 3: Bug Fixing
Scenario 4: Test Generation
Scenario 5: Documentation Generation

6. Troubleshooting and Common Issues

Common Issue Solutions

1. Permission Issues
Problem: Encountering permission error “EACCES: permission denied”Solution:
2. Node Version Issues
Problem: Node.js version too lowSolution:
3. Network Connection Issues
Problem: Cannot connect to APISolution:
  • Confirm API Key is configured correctly
  • Check if base_url configuration is correct
  • Use GPT Proto to ensure direct connection in China
  • Verify network connection: curl https://gptproto.com/v1/models
4. Invalid API Key
Problem: “Invalid API Key” errorSolution:

Verify Configuration

Debug Mode

Enable verbose log output to help diagnose issues:

7. Advanced Usage Tips

1. Custom Prompt Templates

Create commonly used prompt templates to improve efficiency:

2. Batch Processing

3. Git Integration

4. Configure Aliases

Add aliases in ~/.bashrc or ~/.zshrc:

8. Best Practice Recommendations


Official Resources

Third-Party Resources


Summary

Through this guide, you have successfully:
  • ✅ Understood basic concepts and features of OpenAI Codex CLI
  • ✅ Installed Codex CLI on Linux/macOS/Windows systems
  • ✅ Configured GPT Proto Aggregation API (direct connection in China)
  • ✅ Mastered basic usage methods and common commands
  • ✅ Understood IDE integration methods
  • ✅ Learned troubleshooting and problem solving
  • ✅ Mastered advanced usage tips and best practices
🚀 You can now start enjoying the efficient coding experience brought by Codex CLI!

Need Help?

If you encounter problems during use:
© 2025 OpenAI Codex CLI Tutorial | Complete Installation Configuration Guide Thank you for using Codex CLI - Let AI be your programming assistant Recommended to use GPT Proto for stable direct connection API service in China