linux ollama 下载安装
- ollama 下载地址
- ollama 手动安装
- 解决找不到命令的问题
- 升级[gcc](https://so.csdn.net/so/search?q=gcc&spm=1001.2101.3001.7020)
- Centos7 停止维护之后 [升级gcc||找不到devtoolset-8-gcc* 问题解决方案](https://www.cnblogs.com/Jedi-Pz/p/18447117)(连接)
- 修改 CentOS-SCLo-scl.repo
- 修改 CentOS-SCLo-scl-rh.repo
- 刷新缓存
- make
- 更新libstdc++.so.6.0.26
- 更新[glibc](https://so.csdn.net/so/search?q=glibc&spm=1001.2101.3001.7020)
- 参考连接
- 启动 ollama
- 配置 systemctl
- 刷新systemctl文件
- 启动 systemctl
- 测试
- 下载一个qwen2.5:3b模型
- 发送一个聊天请求
- 使用Golang调用 Ollama
ollama 下载地址
下载: https://ollama.com/download/linux
github; https://github.com/ollama/ollama
ollama 手动安装
curl -L https://ollama.com/download/ollama-linux-amd64.tgz -o ollama-linux-amd64.tgz
sudo tar -C /usr -xzf ollama-linux-amd64.tgz
执行完上边的命令, 安装基本就算完成了, 不过因为系统版本问题, 还会出现找不到命令的问题
解决找不到命令的问题
升级gcc
# 升级GCC(默认为4 升级为8)
yum install -y centos-release-scl bison
yum install -y devtoolset-8-gcc*
ln -s /opt/rh/devtoolset-8/root/bin/gcc /usr/bin/gcc
ln -s /opt/rh/devtoolset-8/root/bin/g++ /usr/bin/g++
ps 在执行 yum install -y devtoolset-8-gcc* 命令时可能会出现下图的报错, 这是因为 Centos7 停止维护之后 升级gcc||找不到devtoolset-8-gcc* 不用担心能解决
Centos7 停止维护之后 升级gcc||找不到devtoolset-8-gcc* 问题解决方案(连接)
修改 CentOS-SCLo-scl.repo
[centos-sclo-sclo]
name=CentOS-7 - SCLo sclo
baseurl=https://mirrors.aliyun.com/centos/7/sclo/x86_64/sclo/
# mirrorlist=http://mirrorlist.centos.org?arch=$basearch&release=7&repo=sclo-sclo
gpgcheck=0
enabled=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-SIG-SCLo
修改 CentOS-SCLo-scl-rh.repo
[centos-sclo-rh]
name=CentOS-7 - SCLo rh
baseurl=https://mirrors.aliyun.com/centos/7/sclo/x86_64/rh/
# mirrorlist=http://mirrorlist.centos.org?arch=$basearch&release=7&repo=sclo-rh
gpgcheck=0
enabled=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-SIG-SCLo
刷新缓存
yum repolist
yum clean all
yum makecache
上边三步都执行完了以后再yum install -y devtoolset-8-gcc* 就没问题了
make
# 升级 make(默认为3 升级为4)
wget http://ftp.gnu.org/gnu/make/make-4.3.tar.gz
tar -xzvf make-4.3.tar.gz && cd make-4.3/
./configure --prefix=/usr/local/make
make && make install
cd /usr/bin/ && mv make make.bak
ln -sv /usr/local/make/bin/make /usr/bin/make
更新libstdc++.so.6.0.26
# 更新lib libstdc++.so.6.0.26wget https://cdn.frostbelt.cn/software/libstdc%2B%2B.so.6.0.26
# 替换系统中的/usr/lib64
cp libstdc++.so.6.0.26 /usr/lib64/
cd /usr/lib64/
ln -snf ./libstdc++.so.6.0.26 libstdc++.so.6
更新glibc
wget http://ftp.gnu.org/gnu/glibc/glibc-2.28.tar.gz
tar xf glibc-2.28.tar.gz
cd glibc-2.28/ && mkdir build && cd build
../configure --prefix=/usr --disable-profile --enable-add-ons --with-headers=/usr/include --with-binutils=/usr/bin
make && make install
参考连接
https://blog.csdn.net/my_errors/article/details/146333105
https://www.cnblogs.com/Jedi-Pz/p/18447117
启动 ollama
nohup ollama serve &> output.log & 后台启动ollama 日志输出到 output.log 文件中
ollama create #从模型文件创建模型
ollama show #显示模型信息
ollama run #运行模型
ollama pull #从注册表中拉取模型
ollama push #将模型推送到注册表
ollama list #列出模型
ollama cp #复制模型
ollama rm #删除模型
ollama help #获取有关任何命令的帮助信息
这几个命令打出来没问题了, 就可以配置 systemctl 进行管理
配置 systemctl
vim /etc/systemd/system/ollama.service[Unit]
Description=Ollama Service
After=network-online.target[Service]
ExecStart=/usr/bin/ollama serve
User=启动ollama 的用户名
Group=启动ollama 的用户 组
Restart=always
RestartSec=3
# 加上这两个环境变量是为了让ollama在外部可以正常访问
# 同时也要看防火墙和平台端口出入规则
Environment="OLLAMA_HOST=0.0.0.0:11424"
Environment="OLLAMA_ORIGINS=*"[Install]
WantedBy=default.target
刷新systemctl文件
systemctl daemon-reload
systemctl enable ollama
启动 systemctl
# 关闭后台启动的ollama
ps aux | grep ollama
kill 进程号
# 重新启动 ollama
systemctl start ollama
到这里基本上是安装完了, 不出意外的话, 可以调用接口进行对话了
测试
下载一个qwen2.5:3b模型
ollama run qwen2.5:3b
更多模型参考 https://ollama.com/library/qwen2.5:3b
我的服务器只有8G内存, 下载个小的, 太大了运行不用起来
发送一个聊天请求
#
curl --location --request POST 'http://8.139.5.142:11434/api/chat' \
--header 'Content-Type: application/json' \
--data-raw '{"model":"qwen2.5:3b","messages":[{"role":"user","content":"您好"}],"stream":false
}'
使用Golang调用 Ollama
package mainimport ("bytes""encoding/json""fmt""net/http""os""time"
)type Request struct {Model string `json:"model"`Messages []Message `json:"messages"`Stream bool `json:"stream"`
}type Message struct {Role string `json:"role"`Content string `json:"content"`
}type Response struct {Model string `json:"model"`CreatedAt time.Time `json:"created_at"`Message Message `json:"message"`Done bool `json:"done"`TotalDuration int64 `json:"total_duration"`LoadDuration int `json:"load_duration"`PromptEvalCount int `json:"prompt_eval_count"`PromptEvalDuration int `json:"prompt_eval_duration"`EvalCount int `json:"eval_count"`EvalDuration int64 `json:"eval_duration"`
}const defaultOllamaURL = "http://localhost:11434/api/chat"func main() {start := time.Now()msg := Message{Role: "user",Content: "您好",}req := Request{Model: "gemma3:1b",Stream: false,Messages: []Message{msg},}resp, err := talkToOllama(defaultOllamaURL, req)if err != nil {fmt.Println(err)os.Exit(1)}fmt.Println(resp.Message.Content)fmt.Printf("Completed in %v", time.Since(start))
}func talkToOllama(url string, ollamaReq Request) (*Response, error) {js, err := json.Marshal(&ollamaReq)if err != nil {return nil, err}client := http.Client{}httpReq, err := http.NewRequest(http.MethodPost, url, bytes.NewReader(js))if err != nil {return nil, err}httpResp, err := client.Do(httpReq)if err != nil {return nil, err}defer httpResp.Body.Close()ollamaResp := Response{}err = json.NewDecoder(httpResp.Body).Decode(&ollamaResp)return &ollamaResp, err
}