This gets Claude Code running on an open-source model on your own machine. Nothing leaves your computer, nothing hits a meter, and the setup takes about twenty minutes.
Your coding agent runs on a meter. Every token, every session, every month, and the bill climbs the moment the work does.
There is a second cost most people never price in. Every file you hand the agent leaves your machine. For a side project, fine. For a client's codebase under an NDA, that is a conversation you do not want to have.
Here is the whole method before any setup. Install Ollama. Pull one open-source model onto your machine. Install Claude Code. Point it at the local model with two environment variables. Then run it like you always would, except now the model lives on your hardware and the work stays there.
What You Need First
Three things have to be on your machine before Step 1. If you already have them, skip ahead.
Node.js, version 18 or higher. Claude Code runs on it. Go to nodejs.org, click the green LTS button, run the installer, click through. Then check it:
Copy this.
node --version
You want to see something like v22.x.x. Any number 18 or higher is fine.
Python 3. You need it the moment you ask the agent to run a Python project. Go to python.org, open Downloads, grab the latest. On Windows, tick the box that says Add Python to PATH during install. On a Mac it is usually already there. Confirm it:
Copy this.
python3 --version
A terminal. You will type commands into it the whole way through.
| Operating System | How to open a terminal |
|---|---|
| Mac | Press Cmd + Space, type "Terminal", hit Enter |
| Windows | Press the Win key, type "PowerShell", hit Enter |
| Linux | Press Ctrl + Alt + T |
Two commands are all the terminal you need for this. Type cd foldername to move into a folder. Type cd .. to go back up one level. That is the whole skill.
Step 1: Install Ollama
Ollama is the tool that runs open-source models on your own machine. Go to ollama.com, download the installer for your operating system, run it, follow the prompts.
If you already have Ollama, update it to the latest version first. Older builds do not expose the interface Claude Code needs, and you will chase errors that were never your fault.
Once it is installed, Ollama runs quietly in the background. Confirm it by opening your browser and going to:
Copy this.
http://localhost:11434
If the page says "Ollama is running," you are set.
Now the one setting people skip. Open the Ollama app, go to Settings, and raise the context length to 32,000 or 64,000. This is the model's working memory. Claude Code leans on it hard for tool-calling and file edits, and if you leave it at the default, you will get errors that look like the model is broken when the real problem is the setting.
Change it, then leave Ollama running. Do not fully quit the app. Minimize it or let it sit in the system tray.
Step 2: Pull a Model
The model is the actual brain. Which one you pull comes down to how much machine you have.
| Your machine | Model | Command |
|---|---|---|
| Strong (32GB+ RAM, M-series Mac or modern GPU) | qwen2.5-coder:32b | ollama run qwen2.5-coder:32b |
| Mid-range (16GB RAM) | qwen2.5-coder:14b | ollama run qwen2.5-coder:14b |
| Lighter (8GB RAM) | qwen2.5-coder:7b | ollama run qwen2.5-coder:7b |
The rule is simple. Bigger models write better code and need more horsepower. The 7B class is fine for chatting and small jobs, but it starts to buckle on complex tool-calling and multi-file work. If you want a coding agent you can actually lean on, aim for the 14B or 32B class.
Run the command for your machine. The first time, Ollama downloads the model, which takes a few minutes depending on your connection. You get a progress bar. When it lands, you drop into a chat prompt. Type "hi" to confirm it answers, then type /bye to leave the chat.
Want to see everything you have downloaded? Run:
Copy this.
ollama list
Step 3: Install Claude Code
Claude Code is Anthropic's command-line coding agent. Out of the box it talks to Anthropic's servers. In the next step you point it somewhere else, but first install it.
Open your terminal and run:
Copy this.
npm install -g @anthropic-ai/claude-code
That uses the Node.js you installed earlier to put Claude Code on your machine globally. About a minute. Then check it:
Copy this.
claude --version
You should see a version number, 2.17 or higher at the time of writing. Older number? Re-run the install command and it updates itself.
Step 4: Point Claude Code at the Local Model
This is the part that does the work. You tell Claude Code to talk to your local Ollama model instead of Anthropic's servers, and you do it with two environment variables.
Mac or Linux:
Copy this.
export ANTHROPIC_API_KEY=ollama export ANTHROPIC_BASE_URL=http://localhost:11434/v1
Windows (PowerShell):
Copy this.
$env:ANTHROPIC_API_KEY = "ollama" $env:ANTHROPIC_BASE_URL = "http://localhost:11434/v1"
The API key is not a real key here. Ollama does not check it. It just needs a value in the slot, and "ollama" is as good as any.
Now launch the agent and name the model:
Copy this.
claude --model qwen2.5-coder:32b
Swap qwen2.5-coder:32b for whatever you pulled in Step 2. The name has to match exactly. If you are not sure, run ollama list and copy it.
Claude Code starts and asks whether you trust the files in the current folder. Type yes. That is it. You are running Claude Code on a model that lives on your own hardware.
Make It Stick
The export commands above only last for the terminal window you typed them in. Close it, open a fresh one, and they are gone. Set them once, permanently, and skip the retyping.
Mac or Linux: Add the two lines to your shell config, then reload it.
Copy this.
echo 'export ANTHROPIC_API_KEY=ollama' >> ~/.zshrc echo 'export ANTHROPIC_BASE_URL=http://localhost:11434/v1' >> ~/.zshrc source ~/.zshrc
If you are on Bash instead of Zsh, swap .zshrc for .bashrc. Not sure which you have? Most current Macs use Zsh.
Windows: Open PowerShell as Administrator and run:
Copy this.
[Environment]::SetEnvironmentVariable("ANTHROPIC_API_KEY", "ollama", "User") [Environment]::SetEnvironmentVariable("ANTHROPIC_BASE_URL", "http://localhost:11434/v1", "User")
After this, any terminal you open already knows. Just run claude --model qwen2.5-coder:32b and go.
Step 5: Give It a Real Job
Do not trust a setup until you have watched it build something. Make a fresh folder and move into it.
Copy this.
mkdir ~/Desktop/test-project cd ~/Desktop/test-project
Launch the agent. If you made the variables permanent, this line is all you need.
Copy this.
claude --model qwen2.5-coder:32b
Then give it something with moving parts.
Copy this.
Create a snake game in Python using Pygame and save it as snake.py
The agent thinks it through, writes the code, and creates the file. The first time it asks permission to create and edit files. Say yes, or pick "allow all edits during this session" so it stops asking.
When the file exists, open a second terminal window, move into the same folder, and run it.
Copy this.
cd ~/Desktop/test-project python3 snake.py
A game window pops up. Done. You have Claude Code running locally, for free, entirely on your own hardware.
The Four Things That Break It
When a local setup misbehaves, it is almost always one of these. Check them in order.
Ollama is not running. The agent cannot reach a model that is not on. Visit http://localhost:11434 in your browser. If it does not say "Ollama is running," restart the app.
The context length is still on default. If you skipped the 32K or 64K setting in Step 1, you get errors the moment the agent tries to touch files or call tools. Go back to Ollama, Settings, and raise it.
The model is too small for the job. The 2B to 7B models chat fine and choke on real coding. If you are getting garbage or repeated failures on a multi-file task, step up a size class.
The model name is wrong. The name after --model has to match what Ollama pulled, character for character. Run ollama list and copy the exact string.
One more that trips people up: the environment variables reset when you close the terminal, unless you made them permanent in Step 4. If the agent suddenly forgets it is talking to Ollama, that is why.
Is a Local Model as Good as the Hosted One?
No, and you should know that before you cancel anything. A 32B model on your laptop is not the frontier model on Anthropic's servers. It reasons less deeply on hard problems, and it runs slower because your machine is doing all the work a data center used to do. On a big, tangled task you will feel the gap.
That is the honest trade. What you get back is worth it. There is no monthly bill and no token meter running in the background. And nothing you feed the agent leaves the room, which is the part that matters when the code belongs to a client who was promised it would stay put.
Use local for the everyday work, the private work, and anything you would rather not send over a wire. Keep a hosted model in your back pocket for the jobs where you need the extra reasoning and the file is not sensitive. The setup here does not lock you into either. Change the model name and you have changed engines.
If you would rather keep a cloud model in the loop and just swap which one runs the work, that is a different setup with its own tradeoffs, and I walk through it in running Claude Code through a router. This guide is the other end of the spectrum: the cloud is out of the picture entirely.
Do This One Thing Today
Pull one model and run the snake test. Not all three sizes, not a perfect model choice, just the one that fits your machine from the table in Step 2.
Twenty minutes from now you will know whether a local agent handles your kind of work, and you will know it from watching it build something instead of reading about it. If it does not fit, you spent twenty minutes and a few gigabytes of disk. If it does, you just took the meter and the privacy problem off the table in the same afternoon.