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

OpenAI's Codex CLI brings autonomous coding to the terminal. This guide covers Node.js setup, npm installation, authentication, and first commands across macOS, Linux, and Windows.

axonn bots
axonn bots
·3 min read
This guide covers installing OpenAI Codex CLI via npm, Homebrew, or official scripts on macOS, Linux, and Windows. It explains Node.js prerequisites, ChatGPT versus API key authentication, approval modes, AGENTS.md configuration, and common installation pitfalls.

OpenAI's Codex CLI is an autonomous coding agent that lives in your terminal. It can read your codebase, propose edits, run tests, and even execute shell commands, depending on how much autonomy you grant it. Getting it running takes about ten minutes if you know the right sequence, and slightly longer if you hit the common pitfalls that trip up first-time users.

Prerequisites

Codex CLI requires Node.js 18 or higher, though Node 22 is the safer choice. It runs on macOS 13+, Ubuntu 22.04+, and Windows 11. On Windows, WSL2 is strongly recommended for full sandbox support, though recent releases have improved native compatibility. You also need a Git repository initialized in your project directory, because Codex uses Git context to understand structure and propose changes.

Authentication requires either a ChatGPT subscription (Plus, Pro, Business, Edu, or Enterprise) or an OpenAI API key. The ChatGPT route bills against your existing subscription. The API key route uses standard metered billing and is the better fit for CI/CD pipelines or shared team accounts.

Installation

There are four practical paths, depending on what is already on your machine.

npm (most portable):

Bash
npm install -g @openai/codex

Homebrew (macOS / Linux):

Bash
brew install --cask codex

Official script (macOS / Linux, no Node required):

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

PowerShell (Windows):

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

Verify the install with codex --version. The package updates frequently, sometimes several times a month, so avoid hardcoding version numbers in scripts.

Authentication

For interactive use, launch Codex and follow the browser prompt:

Bash
codex

For API key auth, export the key first:

Bash
1export OPENAI_API_KEY="sk-your-key-here" 2codex

Do not paste the key into shell history or committed files. Use a secrets manager or a tool like direnv to scope the key to a project directory.

First Session and Approval Modes

Start inside a Git repo and try something small:

Bash
1cd ~/projects/my-test-repo 2codex

At the prompt, type a verifiable task:

Plain Text
> Add a .gitignore entry for node_modules and .env, then explain what you changed.

Codex will read the repo, propose a diff, explain its reasoning, and ask for confirmation. This is the default suggest approval mode. Other modes include auto-edit, which applies safe file changes without asking, and full-auto, which can run shell commands. Full-auto is designed for sandboxed CI environments, not your main development machine.

Configuration and AGENTS.md

Create an AGENTS.md file at your repo root to give Codex standing instructions: coding conventions, test commands, directories to avoid. Without this file, Codex infers everything from scratch every session, which is the root cause of most "it edited the wrong thing" complaints.

MARKDOWN
1# AGENTS.md 2## Project 3Express.js REST API, Node 22, Jest for tests. 4## Commands 5- Install: npm install 6- Test: npm test 7## Conventions 8- Use async/await, not .then() chains. 9- Never edit files under /generated.

Common Pitfalls

  • Installing globally with sudo creates permission conflicts on update. Use a user-owned prefix or a version manager.
  • Running an outdated Node version below 18 causes subtle compatibility issues.
  • Skipping AGENTS.md leaves Codex guessing at your conventions.
  • Mixing ChatGPT and API key auth without tracking which is active leads to billing confusion. Check with codex login status.

Codex CLI is genuinely capable of autonomous work, which means it is also capable of autonomous mistakes. Start with suggest mode, read the reasoning, and scale up trust gradually.