Change ollama model save location

 

Thanks the solution provided in https://www.zhihu.com/en/answer/3561622168 and https://aleksandarhaber.com/how-to-find-where-ollama-stores-downloaded-llm-models-on-linux-systems/.

Show the model save location

First make sure that plocate search tool is installed. We can install this tools as follows.

sudo apt install plocate
sudo updatedb

Then, search of ollama

plocate ollama | grep -w models

The output looks like this:

/usr/share/ollama/.ollama/models
/usr/share/ollama/.ollama/models/blobs
/usr/share/ollama/.ollama/models/manifests

Change the path for saving the model

  1. Create the path for saving the model The path /nvmedata/ollama/models can be changed to any other path you like.
    sudo mkdir /nvmedata/ollama/models
    
  2. Change the owner and group of the target path to root
    sudo chown -R root:root /nvmedata/ollama/models
    
  3. Change its file permissions to 755
    sudo chmod -R 755 /nvmedata/ollama/models
    
  4. Add the corresponding environment variables [Service] below Environment, including OLLAMA_HOST and OLLAMA_MODELS by sudo nano /etc/systemd/system/ollama.service
    Environment="OLLAMA_MODELS=/nvmedata/ollama/models" 
    Environment="OLLAMA_HOST=0.0.0.0:11434"
    

By setting this up, all models pulled by the ollama pull command in the future will be saved in /nvmedata/ollama/models, while models run by the ollama run command can be accessed at 0.0.0.0:11434. The modified ollama.service content is as follows:

[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/..."
Environment="OLLAMA_MODELS=/nvmedata/ollama/models"
Environment="OLLAMA_HOST=0.0.0.0:11434"

[Install]
WantedBy=default.target
  1. Finally, use the following two commands with sudo to make the settings take effect and restart ollama:
    sudo systemctl daemon-reload
    sudo systemctl restart ollama