#ubuntu #ollama #qwen-coder #qwen #openclaw ```table-of-contents ``` # 一、系统要求 运行 `qwen2.5-coder:7b` 推荐配置: | 资源 | 最低 | 推荐 | | ---- | ------- | ---------- | | CPU | 4 cores | 8+ cores | | RAM | 8GB | 16GB | | GPU | 无需 | NVIDIA GPU | | Disk | 10GB | 20GB | | | | | 模型大小: ``` 约 4.5GB ``` --- # 二、Ubuntu 安装 Ollama ## 1 更新系统 ```bash sudo apt update sudo apt upgrade -y ``` 安装 curl ```bash sudo apt install -y curl ``` --- ## 2 安装 Ollama 执行官方安装脚本: ```bash curl -fsSL https://ollama.com/install.sh | sh ``` 安装过程会自动: - 安装 `ollama` CLI - 创建 systemd 服务 - 启动 Ollama API --- ## 3 验证安装 ```bash ollama --version ``` 示例: ``` ollama version 0.5.x ``` --- # 三、启动 Ollama 服务 检查状态: ```bash systemctl status ollama ``` 如果未运行: ```bash sudo systemctl start ollama ``` 开机启动: ```bash sudo systemctl enable ollama ``` --- # 四、下载 Qwen2.5-Coder 7B 下载模型: ```bash ollama pull qwen2.5-coder:7b ``` 下载大小: ``` ≈ 4.5GB ``` 下载完成查看: ```bash ollama list ``` 示例: ``` NAME SIZE qwen2.5-coder:7b 4.6 GB ``` --- # 五、运行模型 启动交互模式: ```bash ollama run qwen2.5-coder:7b ``` 终端将进入: ``` >>> Send a message (/? for help) ``` 测试: ``` Write a Python script to monitor CPU usage ``` 模型会生成代码。 --- # 六、通过 API 调用 Ollama 默认提供 REST API: ``` http://localhost:11434 ``` 测试 API: ```bash curl http://localhost:11434/api/chat -d '{ "model": "qwen2.5-coder:7b", "messages": [ {"role": "user", "content": "Write a bash script to backup a directory"} ] }' ``` 返回示例: ```json { "message": { "role": "assistant", "content": "Here is a bash backup script..." } } ``` --- # 七、Python 调用 安装 SDK: ```bash pip install ollama ``` 示例代码: ```python from ollama import chat response = chat( model="qwen2.5-coder:7b", messages=[ { "role": "user", "content": "Write a Python script to parse a CSV file" } ] ) print(response["message"]["content"]) ``` --- # 八、NodeJS 调用 安装 SDK: ```bash npm install ollama ``` 示例: ```javascript import ollama from 'ollama' const response = await ollama.chat({ model: 'qwen2.5-coder:7b', messages: [ { role: 'user', content: 'Write a docker-compose for n8n and postgres' } ] }) console.log(response.message.content) ``` --- # 九、开放远程 API(推荐) 默认只监听: ``` 127.0.0.1 ``` 如果要给: - n8n - OpenClaw - WebUI - Agent 使用,需要修改。 编辑: ``` /etc/systemd/system/ollama.service ``` 增加: ``` Environment="OLLAMA_HOST=0.0.0.0" ``` 重新加载: ```bash sudo systemctl daemon-reload sudo systemctl restart ollama ``` 访问: ``` http://服务器IP:11434 ``` --- # 十、GPU 加速(可选) 检查 GPU: ```bash nvidia-smi ``` 如果安装了 CUDA: Ollama 会 **自动使用 GPU**。 无需额外配置。 --- # 十一、模型管理 查看模型: ```bash ollama list ``` 删除模型: ```bash ollama rm qwen2.5-coder:7b ``` 更新模型: ```bash ollama pull qwen2.5-coder:7b ``` --- # 十二、性能优化 如果机器较小: 推荐: ``` qwen2.5-coder:3b ``` 下载: ```bash ollama pull qwen2.5-coder:3b ``` --- # 十三、推荐搭配工具 如果你正在做 **AI automation / agent / coding** 建议搭配: |工具|用途| |---|---| |Open WebUI|ChatGPT UI| |n8n|AI automation| |LangChain|Agent framework| |OpenClaw|AI coding agent| 例如 OpenClaw 配置: ``` ollama/qwen2.5-coder:7b ``` --- # 十四、最简安装流程(3条命令) 如果你只想快速安装: ```bash curl -fsSL https://ollama.com/install.sh | sh ollama pull qwen2.5-coder:7b ollama run qwen2.5-coder:7b ``` --- # 十五、一个很多人不知道的优化(非常重要) 如果你打算做: - DevOps automation - SQL Agent - Kubernetes troubleshooting - n8n workflow AI 强烈建议使用: ``` qwen2.5-coder:7b ``` 原因: - Tool usage能力强 - Shell / Python / SQL理解强 - Repo级代码理解 比普通 `qwen2.5:7b` **更适合工程任务**。 --- 如果需要,我可以再给你一份 **更高级部署指南**: **《Ubuntu 本地 AI Stack:Ollama + Qwen2.5-Coder + OpenWebUI + n8n + Agent 自动化》** 会包含: - 本地 ChatGPT UI - AI Coding Agent - 自动化工作流 - DevOps AI 助手 基本上是一套 **完整的本地 AI 基础设施(非常适合开发者)**。