🦙

NixOSでpodman上でOllamaを動かす

に公開

モチベーション

Ollamaをホストするマシンをサッと作りたい。NvidiaのGPUを使いたい。とにかく簡単に。

コンテナとして動かす理由

NixOSのOllamaパッケージもある。ただ、Ollamaはバージョンアップがけっこう頻繁に行われるので、新しいモデルが出たときにすぐ使えると嬉しい。

OllamaはDockerイメージとしても提供されているので、これを使うと公式のバージョンが上がったときにすぐに追従できて便利。

設定方法

NiXOSをインストールする。コンソールだけでよい。

そして、 /etc/nixos/configurations.nix の末尾のほうに

  # ...

  hardware.graphics.enable = true;
  services.xserver.videoDrivers = [ "nvidia" ];
  hardware.nvidia = {
    modesetting.enable = true;
    powerManagement.enable = false;
    powerManagement.finegrained = false;
    open = false;
    nvidiaSettings = true;
  };
  hardware.nvidia-container-toolkit.enable = true;

  virtualisation.oci-containers = {
    containers = {
      ollama = {
        image = "ollama/ollama:0.12.9";
        ports = [ "127.0.0.1:11434:11434" ];
        devices = [ "nvidia.com/gpu=all" ];
        volumes = [ "ollama:/root/.ollama" ];
      };
    };
  };
}

と書く。これで

sudo nixos-rebuild switch

して再起動する。これでpodman上でOllamaが起動し、localhost:11434をlistenした状態になる。はず。

$ curl localhost:11434
Ollama is running

あとは煮るなり焼くなりすればよい。

systemd経由で状態を確認

上記の設定をすると、 podman-ollama.service が登録される。なので

$ systemctl status podman-ollama.service 

などとすれば状態を確認できる。GPUが認識されていればログの最後の行にその情報が出るはず。

11月 01 15:12:33 nixos ollama[9333]: time=2025-11-01T06:12:33.436Z level=INFO source=types.go:42 msg="inference compute" id=GPU-00000000-0000-0000-0000-000000000000 filtered_id="" library=CUDA compute=8.6 name=CUDA0 description="NVIDIA GeForce RTX 3090" libdirs=ollama,cuda_v12 driver=12.8 pci_id=0000:08:00.0 type=discrete total="24.0 GiB" available="23.5 GiB"

Ollamaのバージョンを変えたいとき

バージョンを 0.12.9 に固定してある。上げたくなったらここを書き換えて nixos-rebuild switch を実行すればよい。

まとめ

NixOSを使って、CUDAを有効にしてOllamaサーバをpodmanコンテナとして起動できた。細々いろいろな設定をしなくていいので簡単でよい。

Discussion