
Running OpenAI’s Codex CLI Locally with Ollama
I like knowing what’s happening on my machine. When I use a tool, I want to understand what it’s doing, where my code is going, and what I’m paying for. That’s why I spent some time getting OpenAI’s Codex CLI running locally through Ollama, and I think you should try it too.
Codex CLI is OpenAI’s open-source coding agent. It runs in your terminal, reads your codebase, edits files, and executes commands. By default, it connects to OpenAI’s cloud models. But with a single flag, you can point it at Ollama and run the whole thing on your own hardware. No API costs. No data leaving your machine. Just you and a local model doing work.
Why Run Locally?
There are a few reasons I find this worth doing.
First, privacy. We build software for partners across multiple industries. Some of that code shouldn’t leave our network. Running inference locally means the code stays on the machine.
Second, cost. Cloud API calls add up, especially when you’re experimenting. A local setup lets you iterate without watching a billing dashboard.
Third, and this is the one that matters most to me, understanding. We in software are sometimes too focused on layering services to solve our problems. Running a model locally strips away the abstraction. You see the hardware constraints. You feel the latency. You start to understand what these models actually need to do their job. That understanding makes you better at using the cloud versions too.
What You Need
Before we start, here’s what you’ll need:
- Node.js (for installing the Codex CLI via npm)
- Ollama installed and running (ollama.com/download)
- A machine with at least 16 GB of RAM (for the 20B parameter model)
- Some patience for the initial model download
If you have a machine with a decent GPU or an Apple Silicon Mac, you’ll have a better experience. But the 20B model will run on CPU. It’s just slower.
Step 1: Install Ollama
If you don’t already have Ollama, grab it from ollama.com/download. It’s available on macOS, Linux, and Windows.
On Mac, you can also install it with Homebrew:
brew install ollama
Once installed, start the Ollama service. On macOS, it runs as a menu bar app. On Linux, you can start it with:
ollama serve
Step 2: Pull the Model
Codex works with OpenAI’s open-weight gpt-oss models. There are two sizes:
- gpt-oss:20b (21 billion parameters, needs about 16 GB of memory)
- gpt-oss:120b (117 billion parameters, needs an 80 GB GPU)
For most developers on a laptop or desktop, the 20B model is the practical choice.
ollama pull gpt-oss:20b
This will take a few minutes depending on your connection. The model uses OpenAI’s MXFP4 quantization format, which Ollama supports natively. That quantization is what makes it possible to fit a 21 billion parameter model into 16 GB of memory.
Step 3: Install Codex CLI
Install the Codex CLI globally:
npm install -g @openai/codex
You can verify it installed correctly by running:
codex --version
Step 4: Run Codex with Ollama
This is where it gets fun. Navigate to a project directory and run:
codex --oss
That --oss flag tells Codex to use your local Ollama instance instead of OpenAI’s cloud. By default, it will use gpt-oss:20b.
If you want to use the larger model (and you have the hardware for it):
codex --oss -m gpt-oss:120b
Codex will start up its terminal UI, connect to the Ollama server running on localhost:11434, and you’re in business. You can ask it to read files, write code, run tests, and explain what’s happening in your codebase.
The Quick Setup Option
Ollama also provides a convenience command that handles configuration for you:
ollama launch codex
This sets up the connection between Codex and Ollama automatically. If you just want to configure without launching, you can run:
ollama launch codex –config
Setting Up Persistent Configuration
If you plan to use this regularly (and I think you will), you’ll want to set up a configuration profile so you don’t have to pass flags every time.
Create or edit ~/.codex/config.toml and add:
[model_providers.ollama]
name = "Ollama"
base_url = "http://localhost:11434/v1"
[profiles.local]
model = "gpt-oss:20b"
model_provider = "ollama"
Then you can run Codex with your profile:codex --profile local
You can set up multiple profiles for different models or providers. This is useful if you want to switch between local and cloud models depending on the task.
What I’ve Noticed
I’ll be honest about the tradeoffs. The gpt-oss:20b model running locally on my machine is noticeably slower than the cloud models. For quick questions and small edits, it works well. For complex multi-file refactoring or large codebase analysis, you’ll feel the difference.
But here’s the thing. For the work I use it for most often (explaining unfamiliar code, writing quick utility functions, generating test scaffolding), the local model does the job. And it does it without me thinking about token costs or worrying about what code I’m sending to a third party.
One thing to watch, Codex recommends a context window of at least 32K tokens, and ideally 64K. Make sure your Ollama model configuration supports that. Check the Ollama context length documentation for how to adjust this for your model.
You Can Also Use Other Models
One of the nice things about this setup is that Ollama supports a wide range of models. The --oss flag defaults to gpt-oss:20b, but you can point Codex at other coding-focused models available in Ollama’s library, like DeepSeek Coder or Qwen. Just pass the model name with the -m flag.
Experimenting with different models on different tasks is a good way to build your intuition about what these models are actually good at. Not all models are created equal, and the best way to learn that is to try them.
Give It a Try
Setting up Codex with Ollama takes about ten minutes. The gpt-oss:20b model is released under an Apache 2.0 license, so you can use it without restrictions. The Codex CLI is open source too.
If you’re someone who likes to understand the tools you use (and if you’re reading this blog, I suspect you are), running a coding agent locally is worth the time. You’ll learn something about how these models behave, and you’ll have a useful tool that works even when you’re offline.
If you try it out, I’d like to hear about your experience. Reach out to me at @chadmichel on X.


