はじめに
Ollamaをサービスとして実行している際に、Ollamaの環境変数を設定する方法を紹介する。
動作確認環境
- Ubuntu 22.04
- Ollama v0.12.3
事前準備
- Ollamaがサービスとしてインストールされ、実行されていること
方法
公式ドキュメントの方法で環境変数を設定する。
設定ファイルの修正
以下のコマンドでOllamaのサービス設定を変更する。
sudo systemctl edit ollama
エディタが開いたら、以下のコメント行の間に環境変数を追記する。
### Anything between here and the comment below will become the new contents of the file
### Lines below this comment will be discarded
ポイント
英語のコメントにあるように、### Anything between here and the comment below will become the new contents of the file
と### Lines below this comment will be discarded
の間に必ず記載すること。
### 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:11434"
Environment="OLLAMA_CONTEXT_LENGTH=4096"
Environment="OLLAMA_MAX_LOADED_MODELS=1"
Environment="OLLAMA_NUM_PARALLEL=1"
Environment="OLLAMA_SCHED_SPREAD=0"
Environment="OLLAMA_FLASH_ATTENTION=1"
Environment="OLLAMA_KV_CACHE_TYPE=q8_0"
### Lines below this comment will be discarded
### /etc/systemd/system/ollama.service
# [Unit]
# Description=Ollama Service
# After=network-online.target
#
# [Service]
# ExecStart=/usr/local/bin/ollama serve
# User=ollama
# Group=ollama
# Restart=always
# RestartSec=3
# Environment="PATH=/home/<username>/.local/bin:/home/<username>/bin:/home/<username>/.local/bin:/home/<username>/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/b>
#
# [Install]
# WantedBy=default.target
設定の反映
以下のコマンドで設定を反映させる。
sudo systemctl daemon-reload
sudo systemctl restart ollama
ステータス確認
以下のコマンドでステータスを確認する。
sudo systemctl status ollama
以下のようになっていれば成功。
● ollama.service - Ollama Service Loaded: loaded (/etc/systemd/system/ollama.service; enabled; vendor preset: enabled)
Drop-In: /etc/systemd/system/ollama.service.d
└─override.conf
Active: active (running) since Tue 2025-09-30 06:44:28 JST; 5s ago
Main PID: 4145052 (ollama)
Tasks: 9 (limit: 36439)
Memory: 15.4M
CPU: 84ms
CGroup: /system.slice/ollama.service
└─4145052 /usr/local/bin/ollama serve
9月 30 06:44:28 agx-orin32gb-dev-2 systemd[1]: Started Ollama Service.
9月 30 06:44:28 agx-orin32gb-dev-2 ollama[4145052]: time=2025-09-30T06:44:28.634+09:00 level=INFO source=routes.go:1475 msg="server config" env="map[CUDA_VISIBLE_DEVICES: GPU_DEVICE_ORD> 9月 30 06:44:28 agx-orin32gb-dev-2 ollama[4145052]:\ time=2025-09-30T06:44:28.634+09:00 level=INFO source=images.go:518 msg="total blobs: 0"
9月 30 06:44:28 agx-orin32gb-dev-2 ollama[4145052]: time=2025-09-30T06:44:28.634+09:00 level=INFO source=images.go:525 msg="total unused blobs removed: 0"
9月 30 06:44:28 agx-orin32gb-dev-2 ollama[4145052]: time=2025-09-30T06:44:28.635+09:00 level=INFO source=routes.go:1528 msg="Listening on [::]:11434 (version 0.12.3)"
9月 30 06:44:28 agx-orin32gb-dev-2 ollama[4145052]: time=2025-09-30T06:44:28.635+09:00 level=INFO source=gpu.go:217 msg="looking for compatible GPUs"
9月 30 06:44:28 agx-orin32gb-dev-2 ollama[4145052]: time=2025-09-30T06:44:28.706+09:00 level=INFO source=types.go:131 msg="inference compute" id=GPU-36f5ecbd-0a65-51bd-8793-09c00532a5d0>~
環境変数の確認
以下のコマンドで設定されている環境変数を確認する。
systemctl show ollama -p Environment
以下のように設定した環境変数が表示されていれば成功。
Environment=PATH=/home/<username>/.local/bin:/home/<username>/bin:/home/<username>/.local/bin:/home/<username>/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin OLLAMA_HOST=0.0.0.0:11434 OLLAMA_CONTEXT_LENGTH=4096 OLLAMA_MAX_LOADED_MODELS=1 OLLAMA_NUM_PARALLEL=1 OLLAMA_SCHED_SPREAD=0 OLLAMA_FLASH_ATTENTION=1 OLLAMA_KV_CACHE_TYPE=q8_0
まとめ
Ollamaをサービスとして実行している際に、Ollamaの環境変数を設定する方法を紹介した。設定ファイルで環境変数を設定する場所を間違えるとエラーになるので気を付けてほしい。
トラブルシューティング
「temporary file is empty.」と表示され設定ファイルを保存できない
環境変数を記載して設定ファイルを保存すると以下のエラーとなることがある。
Editing "/etc/systemd/system/ollama.service.d/override.conf" canceled: temporary file is empty.
原因
設定ファイルが保存できていない時に発生する。何も書かずにファイルを閉じた、あるいは環境変数の記載位置が誤っているとファイルが更新されず、このエラーが発生する。
対処方法
環境変数を、### Anything between here and the comment below will become the new contents of the file
と### Lines below this comment will be discarded
の間に記載すると解消する。
参考