Skip to content

How-ToGPU

How to Use OpenCode with Ollama for a Free, Local AI Coding Agent

OpenCode is an open-source AI coding agent that can use local models through Ollama. The quickest start is ollama launch opencode in your project folder; alternatively add Ollama as a provider in opencode.json. Raise Ollama's context length to at least 64k tokens, as Ollama recommends for coding tools, and check ollama ps shows the model on your GPU.

By , Editorial team

4 min read

How to Use OpenCode with Ollama for a Free, Local AI Coding Agent

OpenCode is an open-source coding agent that can read your project, edit files and run commands, and it can use a model running on your own PC through Ollama. With a local model there is no per-token API bill, and prompts are processed by the model on your own PC. This guide follows Ollama's and OpenCode's official documentation. Forge PC hasn't benchmarked OpenCode with different models or graphics cards, so there are no speed figures here; how well it works depends on the model you choose and how much VRAM you have.

What you need

  • Ollama installed and running. If you haven't set it up yet, start with our guide to running AI models locally with Ollama and LM Studio.

  • A model with a large context window. Ollama's OpenCode page says OpenCode requires a context length of 64k tokens or more. A bigger context uses more memory, so this is the main hardware hurdle.

  • A graphics card with enough VRAM for your chosen model plus that context. Our guides to how much VRAM you need and the best GPU for local LLMs cover this.

Method 1: let Ollama launch OpenCode (easiest)

Ollama has a built-in launcher for OpenCode. According to Ollama's documentation:

  1. Open a terminal (PowerShell or Windows Terminal on Windows).

  2. Change to your project folder, for example cd C:\code\my-project.

  3. Run ollama launch opencode.

  4. Pick a model when asked. Ollama starts OpenCode with an inline configuration for that model.

Ollama says ollama launch opencode doesn't overwrite your OpenCode configuration file, so any existing OpenCode settings still apply. If you only want to set up the configuration without starting a session, Ollama lists ollama launch opencode --config.

One limitation: models defined only in OpenCode's own config file won't appear in the ollama launch model picker.

Method 2: install OpenCode and let it find Ollama

You can also install OpenCode yourself. OpenCode's current documentation offers several options:

  • Desktop app or standalone binary: OpenCode offers downloads for Windows x64 and ARM64, as well as macOS and Linux. OpenCode notes that Windows package managers aren't supported, so on Windows use the download or npm.

  • npm: npm install -g @opencode/cli (you'll need Node.js installed).

  • Linux and macOS install script: OpenCode's docs list a curl install script.

Then:

  1. Make sure Ollama is running and you've downloaded a model, for example with ollama pull followed by the model name.

  2. Start OpenCode in your project folder.

  3. Use /models to choose a model. OpenCode's providers documentation says it checks http://127.0.0.1:11434 for a running Ollama server and adds its models automatically, without an account. Embedding-only models don't appear.

If your Ollama server runs on another PC on your network, OpenCode's docs explain how to point it at that server with a baseURL setting that ends in /v1. Our guide to sharing Ollama on your local network explains the server side.

Note that Ollama's own OpenCode page still shows an older manual setup using an opencode.json provider block, while OpenCode's current docs describe automatic discovery. If one doesn't work, follow the instructions for the OpenCode version you installed.

Set the context length to at least 64k

This is the step most likely to trip you up. Ollama's documentation says its default context length depends on VRAM: 4k tokens on GPUs with less than 24GiB, 32k for 24 to 48GiB and 256k for 48GiB or more. Ollama recommends at least 64,000 tokens for agents and coding tools.

To raise it:

  • Ollama app: drag the context length slider in Settings.

  • Command line: start the server with OLLAMA_CONTEXT_LENGTH=64000 ollama serve (on Windows, set OLLAMA_CONTEXT_LENGTH as an environment variable and restart Ollama).

Then run ollama ps while OpenCode is working. Ollama says that for best performance the model should show 100% GPU under PROCESSOR. If part of it has spilled onto the CPU, responses will be much slower, so try a smaller model or a lower quantisation. Our full guide to setting the context length in Ollama goes into more detail, and you can watch memory use with our guide to checking VRAM usage in Windows.

Choosing a model

Pick a model built for code or agent work, and make sure it supports the context length you need. A longer context uses more memory, so a model that fits your VRAM at a short context may spill onto the CPU at 64k; check with ollama ps after changing it. Our best local LLM models round-up is a good starting point.

If you'd rather use a different tool, we also cover Claude Code with Ollama, Continue in VS Code and Aider.

Our verdict

The quickest way to try OpenCode locally is ollama launch opencode in your project folder. Whichever setup you use, raise Ollama's context length to at least 64k tokens and check with ollama ps that the model still runs fully on your GPU. Do that, pick a coding model that fits your VRAM, and you have a coding agent running on your own hardware with no per-token fees.

The bottom line

The quickest way to try OpenCode locally is `ollama launch opencode` in your project folder. Whichever setup you use, raise Ollama's context length to at least 64k tokens and check with `ollama ps` that the model still runs fully on your GPU. Do that, pick a coding model that fits your VRAM, and you have a coding agent running on your own hardware with no per-token fees.

Sources

  1. 01Ollama, OpenCode integration
  2. 02Ollama, Context length
  3. 03OpenCode, Intro (installation)
  4. 04OpenCode, Providers

About the author

Forge PC Staff

Editorial team

The shared byline for articles researched, written and edited by the Forge PC editorial team. How we source, check and correct our work is set out in our editorial policy.

More from Forge PC Staff

Read next

Lemonade vs Ollama: Which Local AI Server Should You Use on an AMD or NVIDIA PC?

Versus

Lemonade vs Ollama: Which Local AI Server Should You Use on an AMD or NVIDIA PC?

Ollama is the safest default for local LLMs, with a big model library and support for NVIDIA, AMD and Apple hardware. Lemonade, an open-source server with AMD-backed optimisations, is the better choice on Ryzen AI and Radeon PCs if you want NPU support or speech and image models alongside LLMs. It also speaks the Ollama and OpenAI APIs.

Forge PC Staff5 min

Sleep vs Hibernate vs Shut Down in Windows 11: Which Should You Use on a Gaming PC?

Versus

Sleep vs Hibernate vs Shut Down in Windows 11: Which Should You Use on a Gaming PC?

Sleep keeps your session in RAM for the fastest resume but loses unsaved work in a power cut. Hibernate writes it to hiberfil.sys on your SSD and uses no power, but resumes more slowly. Shut down or restart for a clean start. On a gaming desktop, sleep for short breaks and hibernate for long ones; on a laptop, hibernate or shut down before travelling.

Forge PC Staff5 min

RTX 5090 vs RTX 4090 for Local AI: Is 32GB and 1.8TB/s Worth the Upgrade?

Versus

RTX 5090 vs RTX 4090 for Local AI: Is 32GB and 1.8TB/s Worth the Upgrade?

For local AI, the RTX 5090's 32GB of GDDR7 and 1,792GB/s of bandwidth beat the RTX 4090's 24GB and 1,008GB/s, so bigger models and longer contexts fit and token generation should be faster, with FP4 support too. It needs more power and NVIDIA recommends a 1,000W PSU. If your models fit in 24GB, a good-value RTX 4090 is still very capable.

Forge PC Staff5 min

How to Add Web Search to Your Local AI Chatbot in Open WebUI

How-To

How to Add Web Search to Your Local AI Chatbot in Open WebUI

Open WebUI can give a local model live web search. Start with the built-in DDGS engine, which needs no API key, move to a self-hosted SearXNG instance (with JSON output enabled) for more control, or use a hosted search API. Raise the model's context length to at least 16K tokens and use a model with good tool calling for useful answers.

Forge PC Staff5 min