|
1 | 1 | --- |
2 | 2 | layout: post |
3 | | -title: How to set up OpenCode with Ollama |
| 3 | +title: How to use OpenCode with Ollama |
4 | 4 | date: 2026-02-04 14:22:24 |
| 5 | +updated: 2026-05-02 19:41:41 |
5 | 6 | excerpt: How to set up OpenCode IDE with an Ollama coding model. |
6 | 7 | categories: opencode ollama ai |
7 | 8 | --- |
8 | 9 |
|
9 | 10 | This post goes over how to set up [OpenCode](https://opencode.ai/) with [Ollama](https://ollama.com/). |
10 | 11 |
|
11 | | -## Prerequisites |
| 12 | +## Install |
12 | 13 |
|
13 | | -[OpenCode](https://opencode.ai/): |
| 14 | +[Ollama](https://ollama.com/): |
14 | 15 |
|
15 | 16 | ```sh |
16 | | -brew install opencode |
| 17 | +brew install ollama |
17 | 18 | ``` |
18 | 19 |
|
19 | | -[Ollama](https://ollama.com/): |
| 20 | +[OpenCode](https://opencode.ai/): |
20 | 21 |
|
21 | 22 | ```sh |
22 | | -brew install ollama |
| 23 | +brew install opencode |
23 | 24 | ``` |
24 | 25 |
|
25 | 26 | ## Ollama |
26 | 27 |
|
27 | | -Start Ollama: |
| 28 | +Start the server in a separate terminal: |
28 | 29 |
|
29 | 30 | ```sh |
30 | 31 | ollama serve |
31 | 32 | ``` |
32 | 33 |
|
33 | | -Ollama by default uses a [context window size](https://docs.ollama.com/context-length) of 4096 tokens. For tasks that require large context like web search, agents, and coding tools, set the [context window](https://docs.ollama.com/faq#how-can-i-specify-the-context-window-size) to at least 64000 tokens: |
| 34 | +Install [gemma4](https://ollama.com/library/gemma4): |
| 35 | + |
| 36 | +```sh |
| 37 | +ollama pull gemma4 |
| 38 | +``` |
| 39 | + |
| 40 | +Confirm it's downloaded: |
| 41 | + |
| 42 | +```sh |
| 43 | +ollama ls gemma4 |
| 44 | +``` |
| 45 | + |
| 46 | +You should see: |
34 | 47 |
|
35 | 48 | ```sh |
36 | | -OLLAMA_CONTEXT_LENGTH=64000 ollama serve |
| 49 | +NAME ID SIZE MODIFIED |
| 50 | +gemma4:latest c6eb396dbd59 9.6 GB 5 seconds ago |
37 | 51 | ``` |
38 | 52 |
|
39 | | -Install [qwen2.5-coder](https://ollama.com/library/qwen2.5-coder): |
| 53 | +Check the model's context length: |
40 | 54 |
|
41 | 55 | ```sh |
42 | | -ollama pull qwen2.5-coder:latest |
| 56 | +ollama show gemma4 |
| 57 | +``` |
| 58 | + |
| 59 | +You should see: |
| 60 | + |
| 61 | +``` |
| 62 | + Model |
| 63 | + architecture gemma4 |
| 64 | + parameters 8.0B |
| 65 | + context length 131072 |
| 66 | + embedding length 2560 |
| 67 | + quantization Q4_K_M |
| 68 | + requires 0.20.0 |
| 69 | +``` |
| 70 | + |
| 71 | +Restart the server with the context length (to maximize memory): |
| 72 | + |
| 73 | +```sh |
| 74 | +OLLAMA_CONTEXT_LENGTH=131072 ollama serve |
| 75 | +``` |
| 76 | + |
| 77 | +Check the performance of the model: |
| 78 | + |
| 79 | +```sh |
| 80 | +ollama run gemma4 "ping" && ollama ps |
43 | 81 | ``` |
44 | 82 |
|
45 | 83 | ## OpenCode |
46 | 84 |
|
47 | | -Open **OpenCode** > ⌘P > **Settings** (⌘,) > **Providers** > **Custom provider** > **+ Connect**. |
48 | | - |
49 | | -Fill out the custom provider fields: |
50 | | - |
51 | | -- Provider ID: |
52 | | - ``` |
53 | | - ollama |
54 | | - ``` |
55 | | -- Display name: |
56 | | - ``` |
57 | | - Ollama |
58 | | - ``` |
59 | | -- Base URL: |
60 | | - ``` |
61 | | - http://localhost:11434/v1 |
62 | | - ``` |
63 | | -- Model ID: |
64 | | - ``` |
65 | | - qwen2.5-coder:latest |
66 | | - ``` |
67 | | -- Model Display Name: |
68 | | - ``` |
69 | | - Qwen2.5 Coder |
70 | | - ``` |
71 | | - |
72 | | -Click **Submit**. |
73 | | - |
74 | | -> To delete a manually configured model, edit the global config `~/.config/opencode/opencode.jsonc`. |
75 | | -
|
76 | | -Ollama is connected and the model is ready for use! |
| 85 | +Launch opencode with [gemma4](https://ollama.com/library/gemma4): |
| 86 | + |
| 87 | +```sh |
| 88 | +ollama launch opencode --model gemma4 |
| 89 | +``` |
0 commit comments