Run the most popular coding agent locally !
This tutorial documents a working setup for running Claude Code against a local llama.cpp llama-server instance using Qwen3.5 GGUF models.
The recommended default is Qwen3.5-9B for weaker machines. A Qwen3.5-35B-A3B command is included as an optional larger-model setup.
The final working architecture is:
Claude Code CLI
-> Anthropic-compatible HTTP request
-> local llama.cpp llama-server
-> Qwen3.5 GGUF model
- Tested outcome
- Important notes
- 1. Install system dependencies
- 2. Add CUDA to your shell environment
- 3. Clone llama.cpp and pin a release tag
- 4. Build llama.cpp with CUDA
- 5. Start llama-server with Qwen3.5-9B
- 6. Optional: Qwen3.5-35B-A3B command
- 7. Test the local Anthropic-compatible endpoint
- 8. Install Claude Code
- 9. Configure Claude Code for the local model
- 10. Configure Claude Code settings
- 11. Start Claude Code
- 12. Optional launcher script
- 13. Troubleshooting
- 14. Minimal working recipe
- 15. Notes on Homebrew
- References
A working validation inside Claude Code should look like this:
/model
Set model to unsloth/Qwen3.5-9B and saved as your default for new sessions
Say one sentence confirming which model name you are using.
I am using the unsloth/Qwen3.5-9B model.
nvidia-smiworking only proves that the NVIDIA driver is visible. Building llama.cpp with CUDA also requires the CUDA compilernvcc.- Claude Code must use only one local auth variable. Do not set both
ANTHROPIC_API_KEYandANTHROPIC_AUTH_TOKEN. - Qwen3.5 with Claude Code may need llama.cpp's chat parser workaround:
--skip-chat-parsing. - For weaker machines, start with Qwen3.5-9B and
--ctx-size 8192or16384. - For CUDA builds, prefer building llama.cpp from source. Homebrew is not recommended for NVIDIA CUDA builds.
sudo apt update
sudo apt install -y git cmake build-essential curl libcurl4-openssl-dev ccacheFor CUDA acceleration, install the NVIDIA CUDA Toolkit appropriate for your distro.
Check whether nvcc is available:
which nvcc
nvcc --versionIf nvidia-smi works but nvcc is missing, the NVIDIA driver is installed but the CUDA Toolkit is not correctly installed or is not in your shell PATH.
If nvcc exists under /usr/local/cuda/bin/nvcc but is not found by your shell, add CUDA to ~/.bashrc:
echo 'export CUDA_HOME=/usr/local/cuda' >> ~/.bashrc
echo 'export PATH=/usr/local/cuda/bin:$PATH' >> ~/.bashrc
echo 'export LD_LIBRARY_PATH=/usr/local/cuda/lib64:$LD_LIBRARY_PATH' >> ~/.bashrc
source ~/.bashrcVerify again:
which nvcc
nvcc --versionIf you installed a versioned CUDA path such as /usr/local/cuda-13.3, either make sure /usr/local/cuda points to it or replace /usr/local/cuda in the commands above with the versioned path.
On WSL, install the NVIDIA driver on Windows. Inside WSL, install the CUDA Toolkit, not the Linux NVIDIA display driver. If nvidia-smi works in WSL but nvcc does not, you still need the toolkit inside WSL.
llama.cpp moves quickly. For reproducibility, use a release tag instead of master.
At the time this tutorial was finalized, b9478 was the latest checked tag used for this setup.
cd ~
git clone https://github.com/ggml-org/llama.cpp
cd llama.cpp
git fetch --tags
git checkout b9478
git describe --tags --alwaysExpected output:
b9478
If this tag no longer exists or a newer tag is preferred, check the llama.cpp releases page and use a recent release tag.
Use a clean build directory:
cd ~/llama.cpp
rm -rf buildConfigure with CUDA:
cmake -B build \
-DBUILD_SHARED_LIBS=OFF \
-DGGML_CUDA=ON \
-DCMAKE_CUDA_COMPILER=/usr/local/cuda/bin/nvccBuild with low parallelism to avoid RAM exhaustion:
cmake --build build --config Release --parallel 2 --target llama-cli llama-serverIf the build is killed with an error like this:
c++: fatal error: Killed signal terminated program cc1plus
nvcc: Terminated
then the OS likely killed the compiler due to insufficient RAM or swap. Retry with one build job:
cmake --build build --config Release --parallel 1 --target llama-cli llama-serverConfirm an out-of-memory kill with:
sudo dmesg -T | grep -Ei 'killed process|out of memory|oom|cc1plus|nvcc' | tail -n 50Optional temporary swap:
sudo fallocate -l 16G /swapfile
sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile
free -hMake swap persistent only if you want it permanently:
echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstabIf you do not want CUDA:
cd ~/llama.cpp
rm -rf build
cmake -B build \
-DBUILD_SHARED_LIBS=OFF \
-DGGML_CUDA=OFF
cmake --build build --config Release --parallel 2 --target llama-cli llama-serverCPU-only will work but will be much slower for coding-agent use.
This is the recommended command for weaker machines.
cd ~/llama.cpp
./build/bin/llama-server \
-hf unsloth/Qwen3.5-9B-GGUF:UD-Q4_K_XL \
--alias unsloth/Qwen3.5-9B \
--host 127.0.0.1 \
--port 8001 \
--ctx-size 16384 \
--parallel 1 \
--n-gpu-layers 99 \
--flash-attn on \
--jinja \
--skip-chat-parsing \
--chat-template-kwargs '{"enable_thinking":false}' \
--temp 0.6 \
--top-p 0.95 \
--top-k 20 \
--min-p 0.00For lower VRAM, reduce context:
--ctx-size 8192If the model does not fit fully on GPU, reduce GPU offload:
--n-gpu-layers 20or let llama.cpp decide:
--n-gpu-layers autoIf VRAM is tight, add:
--cache-type-k q8_0 \
--cache-type-v q8_0This mainly reduces KV cache memory. It may or may not improve speed; benchmark both default KV cache and q8_0 KV cache on your machine.
Use this only if you have enough RAM/VRAM.
cd ~/llama.cpp
./build/bin/llama-server \
--model unsloth/Qwen3.5-35B-A3B-GGUF/Qwen3.5-35B-A3B-UD-Q4_K_XL.gguf \
--alias unsloth/Qwen3.5-35B-A3B \
--host 127.0.0.1 \
--port 8001 \
--ctx-size 16384 \
--parallel 1 \
--n-gpu-layers all \
--flash-attn on \
--jinja \
--skip-chat-parsing \
--chat-template-kwargs '{"enable_thinking":false}' \
--temp 0.6 \
--top-p 0.95 \
--top-k 20 \
--min-p 0.00cd ~/llama.cpp
./build/bin/llama-server \
--model unsloth/Qwen3.5-35B-A3B-GGUF/Qwen3.5-35B-A3B-UD-Q4_K_XL.gguf \
--alias unsloth/Qwen3.5-35B-A3B \
--host 127.0.0.1 \
--port 8001 \
--ctx-size 8192 \
--parallel 1 \
--n-gpu-layers all \
--flash-attn on \
--jinja \
--skip-chat-parsing \
--chat-template-kwargs '{"enable_thinking":false}' \
--temp 0.6 \
--top-p 0.95 \
--top-k 20 \
--min-p 0.00 \
--kv-unified \
--cache-type-k q8_0 \
--cache-type-v q8_0Notes:
--n-gpu-layers allis usually the biggest speed win if the model fits in VRAM.--flash-attn onis generally worth trying for long context.--parallel 1is appropriate for a single Claude Code session.q8_0KV cache is mostly for memory reduction; test whether it helps speed on your GPU.- If you used
hf download, make sure the--modelpath matches your actual file path.
In a second terminal, test llama-server directly:
curl http://127.0.0.1:8001/v1/messages \
-H "Content-Type: application/json" \
-H "Authorization: Bearer sk-local" \
-d '{
"model": "unsloth/Qwen3.5-9B",
"max_tokens": 64,
"messages": [
{
"role": "user",
"content": "Say one sentence confirming the local model works."
}
]
}'If this fails, fix llama-server before starting Claude Code.
curl -fsSL https://claude.ai/install.sh | bash
claude --versionRestart your shell if claude is not found.
Use only ANTHROPIC_AUTH_TOKEN. Do not also set ANTHROPIC_API_KEY.
unset ANTHROPIC_API_KEY
export ANTHROPIC_BASE_URL="http://127.0.0.1:8001"
export ANTHROPIC_AUTH_TOKEN="sk-local"
export ANTHROPIC_CUSTOM_MODEL_OPTION="unsloth/Qwen3.5-9B"
export ANTHROPIC_CUSTOM_MODEL_OPTION_NAME="Qwen3.5 9B Local"
export ANTHROPIC_CUSTOM_MODEL_OPTION_DESCRIPTION="Qwen3.5-9B served locally by llama.cpp"Remove conflicting API key exports from ~/.bashrc:
sed -i '/ANTHROPIC_API_KEY/d' ~/.bashrcPersist the local configuration:
cat >> ~/.bashrc <<'EOF'
# Claude Code local llama.cpp / Qwen3.5-9B
export ANTHROPIC_BASE_URL="http://127.0.0.1:8001"
export ANTHROPIC_AUTH_TOKEN="sk-local"
export ANTHROPIC_CUSTOM_MODEL_OPTION="unsloth/Qwen3.5-9B"
export ANTHROPIC_CUSTOM_MODEL_OPTION_NAME="Qwen3.5 9B Local"
export ANTHROPIC_CUSTOM_MODEL_OPTION_DESCRIPTION="Qwen3.5-9B served locally by llama.cpp"
EOF
source ~/.bashrcIf switching to Qwen3.5-35B-A3B, change the model variables:
export ANTHROPIC_CUSTOM_MODEL_OPTION="unsloth/Qwen3.5-35B-A3B"
export ANTHROPIC_CUSTOM_MODEL_OPTION_NAME="Qwen3.5 35B A3B Local"
export ANTHROPIC_CUSTOM_MODEL_OPTION_DESCRIPTION="Qwen3.5-35B-A3B served locally by llama.cpp"Create or update ~/.claude/settings.json:
mkdir -p ~/.claude
cat > ~/.claude/settings.json <<'JSON'
{
"env": {
"CLAUDE_CODE_ATTRIBUTION_HEADER": "0",
"CLAUDE_CODE_ENABLE_TELEMETRY": "0",
"CLAUDE_CODE_DISABLE_NONESSENTIAL_TRAFFIC": "1"
}
}
JSONThis reduces nonessential traffic and avoids extra attribution content that can interfere with local gateway prompt handling.
Make sure llama-server is already running in another terminal.
cd ~/Teachess
claude --model unsloth/Qwen3.5-9BInside Claude Code, run:
/model
Select or confirm:
unsloth/Qwen3.5-9B
Then test:
Say one sentence confirming which model name you are using.
Expected answer:
I am using the unsloth/Qwen3.5-9B model.
If the first answer still reports Claude Opus, use /model again and explicitly select the Qwen model. After selection, repeat the test.
Create a helper script:
mkdir -p ~/bin
cat > ~/bin/claude-qwen35-local <<'EOF'
#!/usr/bin/env bash
set -euo pipefail
unset ANTHROPIC_API_KEY
export ANTHROPIC_BASE_URL="http://127.0.0.1:8001"
export ANTHROPIC_AUTH_TOKEN="sk-local"
export ANTHROPIC_CUSTOM_MODEL_OPTION="unsloth/Qwen3.5-9B"
export ANTHROPIC_CUSTOM_MODEL_OPTION_NAME="Qwen3.5 9B Local"
export ANTHROPIC_CUSTOM_MODEL_OPTION_DESCRIPTION="Qwen3.5-9B served locally by llama.cpp"
exec claude --model unsloth/Qwen3.5-9B "$@"
EOF
chmod +x ~/bin/claude-qwen35-localRun:
cd ~/Teachess
claude-qwen35-localExample:
Auth conflict: Both a token (ANTHROPIC_AUTH_TOKEN) and an API key (ANTHROPIC_API_KEY) are set.
Fix:
unset ANTHROPIC_API_KEY
sed -i '/ANTHROPIC_API_KEY/d' ~/.bashrc
source ~/.bashrcKeep only:
export ANTHROPIC_AUTH_TOKEN="sk-local"Example:
API Error: 400 Unable to generate parser for this template.
Error: Jinja Exception: System message must be at the beginning.
Fix by restarting llama-server with:
--jinja \
--skip-chat-parsing \
--chat-template-kwargs '{"enable_thinking":false}'If it still fails, try the ChatML fallback:
--jinja \
--chat-template chatml \
--skip-chat-parsing \
--chat-template-kwargs '{"enable_thinking":false}'Example:
No CMAKE_CUDA_COMPILER could be found.
Check:
which nvcc
nvcc --version
ls -l /usr/local/cuda/bin/nvccIf nvcc exists, export CUDA paths:
echo 'export CUDA_HOME=/usr/local/cuda' >> ~/.bashrc
echo 'export PATH=/usr/local/cuda/bin:$PATH' >> ~/.bashrc
echo 'export LD_LIBRARY_PATH=/usr/local/cuda/lib64:$LD_LIBRARY_PATH' >> ~/.bashrc
source ~/.bashrcThen rebuild from a clean directory:
cd ~/llama.cpp
rm -rf build
cmake -B build \
-DBUILD_SHARED_LIBS=OFF \
-DGGML_CUDA=ON \
-DCMAKE_CUDA_COMPILER=/usr/local/cuda/bin/nvccExample:
c++: fatal error: Killed signal terminated program cc1plus
nvcc: Terminated
Use fewer build jobs:
cmake --build build --config Release --parallel 1 --target llama-cli llama-serverOptionally add swap:
sudo fallocate -l 16G /swapfile
sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile
free -hCheck that llama-server is running and listening on the expected port:
curl http://127.0.0.1:8001/v1/modelsIf you used another port, update:
export ANTHROPIC_BASE_URL="http://127.0.0.1:<port>"Inside Claude Code, run:
/model
Then explicitly select:
unsloth/Qwen3.5-9B
Repeat:
Say one sentence confirming which model name you are using.
Also watch the terminal running llama-server; a local request should appear there.
Try these changes in order:
--ctx-size 8192--cache-type-k q8_0 \
--cache-type-v q8_0--n-gpu-layers 20For very limited machines, use Qwen3.5-9B rather than Qwen3.5-35B-A3B.
Unset the local gateway variables:
unset ANTHROPIC_BASE_URL
unset ANTHROPIC_AUTH_TOKEN
unset ANTHROPIC_API_KEY
unset ANTHROPIC_CUSTOM_MODEL_OPTION
unset ANTHROPIC_CUSTOM_MODEL_OPTION_NAME
unset ANTHROPIC_CUSTOM_MODEL_OPTION_DESCRIPTIONThen run:
claudeTerminal 1:
cd ~/llama.cpp
./build/bin/llama-server \
-hf unsloth/Qwen3.5-9B-GGUF:UD-Q4_K_XL \
--alias unsloth/Qwen3.5-9B \
--host 127.0.0.1 \
--port 8001 \
--ctx-size 16384 \
--parallel 1 \
--n-gpu-layers 99 \
--flash-attn on \
--jinja \
--skip-chat-parsing \
--chat-template-kwargs '{"enable_thinking":false}'Terminal 2:
unset ANTHROPIC_API_KEY
export ANTHROPIC_BASE_URL="http://127.0.0.1:8001"
export ANTHROPIC_AUTH_TOKEN="sk-local"
export ANTHROPIC_CUSTOM_MODEL_OPTION="unsloth/Qwen3.5-9B"
cd ~/Teachess
claude --model unsloth/Qwen3.5-9BInside Claude Code:
/model
Say one sentence confirming which model name you are using.
Expected:
I am using the unsloth/Qwen3.5-9B model.
brew install llama.cpp can be useful for a quick CPU or macOS install, but it is not the recommended path for an NVIDIA CUDA build. For CUDA, build from source with:
-DGGML_CUDA=ONand point CMake at nvcc if needed:
-DCMAKE_CUDA_COMPILER=/usr/local/cuda/bin/nvcc- Claude Code model configuration: https://code.claude.com/docs/en/model-config
- Claude Code LLM gateway configuration: https://code.claude.com/docs/en/llm-gateway
- Claude Code environment variables: https://code.claude.com/docs/en/env-vars
- llama.cpp build documentation: https://github.com/ggml-org/llama.cpp/blob/master/docs/build.md
- llama.cpp server documentation: https://github.com/ggml-org/llama.cpp/blob/master/tools/server/README.md
- llama.cpp releases: https://github.com/ggml-org/llama.cpp/releases
- Unsloth Qwen3.5 documentation: https://unsloth.ai/docs/models/qwen3.5
- Unsloth Claude Code local LLM guide: https://unsloth.ai/docs/basics/claude-code
- NVIDIA CUDA Linux installation guide: https://docs.nvidia.com/cuda/cuda-installation-guide-linux/
- NVIDIA CUDA on WSL guide: https://docs.nvidia.com/cuda/wsl-user-guide/index.html
- CMake build parallelism: https://cmake.org/cmake/help/latest/envvar/CMAKE_BUILD_PARALLEL_LEVEL.html