TL;DR:
- Install Ollama, raise its context to 64,000 tokens, pull a model with tool support such as
qwen3.5:9b, install Claude Code as a separate user, then runollama launch claude --model qwen3.5:9b. - Ollama gives models 4,096 tokens of context without a GPU. Set
OLLAMA_CONTEXT_LENGTH=64000for Ollama andCLAUDE_CODE_MAX_CONTEXT_TOKENS=64000for Claude Code. - The model must list
toolsinollama show, or every request fails withAPI Error: 400. - Ollama's API has no authentication and listens on
127.0.0.1:11434. Claude Code runs on the same server, so no firewall port needs to open. - Plan 16 GB RAM for
qwen3.5:9bat a 64K context. Ollama cloud models need only Claude Code's 4 GB minimum, but they send your code to Ollama's servers. - Claude Code needs no claude.ai login with Ollama, and Remote Control is unavailable with a custom
ANTHROPIC_BASE_URL.
Applies to: Ubuntu 26.04 LTS · Ollama 0.40.15 · Claude Code 2.1.319 · checked November 2026
Claude Code is Anthropic's agentic coding tool: a terminal program that reads a codebase, edits files and runs commands, and sends its model requests to an Anthropic-compatible API. Ollama is an open-source runtime, released under the MIT license, that downloads open-weight language models and serves them through a command line and a local API on port 11434. This tutorial sets up Claude Code with Ollama on a CPU-only Ubuntu 26.04 server, with a local model for short tasks and Ollama's cloud models for long ones.
Can Claude Code use Ollama models?
Yes. Ollama 0.14.0 and later accept Anthropic Messages API requests (announced January 16, 2026), and Ollama 0.15 added ollama launch (January 23, 2026), which sets Claude Code's connection variables and starts it. Claude Code then sends every model request to Ollama on the server.
Anthropic's page on LLM gateways states its position: "Anthropic doesn't endorse, maintain, or audit third-party gateway products, and doesn't support routing Claude Code to non-Claude models through any gateway."
To keep Claude models behind a proxy with a virtual key per app and fallbacks, see How to Self-Host LiteLLM Proxy with Docker Compose on Ubuntu 24.04.
Local models or cloud models: which to use on a CPU server
A local model runs on the server's CPU, and an Ollama cloud model, whose name ends in cloud, such as kimi-k2.7-code:cloud, runs on Ollama's servers. Both use the same commands.
| Local model | Ollama cloud model | |
|---|---|---|
| Where the model runs | On the server's CPU, in system RAM | On Ollama's servers |
| Prompts, file contents and command output | Stay on the server | Go to ollama.com |
| Server RAM | 16 GB for qwen3.5:9b at a 64K context | 4 GB, Claude Code's minimum |
| Account | None | An ollama.com account |
| Use for | Short tasks you start on demand | Long or unattended tasks |
A local model keeps the server's vCPU busy for the whole reply. Sustained full-CPU load is not permitted on Arct plans, so start local tasks when you need a result, and run long or unattended jobs on a cloud model.
Prerequisites
- A server running Ubuntu 26.04 LTS with at least 4 GB RAM for cloud models, the minimum in Anthropic's Claude Code setup guide, or 16 GB for a local model at a 64K context. On Arct Cloud, the
cvm.microandvm.microplans (2 vCPU, 4 GB RAM, 40 GB NVMe) meet the first figure, andcvm.xlarge(8 vCPU, 16 GB RAM, 150 GB NVMe) andhvm.medium(8 vCPU, 16 GB RAM, 160 GB NVMe) meet the second. - A non-root user with sudo privileges and SSH key login. How to Generate an SSH Key creates the key and the sudo user.
- A Git repository for Claude Code to work on, and its clone URL.
- For Step 7 only: an ollama.com account.
Steps 1 to 3 run as the sudo user. Steps 4 to 8 run as the coder user that Step 4 creates, except where a step says to use the sudo user. Replace values written in capitals, such as YOUR_REPO and YOUR_REPO_URL, with your own.
Step 1: Install Ollama and keep it private on the server
Ollama's installer stops with an error if zstd is missing. Install the tools this tutorial uses; apt skips any that are already present:
sudo apt update
sudo apt install -y curl zstd tmux git
Download Ollama's install script and read it:
curl -fsSL https://ollama.com/install.sh -o ollama-install.sh
less ollama-install.sh
If the script looks right, run it:
sh ollama-install.sh
The script asks for your sudo password, installs Ollama to /usr/local, and creates and starts the ollama systemd service under an ollama system user. The output looks similar to this:
>>> Installing ollama to /usr/local
...
>>> Creating ollama systemd service...
>>> Enabling and starting ollama service...
>>> The Ollama API is now available at 127.0.0.1:11434.
>>> Install complete. Run "ollama" from the command line.
WARNING: No NVIDIA/AMD GPU detected. Ollama will run in CPU-only mode.
Check the version:
ollama -v
ollama version is 0.40.15
At the time of testing, the script installed Ollama 0.40.15. Your output may show a newer version.
Ollama's local API has no authentication, so any program that reaches port 11434 can run prompts and pull or delete models. The service listens on the loopback address only:
ss -tln | grep 11434
LISTEN 0 4096 127.0.0.1:11434 0.0.0.0:*
Claude Code runs on the same server, so no firewall port needs to open. Keep OLLAMA_HOST on 127.0.0.1: a value such as 0.0.0.0 puts the open API on the internet.
Step 2: Raise the context length for coding agents
On a server without a GPU, Ollama gives every model 4,096 tokens of context. The service log records the default:
sudo journalctl -u ollama -o cat | grep "default context"
The output includes a line similar to this:
time=2026-11-06T18:02:53.980Z level=INFO source=routes.go:2586 msg="vram-based default context" total_vram="0 B" default_num_ctx=4096
Claude Code's first request carried 11,020 tokens in a -p test run. At 4,096 tokens, Ollama cuts that prompt, and the model answers without Claude Code's instructions. Ollama's documentation recommends at least 64,000 tokens for coding tools. Put the setting in a systemd drop-in file, which survives updates because the install script rewrites only ollama.service:
sudo mkdir -p /etc/systemd/system/ollama.service.d
sudo tee /etc/systemd/system/ollama.service.d/override.conf > /dev/null <<'EOF'
[Service]
Environment="OLLAMA_HOST=127.0.0.1:11434"
Environment="OLLAMA_CONTEXT_LENGTH=64000"
EOF
OLLAMA_HOST records the loopback address in one file. OLLAMA_CONTEXT_LENGTH sets the context for every local model, and RAM use grows with it. To keep every model request on the server, add Environment="OLLAMA_NO_CLOUD=1" as a third line before EOF; it turns off cloud models and web search, so leave it out if you plan to use Step 7.
Apply the change, give the service two seconds to start listening, and test the API:
sudo systemctl daemon-reload
sudo systemctl restart ollama
sleep 2
curl http://127.0.0.1:11434
Ollama is running
With OLLAMA_NO_CLOUD=1, the log shows msg="Ollama cloud disabled: true" after the restart.
Step 3: Pull a model that supports tools
Claude Code reads files, edits them and runs commands through tool calls, and Ollama rejects its requests for a model without tool support. On 16 GB RAM, pull qwen3.5:9b, a 6.6 GB download:
ollama pull qwen3.5:9b
The pull ends with success. Check that the model lists tools:
ollama show qwen3.5:9b
For qwen3.5:0.8b, the smaller model used to check these steps, the Capabilities section looks similar to this. Ollama's library lists the same capabilities for every Qwen3.5 size:
Capabilities
completion
vision
tools
thinking
levels false, true
default true
Load the model once and check the context it runs with:
ollama run qwen3.5:9b --think=false "Reply with the single word OK."
ollama ps
For the smaller test model, the ollama ps output looks similar to this:
NAME ID SIZE PROCESSOR CONTEXT RUNNER UNTIL
qwen3.5:0.8b de63045f2975 1.3 GB 100% CPU 64000 llamacpp 4 minutes from now
100% CPU confirms the model runs in system RAM, and 64000 confirms the Step 2 setting. Ollama unloads a model 5 minutes after the last request.
Step 4: Install Claude Code as a separate user
Commands that Claude Code runs on the server run as the Linux user who started it, so a user without sudo limits what an approved command can reach. Create a coder user and open a login shell as that user:
sudo useradd --create-home --shell /bin/bash coder
sudo -iu coder
If coder already exists from the Claude Code on a VPS tutorial, run only the second command. Install Claude Code with Anthropic's native installer, without sudo:
curl -fsSL https://claude.ai/install.sh | bash
The installer puts the launcher at ~/.local/bin/claude and ends with Installation complete!. Skip the PATH line it suggests: Ubuntu's ~/.profile adds ~/.local/bin once the folder exists. Reload the profile and check the version:
source ~/.profile
claude --version
2.1.319 (Claude Code)
Your output may show a newer version.
Claude Code 2.1.319 starts in auto mode, where a classifier model approves actions instead of you. Set Manual mode, which asks before edits and commands. Check for an existing settings file first:
cat ~/.claude/settings.json
If it shows "defaultMode": "default", go to the clone step below. If it exists without that line, open it with nano ~/.claude/settings.json and add the permissions block below inside its outer braces, after a comma. If cat reports No such file or directory, create the file:
mkdir -p ~/.claude
tee ~/.claude/settings.json > /dev/null <<'EOF'
{
"permissions": {
"defaultMode": "default"
}
}
EOF
"defaultMode": "default" is the setting for Manual mode. Clone the repository that Claude Code will work on into ~/projects:
mkdir -p ~/projects
git clone YOUR_REPO_URL ~/projects/YOUR_REPO
Replace YOUR_REPO_URL with the repository's clone URL. For a private repository, How to Run Claude Code on an Ubuntu 24.04 VPS with tmux adds a deploy key first.
Step 5: Start Claude Code with an Ollama model
Option A: Start it with ollama launch claude
ollama launch claude sets Claude Code's connection variables and starts it in the current folder:
cd ~/projects/YOUR_REPO
CLAUDE_CODE_MAX_CONTEXT_TOKENS=64000 ollama launch claude --model qwen3.5:9b
ollama launch points ANTHROPIC_BASE_URL at Ollama, sets ANTHROPIC_AUTH_TOKEN to ollama, sets Claude Code's Opus, Sonnet, Haiku and subagent models to the same model, and turns off Anthropic's server-side auto-mode checks, which Ollama does not provide. CLAUDE_CODE_MAX_CONTEXT_TOKENS tells Claude Code the context Ollama serves; without it, Claude Code assumes 200k tokens for an unknown model name and compacts only after Ollama has cut the prompt. Keep it equal to OLLAMA_CONTEXT_LENGTH.
On the first start, Claude Code asks for a text style and whether you trust the folder; trust only repositories you control. When it asks "Make auto mode your default permission mode?", choose No, keep manual mode, and the status bar reads manual mode on.
In the test run, /context showed 19.2k of the 64k tokens in use before the first message, 15.3k of them for tool definitions, and a 33k autocompact buffer, which left 11.8k for the conversation. If RAM allows, raise both context values together.
Option B: Set the variables yourself
Without ollama launch, export the connection and model variables that ollama launch sets before you start claude. Replace qwen3.5:9b on every line if you use another model:
export ANTHROPIC_BASE_URL=http://127.0.0.1:11434
export ANTHROPIC_AUTH_TOKEN=ollama
export ANTHROPIC_API_KEY=""
export ANTHROPIC_DEFAULT_OPUS_MODEL=qwen3.5:9b
export ANTHROPIC_DEFAULT_SONNET_MODEL=qwen3.5:9b
export ANTHROPIC_DEFAULT_HAIKU_MODEL=qwen3.5:9b
export CLAUDE_CODE_SUBAGENT_MODEL=qwen3.5:9b
export CLAUDE_CODE_MAX_CONTEXT_TOKENS=64000
claude --model qwen3.5:9b
Ollama ignores the token value, but Claude Code needs one. Settings in ~/.claude/settings.json override shell variables, so keep other base URLs and keys out of its env block.
The exports last until the shell closes, so open a new shell to use Anthropic's models again. ollama launch sets its variables only for the Claude Code process it starts.
Step 6: Run Claude Code in tmux or from a script
Keep a session running in tmux
A session inside tmux keeps running after SSH closes. As coder, open one in the repository and run the Step 5 command in it:
tmux new -s claude -c ~/projects/YOUR_REPO
Detach with Ctrl+B, then D. On your next login, reattach from the sudo user's shell:
sudo -iu coder tmux attach -t claude
A reboot ends the tmux session. Open a new one and add -- --continue to the Option A command, or --continue to the Option B claude command, to load the most recent conversation.
Run one task from a script
--yes runs ollama launch without prompts. It requires --model, pulls the model when it is missing, and passes everything after -- to Claude Code:
cd ~/projects/YOUR_REPO
CLAUDE_CODE_MAX_CONTEXT_TOKENS=64000 ollama launch claude --model qwen3.5:9b --yes -- \
-p "Summarize what this repository does." \
--tools "Read,Grep,Glob" \
--max-turns 3
-p prints the answer and exits. --max-turns 3 ends the task after three agent turns; if the task needs more, Claude Code prints Error: Reached max turns (3) and exits with status 1 without an answer. --tools "Read,Grep,Glob" limits Claude Code to reading and searching, and shortens every request: in the test run, the first request fell from 11,020 tokens to 3,296. With -p, Claude Code also prints a [claude-code:unrecognized_model] line before the answer, even with CLAUDE_CODE_MAX_CONTEXT_TOKENS set.
Step 7: Use Ollama cloud models (optional)
With a cloud model, Claude Code sends prompts, file contents and command output to ollama.com. Ollama's FAQ says it processes them to provide the service and does not store, log or train on them. These commands follow Ollama's Claude Code integration page.
If you added OLLAMA_NO_CLOUD=1 in Step 2, type exit to return to the sudo user, remove that line from override.conf, run the Step 2 systemctl commands again, then run sudo -iu coder. Sign in to your ollama.com account:
ollama signin
On a server without a browser, ollama signin prints a link that starts with https://ollama.com/connect. Open it in a browser on your computer and sign in; the link connects the server's Ollama service to your account.
Start Claude Code with a cloud model that supports tools, such as kimi-k2.7-code:cloud from Ollama's cloud catalog:
cd ~/projects/YOUR_REPO
ollama launch claude --model kimi-k2.7-code:cloud
For cloud models, ollama launch passes the model's context length and output token limit to Claude Code, so CLAUDE_CODE_MAX_CONTEXT_TOKENS is not needed.
Step 8: Verify Claude Code is using Ollama
In a Claude Code session, run /status. For the test model, it includes these lines:
Version: 2.1.319
...
Auth token: ANTHROPIC_AUTH_TOKEN
Anthropic base URL: http://127.0.0.1:11434
Model: qwen3.5:0.8b
Ask a read-only question, such as What does this repository do?. In a second SSH session, as the sudo user, check Ollama's request log:
sudo journalctl -u ollama -o cat | grep "/v1/messages" | tail -n 2
Each Claude Code request adds one line. For the test model, the output looks similar to this:
[GIN] 2026/11/06 - 19:28:33 | 200 | 22.11s | 127.0.0.1 | POST "/v1/messages?beta=true"
[GIN] 2026/11/06 - 19:29:02 | 200 | 9.53s | 127.0.0.1 | POST "/v1/messages?beta=true"
200 means Ollama answered. A 500 marks a request that was cancelled, for example with Esc, or cut off by an out-of-memory kill. For a local model, ollama ps shows 100% CPU and 64000 in the CONTEXT column while the session runs.
Which Ollama model works with Claude Code on a CPU?
Choose a model with tool support that fits RAM at a 64K context. The figures below come from the CPU benchmark of self-hosted LLMs, run on a 16 vCPU server with one request at a time. Servers with fewer vCPU generate more slowly.
| Server RAM | Model | Download | Measured on 16 vCPU |
|---|---|---|---|
| 4 GB | A cloud model, such as Kimi K2.7 Code | None | Runs on Ollama's servers |
| 16 GB | Qwen3.5 9B | 6.6 GB | Peak RAM 8.09 GiB at 32K and 11.24 GiB on a 128K attempt; 7.35 tokens/s after an 8K prompt |
| 32 GB | Qwen3.6 35B-A3B | 24 GB | Peak RAM 22.90 GiB at 8K; 14.24 tokens/s after an 8K prompt |
The Ollama tags for these models, in the same order:
kimi-k2.7-code:cloud
qwen3.5:9b
qwen3.6:35b-a3b-q4_K_M
Qwen3.6 35B-A3B is a mixture-of-experts model that uses part of its weights for each token, so it generates faster on a CPU than dense models of a similar size. Before you rely on it with 32 GB, run free -h while Claude Code processes its first request at 64K: in the test run, ollama ps showed 1.3 GB for the test model while its runner process used 2.7 GiB. Dense 27B models such as qwen3.8:27b generated 2.37 tokens/s, too slow for an agent that sends several requests per task. Qwen3.5 9B took 53.36 seconds to its first token on an 8K prompt, and Claude Code's first request is longer.
qwen3-coder:30b, which Ollama's January announcement recommended for Claude Code, is a 19 GB mixture-of-experts download with tool support and was not part of the benchmark. Best Ollama Models for CPU Servers by RAM Size lists more models.
What changes with a non-Claude model
On Claude Code 2.1.319 with Ollama 0.40.15:
- Remote Control is unavailable with a custom
ANTHROPIC_BASE_URL, and/remote-controlreturnedUnknown command: /remote-control. - Ollama does not support Anthropic's
cache_controlprompt-caching blocks,tool_choice, the token-counting endpoint, PDF input or citations. - Claude Code still sends usage metrics to Anthropic and third-party logging services. Anthropic's data usage page says they never include code, prompts or file paths, and
DISABLE_TELEMETRY=1turns them off.
Update Ollama and Claude Code
As the sudo user (type exit to leave the coder shell), download and read the install script again:
curl -fsSL https://ollama.com/install.sh -o ollama-install.sh
less ollama-install.sh
If the script looks right, run it to update Ollama to the latest release; the Step 2 drop-in stays in place:
sh ollama-install.sh
To install a specific release instead, such as the 0.40.15 checked here, run OLLAMA_VERSION=0.40.15 sh ollama-install.sh. Ollama published 9 releases between October 18 and November 3, 2026, with ollama launch claude fixes in 0.40.7 and 0.40.14, so read the release notes first.
Claude Code's native install updates itself in the background. To update now, run this as coder:
claude update
Current version: 2.1.319
Checking for updates to latest version...
Claude Code is up to date (2.1.319)
A new version applies at the next start: type /exit in tmux and start Claude Code again with --continue, as in Step 6.
Troubleshoot common Claude Code and Ollama errors
| Symptom | Cause | Fix |
|---|---|---|
API Error: 400 registry.ollama.ai/library/gemma3:270m does not support tools | The model has no tool support | Pick a model that lists tools in ollama show |
There's an issue with the selected model (qwen3.5:4b). It may not exist or you may not have access to it. | The model is not pulled, or its name has a typo | ollama list, then ollama pull the exact name |
API Error: Connection refused with (ECONNREFUSED), or Error: could not connect to ollama server | Ollama is stopped, or ANTHROPIC_BASE_URL is wrong | As the sudo user, sudo systemctl start ollama, then check /status |
Replies ignore your request; journalctl -u ollama shows msg="truncating input prompt" | The context is smaller than Claude Code's prompt | Set OLLAMA_CONTEXT_LENGTH=64000 (Step 2) |
Requests stall or retry with (ECONNREFUSED); journalctl -u ollama shows Failed with result 'oom-kill' | The model and context need more RAM than the server has | Use a smaller model or a cloud model |
Model "qwen3.5:9b" isn't recognized · context assumed to be 200k tokens at startup, or Claude Code doesn't recognize the model with -p | Claude Code does not know Ollama model names | Set CLAUDE_CODE_MAX_CONTEXT_TOKENS (Step 5) |
WARNING: Unable to detect NVIDIA/AMD GPU. Install lspci or lshw to automatically detect and install GPU dependencies. | pciutils is not installed | No action on a CPU server; ollama -v confirms the install |
| The first reply takes minutes | The CPU reads Claude Code's whole first prompt before it answers | Use --tools for read-only tasks, or a cloud model |
FAQ
Can Claude Code use local models from Ollama?
Yes. Ollama 0.14.0 and later serve Anthropic's Messages API, and ollama launch claude --model qwen3.5:9b starts Claude Code against it. Use a model that lists tools in ollama show, and set Ollama's context to at least 64,000 tokens.
Which Ollama model is best for Claude Code?
On a CPU server with 16 GB of RAM, use qwen3.5:9b at a 64K context. With 32 GB, qwen3.6:35b-a3b-q4_K_M generated 14.24 tokens/s on 16 vCPU, against 7.35 for the 9B model. Long tasks run faster on an Ollama cloud model.
Can I run Claude Code with Ollama without a GPU?
Yes. Ollama loads the model into system RAM, and ollama ps shows 100% CPU. On 16 vCPU, Qwen3.5 9B took 53.36 seconds to its first token on an 8K prompt, and Claude Code's first request carried 11,020 tokens in a -p test run.
Why does Claude Code show an API error with Ollama?
Usually the model lacks tool support, the model is not pulled, or Ollama is not running. API Error 400 with "does not support tools" means no tool calling. "There's an issue with the selected model" means the model is not pulled. ECONNREFUSED means nothing answers at ANTHROPIC_BASE_URL.
What context length does Claude Code need in Ollama?
At least 64,000 tokens, the figure in Ollama's docs; Ollama uses 4,096 on a server without a GPU. Raise the context in the Ollama service (Step 2) and pass the same limit to Claude Code when you start it (Step 5), so it compacts before Ollama cuts the prompt.
Do Ollama cloud models send my code off the server?
Yes. With a cloud model, the prompts, file contents and command output that Claude Code sends go to Ollama's servers. Ollama says it processes them to provide the service and does not store, log or train on them. OLLAMA_NO_CLOUD=1 keeps every model request on the server.
Do I need an Anthropic account to use Claude Code with Ollama?
No. With ANTHROPIC_AUTH_TOKEN set, Claude Code 2.1.319 showed no claude.ai login screen, and /status listed that token as the credential. Features that need a claude.ai login, such as Remote Control, stay unavailable.
Next steps
- How to Install Ollama on Ubuntu 24.04 with Open WebUI (CPU Only), to add a browser chat to the same Ollama service
- How to Install OpenCode on Ubuntu 24.04 with API or Ollama Models, another coding agent that works with the same Ollama setup
- Claude Code environment variables, for the Claude Code settings this tutorial uses
Arct Cloud High Performance plans run on AMD Ryzen 9 up to 5.7 GHz or AMD EPYC Genoa 9004 Series up to 4.3 GHz, with DDR5 RAM. Compare plans.
This work is licensed under CC BY-NC-SA 4.0.