跳到正文
DEV Community· SEBAA Mohammed·· 4 小時前AI 評分61

如何透過 Ollama 和本地模型執行 Claude Code

Running Claude Code with Ollama and Local Models

AI 導讀

這篇教學示範如何透過 Ollama 將 Claude Code 連接至本地模型,並在 IntelliJ IDEA 的 Java 專案中測試編碼流程。示範模型為 qwen3.5:9b,並列出安裝、設定 API 及建立 Java 任務管理程式的步驟。文中比較本地與雲端方案,指出本地執行可讓程式碼留在電腦上且沒有按次費用,但速度取決於硬件,品質適合小型任務。

正文

Claude Code + Ollama: Coding with a Local Model

Claude Code is Anthropic's agentic coding tool. It reads your project, edits files, and runs commands from the terminal. With Ollama, you can run it on a model hosted on your own machine instead of in the cloud.

Why local models?

  • Your code never leaves your machine
  • No API costs
  • Works offline

In this guide, you'll install Claude Code, run it inside IntelliJ IDEA, connect it to Ollama, and test it on a small Java project.

Step 1: Install Claude Code

Install it globally with npm:

npm install -g @anthropic-ai/claude-code

install claude code via npm

Then open a terminal and run:

claude

run claude code in terminal

If the Claude Code prompt appears, the installation worked.

Step 2: Run It Inside IntelliJ IDEA

You don't need to leave your IDE. Open your project in IntelliJ, open the built-in terminal (View → Tool Windows → Terminal), and run:

claude

Running claude code in a terminal in intelij

Claude Code now has access to your project files and works right next to your code.

Step 3: Connect It to Ollama

Ollama exposes an Anthropic-compatible API, so Claude Code can talk to a local model.

1. Install Ollama
Ollama lets you download and run language models on your own machine. Your code never leaves your computer and there is no per-request cost.

On https://ollama.com you can create a free account. The dashboard shows your free quota for cloud models. For the rest of this tutorial (100% local), it isn't required.

2. Pull a coding model:

ollama pull qwen3.5:9b

3. Point Claude Code at Ollama:

export ANTHROPIC_AUTH_TOKEN=ollama
export ANTHROPIC_BASE_URL=http://localhost:11434
claude --model qwen3.5:9b

On Windows PowerShell, use $env:ANTHROPIC_AUTH_TOKEN="ollama" and $env:ANTHROPIC_BASE_URL="http://localhost:11434" instead of export.

Alternatively, let Ollama handle the setup with one command, using --model

ollama launch claude --model qwen3.5:9b

run claude code with specific model

Step 4: Build a Demo Project

Let's test the setup with a small Java project: a console task manager.

Create an empty Java project in IntelliJ, open the terminal, start Claude Code, and give it this prompt:

Create a simple Java console app: a task manager. Users can add,
list, complete and delete tasks. Save tasks to a text file so they
persist. Keep it to 2-3 classes.

Claude Code will plan the work, create the files, fix the errors, and ask for your permission before making changes. Approve the actions and watch it work.

claude code running

claude code running

Then run the app and try it out:

testing program

testing program

Notice that it doesn't only generate code. It also creates the files, runs the program, and fixes its own errors.

Cloud vs Local: Quick Comparison

Cloud Claude Local model (Ollama)
Privacy Code is sent to the API Stays on your machine
Cost Subscription / API Free
Speed Fast Depends on your hardware
Quality Highest Good for small tasks

Conclusion

Claude Code with Ollama is a practical way to try agentic coding without sending your code anywhere, as long as your machine can handle the model. Start with a small project like this one, then try it on your own code.

Thanks for reading!

來源:DEV Community · dev.to