Ubuntu下ollama的基本使用汇总 | 2025

安装和自启动

1
2
curl -fsSL https://ollama.com/install.sh | sh
systemctl enable ollama

版本更新

1
curl -fsSL https://ollama.com/install.sh | sh

直接执行模型,命令行交互

1
2
3
ollama run deepseek-llm
# 输入内容等待回答
/bye # 结束交互

其他常用操作

1
2
3
ollama list # 显示已安装模型列表
ollama ps # 查询活动的ollama进程,如果模型已经完整读入到GPU,PROCESSOR列会显示100% GPU
pgrep ollama | xargs -I% kill % # 结束活动的ollama进程

TroubleShoot1

docker容器无法连接宿主机的ollama服务

现象容器内部curl报错:
Failed to connect to 172.17.0.1 port 11434 after 0 ms: Connection refused
Failed to connect to host.docker.internal port 11434 after 0 ms: Connection refused
anythingllm报错:
Your Ollama instance could not be reached or is not responding. Please make sure it is running the API server and your connection information is correct in AnythingLLM.

原因

ollama服务默认监听127.0.0.1的11434端口,无法为容器提供服务。
127.0.0.1 和 0.0.0.0 的监听区别:

  • 127.0.0.1(localhost 监听)
    仅允许 本机访问,外部设备或 Docker 容器 无法访问。
  • 0.0.0.0(全网监听)
    允许来自 任何网络接口 的连接,包括本机、局域网和互联网。

解决方法

修改ollama服务的配置,通过0.0.0.0监听

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
systemctl edit ollama.service
-------------------------------------
# 在注释行中间添加配置,报存退出
### Editing /etc/systemd/system/ollama.service.d/override.conf
### Anything between here and the comment below will become the new contents of the file

[Service]
Environment="OLLAMA_HOST=0.0.0.0"

### Lines below this comment will be discarded
-------------------------------------
systemctl daemon-reload
systemctl restart ollama

# MacOS下,执行如下命令并重启ollama服务
launchctl setenv OLLAMA_HOST "0.0.0.0"

# Windows下,添加用户环境变量,然后重启ollama服务
OLLAMA_HOST 的值为 0.0.0.0