Open AIHow toArtificial Intelligence
AIOpen AICodex

How to Install and Use OpenAI Codex CLI: A Step-by-Step Guide

Author AvatarMihir Ahir
May 31, 2025
How to Install and Use OpenAI Codex CLI: A Step-by-Step Guide

The OpenAI Codex CLI tool lets you harness AI to assist with coding directly from your computer’s command line. It can generate code, fix tasks, or even build entire applications. This guide will walk you through setting it up and trying it out with a simple to-do app example, designed to be straightforward for anyone to follow!

Why Use This Tool?

Here’s what makes it stand out:

  • Saves Time: Speeds up coding by assisting.
  • Integrates easily: Works seamlessly with your existing tools.
  • Versatile: Handles both small fixes and large projects.

Let’s dive in!

Before You Start: Get Node.js

You’ll need Node.js installed first. It’s a runtime that lets you run JavaScript on your computer. Here’s how to set it up:

  1. Download It: Visit the Node.js website and select the version for your operating system (Windows, macOS, or Linux).
  2. Install It: Run the downloaded file and follow the installation instructions.
  3. Verify It: Open your command line—Terminal on macOS/Linux or Command Prompt on Windows—and type:
node --version

You should see a version number like v18.17.0. If not, reinstall and try again.

Note: Node.js is essential for the Codex CLI, so don’t skip this step!

Step 1: Install the Tool

Once Node.js is ready, install the Codex CLI by entering this command in your command line:

npm install -g @openai/codex

This installs the tool globally, so you can use it from anywhere on your computer.

Troubleshooting:

  • Ensure you have permission to install (on macOS/Linux, try adding sudo before the command).
  • Confirm your internet connection is active.
  • New to npm? Check out the npm documentation for more details.

Step 2: Add Your OpenAI Key

The tool requires an API key to connect to OpenAI’s services. Here’s how to set it up:

(1). Sign In: Go to platform.openai.com and log in or create an account.

(2). Generate a Key: Navigate to the “API Keys” section, click “Create New Secret Key,” name it (e.g., “Codex Key”), enable all permissions, and click “Create Secret.”

(3). Store It Safely: Copy the key to a secure location—it’s only shown once.

(4). Set It Up: Add the key to your system with the appropriate command:

macOS/Linux:

export OPENAI_API_KEY='your-key-here'

Windows (Command Prompt):

set OPENAI_API_KEY='your-key-here'

Windows (PowerShell):

$env:OPENAI_API_KEY='your-key-here'

 

Replace 'your-key-here' with your actual key.

(5). Keep It Private: Never share your key. You can save it in your environment variables to avoid retyping it.

(6). Verify Your Account: Ensure it’s active under the “General” settings on the OpenAI platform.

Step 3: Make a Project Space (Optional)

It’s a good idea to create a dedicated folder and use Git to track your changes:

Create a Folder:

mkdir codex-app
cd codex-app

Initialize Git:

git init

This sets up a workspace and lets you manage your project’s history.

Why Git?: It helps you revert mistakes easily. New to Git? Explore the Git documentation.

Step 4: Use the Tool

Launch the tool by typing:

codex

If Git isn’t configured, you might see a message, but you can proceed anyway.

For more information on the Codex CLI, refer to the OpenAI API documentation.

How It Works

  • AI Model: It uses 04-mini to perform tasks.
  • Safety Feature: Runs in suggest mode by default, asking for approval before making changes.
  • To enable full automation, type:
approval full-auto

Tip: Start with suggest mode to understand its behavior.

Try It: Make a To-Do App

Let’s create a simple to-do app:

(1). Give the Command:

Create a React web app with Shad CN that is a to-do app with persistence

(2). Wait: The tool will generate the app files.

(3). Run It: Follow the provided instructions, typically:

npm run dev

This launches the app in your browser, where you can add tasks.

Result: A functional app you can customize. Want to learn more? Check out React and Shad CN documentation.

Step 5: Watch the Cost

Using the Codex CLI incurs costs based on usage. Here’s how to monitor it:

  • Check Usage: Visit platform.openai.com and go to “Usage.”
  • Cost Estimate: A to-do app might cost around $0.71, but this varies.
  • Pro Tip: Monitor regularly to avoid surprises.
  • Curious about pricing? See the OpenAI pricing page

If Something Goes Wrong

Common issues and fixes:

  • Installation Fails: Verify permissions and internet connectivity.
  • Key Issues: Double-check the key and account status.
  • Tool Not Found: Reinstall or review your setup.
  • Odd Output: Rephrase your command.
  • Need more help? Visit OpenAI’s help pages.

Easy Tips

  • Review Output: Always check the generated code.
  • Leverage Git: Protect your work with version control.
  • Start Small: Test with simple tasks first.
  • Secure Your Key: Keep it confidential.

Wrap-Up

The OpenAI Codex CLI is a powerful way to integrate AI into your coding workflow. This guide walked you through installation and building a basic app. Now, experiment with it on your projects to streamline your coding!

Need assistance with an AI project? Explore our AI/ML Research & Development services to see how we tackle real-world challenges with AI.