Skip to main content

Ollama 安装配置, 轻松把大模型跑在本地

· 4 min read

Ollama 是一个"让开源大模型可以像本地软件一样被安装、运行和调用"的工具。Ollama 可以理解为一个本地大模型运行平台 + 模型管理器 + API 服务层, 把本地跑 AI 这件事做得足够简单。

Ollama 的安装非常简单快捷, 以 WSL2/Debian 系统为例.

  1. 首先安装 zstd, 因为 Ollama 依赖它
sudo apt-get install zstd

没有安装 zstd 在安装 Ollama 时会将看到如下错误:

ERROR: This version requires zstd for extraction. Please install zstd  > and try again:
- Debian/Ubuntu: sudo apt-get install zstd
- RHEL/CentOS/Fedora: sudo dnf install zstd
- Arch: sudo pacman -S zstd
  1. 然后运行curl -fsSL https://ollama.com/install.sh | sh即可完成安装 官方文档参考: https://docs.ollama.com/linux

安装过程示例:

$ curl -fsSL https://ollama.com/install.sh | sh
>>> Cleaning up old version at /usr/local/lib/ollama
>>> Installing ollama to /usr/local
>>> Downloading ollama-linux-amd64.tar.zst
######################################################################## 100.0%
>>> Creating ollama user...
>>> Adding ollama user to render group...
>>> Adding ollama user to video group...
>>> Adding current user to ollama group...
>>> Creating ollama systemd service...
>>> Enabling and starting ollama service...
Created symlink /etc/systemd/system/default.target.wants/ollama.service → /etc/systemd/system/ollama.service.
>>> Nvidia GPU detected.
>>> The Ollama API is now available at 127.0.0.1:11434.
>>> Install complete. Run "ollama" from the command line.
  1. 验证安装后的运行状态

Ollama 将被安装成一个 systemd service, 安装完成后可查看 Ollama 服务状态:

systemctl status ollama

安装后的Ollama 服务将会默认监听在 127.0.0.1 的 11434 端口, 可以通过浏览器或 curl 访问http://127.0.0.1:11434, Ollama 运行正常时会返回Ollama is running

安装完成后可以修改 ollama 的 Service 文件通过环境变量 OLLAMA_HOST 修改其监听地址的端口. 比如在[Service]下添加一行Environment=OLLAMA_HOST=0.0.0.0:25000让其运行在可供外部访问的 25000 端口.

部署模型

访问 https://ollama.com/library, 查看可用模型, 然后通过ollama pull指令下载:

ollama pull qwen3.5:4b

通过ollama list可查看本地可以模型

$ ollama list
NAME ID SIZE MODIFIED
qwen3-embedding:4b df5bd2e3c74c 2.5 GB 28 minutes ago
qwen3.5:2b 324d162be6ca 2.7 GB 7 days ago
qwen2.5:3b 357c53fb659c 1.9 GB 7 days ago
nomic-embed-text:latest 0a109f422b47 274 MB 7 days ago
qwen3.5:4b 2a654d98e6fb 3.4 GB 7 days ago
qwen3:4b 359d7dd4bcda 2.5 GB 7 days ago
qwen3:8b 500a1f067a9f 5.2 GB 7 days ago

可通过ollama run指令运行一个模型并进入交互状态与AI对话, 也可以通过子命令查看模型信息

$ ollama run qwen3.5:2b
>>> /?
Available Commands:
/set Set session variables
/show Show model information
/load <model> Load a session or model
/save <model> Save your current session
/clear Clear session context
/bye Exit
/?, /help Help for a command
/? shortcuts Help for keyboard shortcuts

Use """ to begin a multi-line message.
Use /path/to/file to include .jpg, .png, or .webp images.

>>> /show info
Model
architecture qwen35
parameters 2.3B
context length 262144
embedding length 2048
quantization Q8_0
requires 0.17.1

Capabilities
completion
vision
tools
thinking

Parameters
presence_penalty 1.5
temperature 1
top_k 20
top_p 0.95

License
Apache License
Version 2.0, January 2004
...

>>> Send a message (/? for help)

对外服务

Ollama 提供 兼容 OpenAI 和 Anthropic 的 API, 可以直接在应用系统中调用

  1. 如果没有修改端口, 默认 base url 是: http://127.0.0.1:11434/v1
  2. 默认没有开启认证, 如果需要可设置环境变量OLLAMA_API_KEY, 然后在 HTTP Header 中以Authorization: Bearer $OLLAMA_API_KEY的方式认证

通过 curl 并指定模型qwen2.5:3b进行测试:

curl http://127.0.0.1:11434/api/generate -d '{
"model": "qwen2.5:3b",
"prompt": "天空为什么是蓝色的?",
"stream": false
}'

参考文档:

  1. OpenAI compatibility
  2. Anthropic compatibility

运行监控

可通过ollama ps指令查看正在使用中的模型或资源使用情况.

理想状态下是把模型完全运行中显存中, 对应的 PROCESSOR 是 100% GPU:

$ ollama ps
NAME ID SIZE PROCESSOR CONTEXT UNTIL
qwen2.5:3b 357c53fb659c 2.4 GB 100% GPU 4096 4 minutes from now

但如果显存不足, 就会出现溢出到内存的情况, 拖慢系统运行速度, PROCESSOR 中可以看到部分 CPU 部分 GPU 的情况:

$ ollama ps
NAME ID SIZE PROCESSOR CONTEXT UNTIL
qwen3.5:2b 324d162be6ca 4.5 GB 42%/58% CPU/GPU 4096 4 minutes from now
ClustrMaps