Zenn
🤗

Ubuntu 以外の Distro で Intel GPU を効かせて Ollama を動かす

2025/03/23に公開

対象読者

背景

https://ollama.com/

などで配布されている本家版では NVIDIA と AMD と Apple の GPU しかサポートしていないようです。 Intel は Intel GPU に対応した Ollama を次のサイトで配布しています。

https://github.com/intel/ipex-llm/blob/main/docs/mddocs/Quickstart/ollama_portable_zip_quickstart.md#run-ollama-portable-zip-on-intel-gpu-with-ipex-llm

ところが Windows と Ubuntu しか対象にしていません (ありがち) 。

対応

Docker を使います (畢竟 Ubuntu コンテナー環境で実行しているだけというへなちょこ) 。

Dockerfile

Dockerfile
FROM ubuntu:24.04
WORKDIR /root
# https://dgpu-docs.intel.com/driver/client/overview.html#installing-client-gpus-on-ubuntu-desktop-24-04-lts
RUN apt-get update && apt-get -y upgrade && \
    apt-get install -q -y --no-install-recommends ca-certificates gpg wget && \
    wget -qO - https://repositories.intel.com/gpu/intel-graphics.key | gpg --yes --dearmor --output /usr/share/keyrings/intel-graphics.gpg && \
    echo "deb [arch=amd64,i386 signed-by=/usr/share/keyrings/intel-graphics.gpg] https://repositories.intel.com/gpu/ubuntu noble unified" >/etc/apt/sources.list.d/intel-gpu-noble.list && \
    apt-get update && \
    apt-get install -y libze-intel-gpu1 libze1 intel-opencl-icd clinfo intel-gsc
# https://github.com/intel/ipex-llm/blob/main/docs/mddocs/Quickstart/ollama_portable_zip_quickstart.md#linux-quickstart
ARG OLLAMA_PORTABLE_ZIP
ADD ./$OLLAMA_PORTABLE_ZIP /opt
RUN cd /opt && ln -s ${OLLAMA_PORTABLE_ZIP%.*} ollama
ENV PATH=/opt/ollama:$PATH OLLAMA_INTEL_GPU=true
WORKDIR /opt/ollama
ENTRYPOINT ["start-ollama.sh"]

docker build

https://github.com/intel/ipex-llm/blob/main/docs/mddocs/Quickstart/ollama_portable_zip_quickstart.md#linux-quickstart

のリンク先から適切な .tgz をダウンロードして Dockerfile と同じ場所に置きます。そこへ cd して

$ podman build -t ollama-ipexllm --build-arg OLLAMA_PORTABLE_ZIP=ollama-ipex-llm-2.2.0b20250318-ubuntu.tgz .

ollama-ipex-llm-2.2.0b20250318-ubuntu.tgz の部分はもちろん実際にダウンロードしたファイル名を指定します。なお、ここでは Fedora Workstation に付属の Podmandocker の代わりに使いました。

$ podman images
REPOSITORY                TAG         IMAGE ID      CREATED        SIZE
localhost/ollama-ipexllm  latest      614b9cd1a6eb  4 seconds ago  975 MB
docker.io/library/ubuntu  24.04       a04dc4851cbc  8 weeks ago    80.7 MB
docker.io/library/ubuntu  latest      a04dc4851cbc  8 weeks ago    80.7 MB

docker run

次のようにして ollama serve 相当を実行します。

$ podman run -it --name ollama-ipexllm --net=host --device=/dev/dri -v ~/.ollama/models/:/root/.ollama/models:z ollama-ipexllm

--device=/dev/dri が Docker コンテナーから GPU にアクセスするためのきもらしいです[1]

-v ... でホスト側とコンテナー側のディレクトリーを共有しますが SELinux が有効の場合 :z をつけないと Permission denied をくらいます[2]

終了するときは Ctrl + C を押下します。再開するときは上のコマンドの再実行ではだめで

$ podman start -a ollama-ipexllm

docker exec

ollama クライアントの実行は別のウインドウで

$ podman exec -it ollama-ipexllm ollama help
ggml_sycl_init: found 1 SYCL devices:
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.
$ podman exec -it ollama-ipexllm ollama run llama3.2
ggml_sycl_init: found 1 SYCL devices:
>>> Hello. Please introduce yourself.
Nice to meet you! I'm an artificial intelligence model, which means I'm a computer program designed to simulate conversation 
and answer questions to the best of my ability. My primary goal is to provide helpful and informative responses while learning 
and improving over time.

I don't have a personal name, but you can think of me as a friendly AI assistant. I'm here to help with any questions or 
topics you'd like to discuss, from science and history to entertainment and culture.

Feel free to ask me anything, and I'll do my best to provide a helpful response!

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

使い勝手を改善

次のようなシェル スクリプトを ~/.local/bin あたりに置いてパスをとおして chmod u+x しておくとよいと思います。

ollama
#!/bin/bash

docker=podman
name=ollama-ipexllm
if [[ $($docker ps -a -q -f name=$name) = '' ]]; then
    $docker create -it --name $name --net=host --device=/dev/dri -v ~/.ollama/models/:/root/.ollama/models:z $name >/dev/null
fi
status=$($docker inspect --format '{{.State.Status}}' $name) 
if [[ "$status" = 'running' ]]; then
    exec $docker exec -it $name ollama "$@"
elif [[ "$1" = 'serve' ]]; then
    exec $docker start -a $name
else
    echo "\`$(basename $0) serve\` is not running." 1>&2
fi

参考文献

付録

GPU 使用率の表示

Windows だとタスク マネージャーに GPU の使用率がグラフで表示されますが、あいにく GNOME System Monitor にはないです。 Intel GPU の場合 igt-gpu-tools というパッケージにふくまれる intel_gpu_top というプログラムで CUI ではあるものの使用率がビジュアルに表示されます。

$ sudo dnf install igt-gpu-tools
$ sudo setcap cap_perfmon=+ep /usr/bin/intel_gpu_top
$ intel_gpu_top

q をタイプすると終了します。 setcap cap_perfmon=+ep ...intel_gpu_top を一般ユーザーで実行するために必要ですが、リブートすると元に戻ってしまうようなので systemd に登録するとよいそうです[3]

脚注
  1. dri は Direct Rendering Infrastructure とのこと (Linux allocated devices (4.x+ version)) 。 ↩︎

  2. Executing podman: Permission denied ↩︎

  3. Intel GPU Monitor ↩︎

Discussion

ログインするとコメントできます