Run Qwen Code with Ollama: Free Local AI Coding Agent

Why Run Qwen Code Locally

Qwen Code is Alibaba's open-source terminal coding agent: you describe the work, and it plans, reads code, edits files, runs commands, and checks the result. It started as a fork of Google's Gemini CLI and now ships subagents, agent teams, and MCP support. The project is Apache 2.0 licensed and has passed 30,000 stars on GitHub since its launch in June 2025.

For a long time, the easiest way to run it was Qwen's free OAuth tier. That door closed on April 15, 2026, when the free tier was discontinued. The paid routes left send your code to someone else's server. A local model fixes both problems. Ollama serves Qwen's own open-weight coder model on your machine, Qwen Code talks to it over a standard OpenAI-compatible endpoint, and the loop costs $0.



Qwen Code running in the terminal. Source: QwenLM/qwen-code on GitHub.

What You Need

Three things: Qwen Code, Ollama, and a tool-capable model. The model here is Qwen3-Coder-30B-A3B-Instruct, a mixture-of-experts model with 30.5 billion total parameters and only about 3.3 billion active per token, which is why it runs on a single consumer GPU. Its native context window is 256K tokens, and the download is about 19 GB. A 24 GB card such as an RTX 4090, or a Mac with 32 GB of unified memory, is the practical minimum. On a smaller machine, swap in a lighter tool-calling model such as qwen3:8b.

Step 1: Install Qwen Code

The npm route needs Node.js 22 or newer:

npm install -g @qwen-code/qwen-code@latest
qwen --version

On macOS or Linux, Homebrew works as well: brew install qwen-code. Any of these gives you the qwen command. Do not run it yet: it will ask you to sign in, and we are skipping sign-in entirely.

Step 2: Install Ollama and Pull the Model

Install Ollama from ollama.com, then pull the model:

ollama pull qwen3-coder:30b
ollama list


Ollama pulling and running a local model in the terminal. Source: A Dev Journey.

Check the tag reads exactly qwen3-coder:30b: a typo here is the most common cause of a "model not found" error later. Confirm the model answers before you wire up the agent:

ollama run qwen3-coder:30b "Write a Python function that reverses a string."

Step 3: Fix the Context Window First

Most guides skip this step, and it decides whether the agent works or flails. An agent juggling files, plans, and tool results needs a long context. Ollama's default context depends on your VRAM, and its documentation advises at least 64,000 tokens for agents and coding tools. Set it before you start the server:

OLLAMA_CONTEXT_LENGTH=65536 ollama serve

If Ollama is already running as a service, restart it with that variable set. The alternative is a Modelfile with PARAMETER num_ctx 65536, which takes precedence over the environment variable. Do this before your first session.

Step 4: Point Qwen Code at Ollama

Qwen Code reads providers from ~/.qwen/settings.json. The official docs give a local-model example for Ollama; this is that shape with Qwen3-Coder filled in:

{
  "env": {
    "OLLAMA_API_KEY": "ollama"
  },
  "modelProviders": {
    "openai": [
      {
        "id": "qwen3-coder:30b",
        "name": "Qwen3-Coder 30B (Ollama)",
        "envKey": "OLLAMA_API_KEY",
        "baseUrl": "http://localhost:11434/v1",
        "generationConfig": {
          "timeout": 300000,
          "streamIdleTimeoutMs": 600000,
          "maxRetries": 1,
          "contextWindowSize": 65536,
          "samplingParams": {
            "temperature": 0.2
          }
        }
      }
    ]
  }
}

Three details matter. First, the base URL must end in /v1: that is Ollama's OpenAI-compatible endpoint. Second, the API key is a placeholder Ollama never checks; Qwen Code just needs it non-empty. Third, the timeouts are generous on purpose: local inference on a consumer GPU is slower than a hosted API, and a short idle timeout will cut a long reply short. The docs example uses a 32,768-token context window; 65,536 here matches the value from step 3.

Edits to modelProviders apply without a restart. For a one-off session, CLI flags work instead:

qwen --auth-type openai --model qwen3-coder:30b \
  --openai-api-key ollama \
  --openai-base-url http://localhost:11434/v1

Step 5: Run Your First Coding Task

Move into a project directory and start the agent:

cd /path/to/your-project
qwen

Inside the session, type /model and pick "Qwen3-Coder 30B (Ollama)". Then give it real work, not a greeting:

Explain this repository and show me where to start.

Start small and verifiable: add a test, or fix a named bug and run the suite. It plans first, then edits, then runs commands to verify. Type /help for the built-in commands, including /resume and plan mode. For scripts and CI, the same agent runs headless: qwen -p "Summarize the repository layout."

Troubleshooting

  • "Model not found." The id in settings.json does not match a tag in ollama list. Copy the tag character for character, including :30b.
  • Connection refused. Ollama is not serving. Run ollama serve and confirm curl http://localhost:11434/v1/models answers.
  • Replies stop mid-sentence. The model is thinking longer than the idle timeout allows. Raise streamIdleTimeoutMs.
  • Tool calls fail. Tool calling needs a context window large enough for the tool definitions. Raise num_ctx to at least 65,536.
  • Everything is slow. That is the price of a 30B model on one GPU. If it is still too slow, drop to a smaller model rather than shrinking the context window.

What You Get

Qwen Code brings the agent harness: subagents, agent teams, auto-memory, skills, MCP servers, and a sandbox. Qwen3-Coder brings the coding ability, trained for multi-file edits and tool use in agent loops. The project's own SWE-bench Verified scores of 76.40% to 77.80% used its hosted flagship model, so treat the local 30B as a capable smaller sibling. You trade away raw speed and frontier quality, and gain an agent that runs offline, keeps code on your machine, and never sends a bill. If you already run OpenCode with Ollama or Aider with Ollama, Qwen Code is the third member of the same family. To tune the context window per model instead of globally, see the custom Ollama Modelfile guide.

Thirty minutes from now, you can have a terminal agent reading your repo, editing code, and running tests, with no account, no API key, and no per-token meter. That is the point of a local coding agent.

Why Run Qwen Code Locally

Qwen Code is Alibaba's open-source terminal coding agent: you describe the work, and it plans, reads code, edits files, runs commands, and checks the result. It started as a fork of Google's Gemini CLI and now ships subagents, agent teams, and MCP support. The project is Apache 2.0 licensed and has passed 30,000 stars on GitHub since its launch in June 2025.

For a long time, the easiest way to run it was Qwen's free OAuth tier. That door closed on April 15, 2026, when the free tier was discontinued. The paid routes left send your code to someone else's server. A local model fixes both problems. Ollama serves Qwen's own open-weight coder model on your machine, Qwen Code talks to it over a standard OpenAI-compatible endpoint, and the loop costs $0.

What You Need

Three things: Qwen Code, Ollama, and a tool-capable model. The model here is Qwen3-Coder-30B-A3B-Instruct, a mixture-of-experts model with 30.5 billion total parameters and only about 3.3 billion active per token, which is why it runs on a single consumer GPU. Its native context window is 256K tokens, and the download is about 19 GB. A 24 GB card such as an RTX 4090, or a Mac with 32 GB of unified memory, is the practical minimum. On a smaller machine, swap in a lighter tool-calling model such as qwen3:8b.

Step 1: Install Qwen Code

The npm route needs Node.js 22 or newer:

npm install -g @qwen-code/qwen-code@latest
qwen --version

On macOS or Linux, Homebrew works as well: brew install qwen-code. Any of these gives you the qwen command. Do not run it yet: it will ask you to sign in, and we are skipping sign-in entirely.

Step 2: Install Ollama and Pull the Model

Install Ollama from ollama.com, then pull the model:

ollama pull qwen3-coder:30b
ollama list
Ollama pulling and running a local model in the terminal. Source: A Dev Journey.

Check the tag reads exactly qwen3-coder:30b: a typo here is the most common cause of a "model not found" error later. Confirm the model answers before you wire up the agent:

ollama run qwen3-coder:30b "Write a Python function that reverses a string."

Step 3: Fix the Context Window First

Most guides skip this step, and it decides whether the agent works or flails. An agent juggling files, plans, and tool results needs a long context. Ollama's default context depends on your VRAM, and its documentation advises at least 64,000 tokens for agents and coding tools. Set it before you start the server:

OLLAMA_CONTEXT_LENGTH=65536 ollama serve

If Ollama is already running as a service, restart it with that variable set. The alternative is a Modelfile with PARAMETER num_ctx 65536, which takes precedence over the environment variable. Do this before your first session.

Step 4: Point Qwen Code at Ollama

Qwen Code reads providers from ~/.qwen/settings.json. The official docs give a local-model example for Ollama; this is that shape with Qwen3-Coder filled in:

{
  "env": {
    "OLLAMA_API_KEY": "ollama"
  },
  "modelProviders": {
    "openai": [
      {
        "id": "qwen3-coder:30b",
        "name": "Qwen3-Coder 30B (Ollama)",
        "envKey": "OLLAMA_API_KEY",
        "baseUrl": "http://localhost:11434/v1",
        "generationConfig": {
          "timeout": 300000,
          "streamIdleTimeoutMs": 600000,
          "maxRetries": 1,
          "contextWindowSize": 65536,
          "samplingParams": {
            "temperature": 0.2
          }
        }
      }
    ]
  }
}

Three details matter. First, the base URL must end in /v1: that is Ollama's OpenAI-compatible endpoint. Second, the API key is a placeholder Ollama never checks; Qwen Code just needs it non-empty. Third, the timeouts are generous on purpose: local inference on a consumer GPU is slower than a hosted API, and a short idle timeout will cut a long reply short. The docs example uses a 32,768-token context window; 65,536 here matches the value from step 3.

Edits to modelProviders apply without a restart. For a one-off session, CLI flags work instead:

qwen --auth-type openai --model qwen3-coder:30b \
  --openai-api-key ollama \
  --openai-base-url http://localhost:11434/v1

Step 5: Run Your First Coding Task

Move into a project directory and start the agent:

cd /path/to/your-project
qwen

Inside the session, type /model and pick "Qwen3-Coder 30B (Ollama)". Then give it real work, not a greeting:

Explain this repository and show me where to start.

Start small and verifiable: add a test, or fix a named bug and run the suite. It plans first, then edits, then runs commands to verify. Type /help for the built-in commands, including /resume and plan mode. For scripts and CI, the same agent runs headless: qwen -p "Summarize the repository layout."

Troubleshooting

  • "Model not found." The id in settings.json does not match a tag in ollama list. Copy the tag character for character, including :30b.
  • Connection refused. Ollama is not serving. Run ollama serve and confirm curl http://localhost:11434/v1/models answers.
  • Replies stop mid-sentence. The model is thinking longer than the idle timeout allows. Raise streamIdleTimeoutMs.
  • Tool calls fail. Tool calling needs a context window large enough for the tool definitions. Raise num_ctx to at least 65,536.
  • Everything is slow. That is the price of a 30B model on one GPU. If it is still too slow, drop to a smaller model rather than shrinking the context window.

What You Get

Qwen Code brings the agent harness: subagents, agent teams, auto-memory, skills, MCP servers, and a sandbox. Qwen3-Coder brings the coding ability, trained for multi-file edits and tool use in agent loops. The project's own SWE-bench Verified scores of 76.40% to 77.80% used its hosted flagship model, so treat the local 30B as a capable smaller sibling. You trade away raw speed and frontier quality, and gain an agent that runs offline, keeps code on your machine, and never sends a bill. If you already run OpenCode with Ollama or Aider with Ollama, Qwen Code is the third member of the same family. To tune the context window per model instead of globally, see the custom Ollama Modelf

Thirty minutes from now, you can have a terminal agent reading your repo, editing code, and running tests, with no account, no API key, and no per-token meter. That is the point of a local coding agent.

Comments

Popular posts from this blog

Open-Source AI Weekly: Bonsai, GigaChat 4 Lead

Claude Claims Proof of Riemann Hypothesis After 167 Years

How to Build an AI Agent with LangGraph in 30 Minutes