在 Mac 上配置 OpenCode、Ollama 和 sbx
Setting up OpenCode with Ollama and sbx on Mac

原始链接: https://tensorsandtokens.com/posts/opencode-ollama/

本指南概述了如何利用 **Ollama**、**Opencode** 和 **Docker 沙盒 (sbx)** 构建安全且本地化的 LLM 应用。此方案专为 Apple Silicon(建议 48GB+ 内存)设计,让您能在隔离且稳定的环境中运行高性能模型,如 Qwen 3.8 和 Gemma 4。 **核心组件:** * **Ollama:** 提供支持 MLX 的高速、稳定本地模型托管服务。 * **Opencode:** 作为主要的开发环境框架。 * **Docker 沙盒 (sbx):** 通过容器化开发环境确保安全性,防止模型异常行为影响宿主机。 **实施步骤:** 1. **安装:** 通过 Homebrew 及官网安装 `sbx`、`opencode` 和 `ollama`。 2. **模型设置:** 使用 Ollama CLI 拉取所需模型(例如 `qwen3.8` 或 `gemma4`)。 3. **配置:** 创建一个 `sbx-kit` 文件夹,其中包含 `spec.yaml` 和 `opencode-local.json` 文件。这些配置用于定义沙盒环境、映射宿主机的 Ollama 端口 (11434),并设置内存上限以防止系统崩溃。 4. **执行:** 使用命令 `sbx run opencode --kit ./sbx-kit/` 启动环境。 该架构在获取高性能模型能力与确保容器化开发安全性之间取得了平衡。

近期关于在 Mac 上部署 OpenCode 的 Hacker News 讨论,凸显了本地大模型社区内部的分歧。尽管原贴重点介绍了易于使用的 **Ollama**,但许多评论者批评该工具过于“臃肿”,并建议使用 **llama.cpp** 等替代方案。 该讨论帖中包含了几项社区推荐的本地模型管理工具: * **Kultivait**:一款旨在代理 Ollama 并作为前沿模型成本优化预处理器的工具(尽管部分功能仍在开发中)。 * **OMLX**:因其流畅的 UI、模型大小调整功能以及对 Hermes 和 Claude 等模型的开箱即用配置而受到用户推荐,但也有用户反映存在稳定性问题。 归根结底,这场争论反映了更广泛的矛盾:一边是看重 Ollama 简洁性的用户,另一边则是倡导更“合乎道德”、性能更佳且轻量化替代方案的支持者。Ollama 的批评者常引用外部资源(如 *Sleeping Robots*)来主张转向 llama.cpp,而另一些人则为 Ollama 的稳定性和低入门门槛进行了辩护。
相关文章

原文
Software development

Adam Lusted3 min read

How to get started with running Ollama local models with Opencode and Docker Sandboxes.

Viable local LLM development is here. With powerful models like Qwen 3.8 and Gemma4 we can now finally use these models to build out web applications. I'm using a Apple Macbook pro m5 48GB model. I think this is the sweet spot for local development as it allows you to run 30B models with a decent sized context.

Why I use Ollama. To be frank it's just easy. It has mlx now, so it's fast on Apple silicon. It has a pretty good model directory. It is also very stable and won't crash.

Why Opencode. Well, we need to start somewhere with this blog and opencode is a great harness.

There is one other tool that I use, docker sandboxes aka sbx. I use frontier models at work which require us to sandbox our harnesses. I also, like to run my local models in containers as they can just as easily mess up your computer with a unwanted hallucination.

Installing the tools

Install Docker Sandbox:

brew trust docker/tap && brew install docker/tap/sbx

Install Opencode:

brew install anomalyco/tap/opencode

Install Ollama:

To install ollama, goto https://ollama.com/ and download and install the app.

Installing the models

For the models, we'll pull 2 models. First ensure ollama is running.

Qwen 3.8 27B mxfp8 (32GB): This is a great workhorse model that will do most of your long running work and can run undisturbed for multiple hours within opencode.

ollama pull qwen3.8:27b-mxfp8

Gemma 4 31b mxfp8 (34GB): This is a great big dense model when you need something bigger.

ollama pull gemma4:31b-mxfp8

Note: If you don't have the 48GB Apple, you can pull the standard models: qwen3.8:27b-mlx and gemma4:31b-mlx .

Configuring your project

For this setup, it requires you to setup a sbx kit for every project. A kit is a way to customise the sandbox. Create the following folders and files:

./sbx-kit/files/home/.config/opencode-local.json
./sbx-kit/spec.yaml

spec.yaml

schemaVersion: "2"
kind: mixin
name: local-ollama-opencode
version: "0.1.0"
displayName: Local Ollama for OpenCode
description: Configure OpenCode in Docker Sandboxes to use Ollama running on the Mac host.

requires:
  agent: opencode

environment:
  variables:
    OPENCODE_CONFIG: /home/agent/.config/opencode-local.json

permissions:
  network:
    allow:
      - localhost:11434
      - localhost:5173
      - localhost:4000

agentInstructions:
  content: |
    Local Ollama runs on the host machine.

    Default model:
      qwen3.8:27b-mxfp8

    Deep file analysis / reasoning:
      gemma4:31b-mxfp8

opencode-local.yaml

{
  "$schema": "https://opencode.ai/config.json",

  "model": "ollama/gemma4:31b-mxfp8",
  "small_model": "ollama/gemma4:31b-mxfp8",
  "lsp": false,

  "provider": {
    "ollama": {
      "npm": "@ai-sdk/openai-compatible",
      "name": "Mac Ollama",

      "options": {
        "baseURL": "http://host.docker.internal:11434/v1"
      },

      "models": {


        "qwen3.8:27b-mxfp8-64K": {
          "id": "qwen3.8:27b-mxfp8",
          "name": "Qwen 3.8 27B MXFP8 [31 GB] [64K ctx]",
          "limit": {
            "context": 65536,
            "output": 8192
          },
          "variants": {
            "low": {
              "reasoningEffort": "low"
            },
            "medium": {
              "reasoningEffort": "medium"
            },
            "high": {
              "reasoningEffort": "high"
            },
            "xhigh": {
              "reasoningEffort": "xhigh"
            }
          }
        },

        "gemma4:31b-mxfp8": {
          "name": "Gemma 4 31B MXFP8 [33 → ~50 GB] [256K ctx]",
          "limit": {
            "context": 262144,
            "output": 8192
          }
        }

      }
    }
  }
}

For the qwen model, we've limited the context to 64k, this will ensure your system dose not lock up when it runs out of memory. We also need 3GB for the sandbox.

Run Opencode

To run opencode, run the following:

sbx run opencode --kit ./sbx-kit/

This will start Opencode with Qwen selected. Ensure you change down to "low" effort via /models command.

You should be good to go.

Note: You will need to login into Docker to run sbx. This feature is not really liked by the development community but there is no way around it.

opencode

 

联系我们 contact @ memedata.com