大语言模型:Linux系统下源码编译Ollama指南

为了进一步学习Ollama底层机制,方便代码分析、问题调试,本文将简单介绍在Linux环境下源码编译Ollama的全流程,整个编译过程在没有GPU的设备上运行,只编译纯CPU的Ollama版本。

必备工具

编译Ollama需要一些基础的开发工具和依赖库

js 复制代码
$ go version
go version go1.23.4 linux/amd64
$ git --version
git version 2.25.1
$ make --version
GNU Make 4.2.1
Built for x86_64-pc-linux-gnu
Copyright (C) 1988-2016 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
$ gcc --version
gcc (Ubuntu 9.4.0-1ubuntu1~20.04.2) 9.4.0
Copyright (C) 2019 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

编译源码

js 复制代码
# 克隆源码,拉取标签为v0.5.7的版本
$ git clone -b v0.5.7 https://github.com/ollama/ollama.git ollama-0.5.7
Cloning into 'ollama-0.5.7'...
remote: Enumerating objects: 30629, done.
remote: Counting objects: 100% (163/163), done.
remote: Compressing objects: 100% (106/106), done.
remote: Total 30629 (delta 111), reused 57 (delta 57), pack-reused 30466 (from 4)
Receiving objects: 100% (30629/30629), 33.54 MiB | 1.79 MiB/s, done.
Resolving deltas: 100% (19453/19453), done.
Note: switching to 'a420a453b4783841e3e79c248ef0fe9548df6914'.

You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by switching back to a branch.

If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -c with the switch command. Example:

  git switch -c <new-branch-name>

Or undo this operation with:

  git switch -

Turn off this advice by setting config variable advice.detachedHead to false
$ cd ollama-0.5.7
$ go mod tidy
$ export CGO_ENABLED=1
$ make help
The following make targets will help you build Ollama

    make all           # (default target) Build Ollama llm subprocess runners, and the primary ollama executable
    make runners        # Build Ollama llm subprocess runners; after you may use 'go build .' to build the primary ollama exectuable
    make <runner>        # Build specific runners. Enabled: 'cpu'
    make dist        # Build the runners and primary ollama executable for distribution
    make help-sync         # Help information on vendor update targets
    make help-runners     # Help information on runner targets

The following make targets will help you test Ollama

    make test           # Run unit tests
    make integration    # Run integration tests.  You must 'make all' first
    make lint           # Run lint and style tests

For more information see 'docs/development.md'
# 执行编译
$ make -j 5
GOARCH=amd64 go build -buildmode=pie "-ldflags=-w -s \"-X=github.com/ollama/ollama/version.Version=0.5.7-0-ga420a45\"  " -trimpath -tags "avx" -o llama/build/linux-amd64/runners/cpu_avx/ollama_llama_server ./cmd/runner
GOARCH=amd64 go build -buildmode=pie "-ldflags=-w -s \"-X=github.com/ollama/ollama/version.Version=0.5.7-0-ga420a45\"  " -trimpath -tags "avx,avx2" -o llama/build/linux-amd64/runners/cpu_avx2/ollama_llama_server ./cmd/runner
GOARCH=amd64 go build -buildmode=pie "-ldflags=-w -s \"-X=github.com/ollama/ollama/version.Version=0.5.7-0-ga420a45\"  " -trimpath  -o ollama .
# 仅编译主程序(ollama)
$ go build .

编译完成后将生成运行器(ollama_llama_server)和主程序(ollama)

js 复制代码
$ find . -type f -executable | grep -v ".sh" | grep -v ".sample"
./ollama
./llama/build/linux-amd64/runners/cpu_avx2/ollama_llama_server
./llama/build/linux-amd64/runners/cpu_avx/ollama_llama_server

运行测试

js 复制代码
$ ./ollama --help
Large language model runner

Usage:
  ollama [flags]
  ollama [command]

Available Commands:
  serve       Start ollama
  create      Create a model from a Modelfile
  show        Show information for a model
  run         Run a model
  stop        Stop a running model
  pull        Pull a model from a registry
  push        Push a model to a registry
  list        List models
  ps          List running models
  cp          Copy a model
  rm          Remove a model
  help        Help about any command

Flags:
  -h, --help      help for ollama
  -v, --version   Show version information

Use "ollama [command] --help" for more information about a command.
# 启动服务
$ ./ollama serve > ollama.log 2>&1 &
# 运行模型
$ ./ollama run deepseek-r1:1.5b "你是谁?"
<think>

</think>

您好!我是由中国的深度求索(DeepSeek)公司开发的智能助手DeepSeek-R1。如您有任何任何问题,我会尽我所能为您提供帮助。
相关推荐
小爷毛毛_卓寿杰2 小时前
突破政务文档理解瓶颈:基于多模态大模型的智能解析系统详解
人工智能·llm
磊叔的技术博客2 小时前
LLM 系列(六):模型推理篇
人工智能·面试·llm
ResponsibilityAmbiti9 小时前
AI 发展 && MCP
人工智能·llm·aigc
AI大模型10 小时前
LangGraph官方文档笔记(6)——时间旅行
程序员·langchain·llm
CoderLiu1 天前
用这个MCP,只给大模型一个figma链接就能直接导出图片,还能自动压缩上传?
前端·llm·mcp
会飞的果粒橙1 天前
在Ubuntu24上安装ollama
ollama·deepseek
智泊AI1 天前
大语言模型LLM底层技术原理到底是什么?大型语言模型如何工作?
llm
moonless02221 天前
🌈Transformer说人话版(二)位置编码 【持续更新ing】
人工智能·llm
小爷毛毛_卓寿杰1 天前
基于大模型与知识图谱的对话引导意图澄清系统技术解析
人工智能·llm
聚客AI1 天前
解构高效提示工程:分层模型、文本扩展引擎与可视化调试全链路指南
人工智能·llm·掘金·日新计划