~/ / AI articles / Local LLMs on a VPS / Import a Hugging Face model into Ollama: GGUF and Safetensors, step by step
Import a Hugging Face model into Ollama: GGUF and Safetensors, step by step
Not every model is in the Ollama library, but many of them are on Hugging Face. Ollama can run them in two ways: straight from a GGUF repository with ollama run hf.co, or from files you download, with a Modelfile and ollama create. This article walks through both, using only what the Ollama and Hugging Face documentation and Ollama's source code say, and shows what to check in the files before you import them. Four tools on this page read the files and the Modelfile in your browser.
This article contains affiliate links (ads) at the end.
Published: · 6 min read · Checked against the Ollama and Hugging Face documentation and the source code of Ollama v0.40.16 on 2026-11-16. Import support changes between versions, so check the current documentation before you rely on it.
Two ways to bring a model in
If the model is published as GGUF, you may not need a Modelfile at all. The Hugging Face documentation says that you can run a GGUF repository with ollama run hf.co/{username}/{repository}, and that huggingface.co works as the domain as well.
The other way is the one Ollama's import page describes: download the files, write a Modelfile whose FROM line points to them, and build the model with ollama create. You need it for a Safetensors folder, or when the files are already on your server.
| Point | ollama run hf.co/... | Modelfile and ollama create |
|---|---|---|
| What it takes | A GGUF repository on Hugging Face | GGUF files, or a folder of Safetensors weights |
| Quantization | Q4_K_M when the repository has it, otherwise one the Hub picks; add :{quantization} to choose | The one in the file. Ollama does not quantize GGUF models during import |
| Chat template | Chosen from the tokenizer.chat_template stored in the GGUF, or from a file named template in the repository | TEMPLATE in the Modelfile, or the one in the GGUF if you leave it out |
| Private models | Add your Ollama public key to your Hugging Face account as an SSH key | Log in when you download, for example with hf auth login |
The Hugging Face documentation also says that a file named system in the repository sets the system prompt and a JSON file named params sets the sampling parameters, and that the template file must be a Go template, not a Jinja template.
Download the files
Hugging Face's command line tool is hf. Its guide installs it on macOS and Linux with curl -LsSf https://hf.co/cli/install.sh | bash, or with pip install -U "huggingface_hub".
hf download <repository> <file> --local-dir <folder> puts a file in a folder of your choice instead of the cache. With --include and --exclude you choose files by pattern, for example --include "*.safetensors" for all the weights of a model. For a private repository, log in first with hf auth login.
A Safetensors model is usually several files. Download config.json with the weights, because Ollama looks for it in the folder, and model.safetensors.index.json if the repository has one: the Safetensors Metadata Viewer further down uses it to find missing files.
Check a GGUF before you import it
A GGUF file carries its metadata with the weights. The GGUF specification defines general.architecture for the architecture, general.file_type for the type of most of the tensors, and [llm].context_length for the context length the model was trained on. Ollama's import page says that Ollama does not quantize GGUF models during import, so the quantization you see here is the one you will run; to change it, quantize the file first with a tool such as llama.cpp's llama-quantize.
A large model can come split into several files. The specification names the shards with a number that starts at 00001, as in 00001-of-00003. Download all of them and keep their names.
The viewer below reads the header of a GGUF file in your browser: the architecture, the quantization, the context length, the chat template and every metadata entry.
Try it: GGUF Metadata Viewer
Or drop a file here. Only the start of the file is read, and it stays on your computer. In our test, the weights file Ollama keeps in its models/blobs folder was a GGUF file too.
KV cache = 2 × layers × KV heads × head size × 2 bytes (f16) × context length. For qwen2.5:7b this gives 224 MiB at 4096 tokens, the size in Ollama's log in our test.
The weights are the tensor data stored in the file; together with the header they make up the file. Running the model also needs the KV cache and working memory, which the memory estimator adds.
Checked on 2026-11-08 against the GGUF specification and llama.cpp's reader, and with the real file of qwen2.5:7b: its metadata and 339 tensors matched what Ollama 0.40.11 reports. GGUF specification · Ollama: importing a model
The file is read in your browser and is not uploaded. Only the start of it is read.
Write a Modelfile and build the model
For a single file, the Modelfile needs one line: FROM /path/to/file.gguf. The Modelfile reference says that the path can be absolute or relative to the Modelfile. For a split model, keep the original file names and use a wildcard that matches every shard, such as FROM /path/to/model-*.gguf, or list the shards with separate FROM lines.
Then run ollama create my-model in the folder that holds the Modelfile, or point to the file with -f, and try the model with ollama run my-model. PARAMETER, SYSTEM and LICENSE lines can go in the same Modelfile.
The checker below reads a Modelfile the way ollama create does and shows the error Ollama would print, with its line. The Ollama Modelfile Generator builds one from a form.
Try it: Ollama Modelfile Checker
Paste the whole file. ollama show --modelfile <model> prints the Modelfile of a model you have.
ollama create can read this Modelfile
No errors in the steps this tool can check. Whether FROM and any files exist is checked only by ollama create on your computer.
Notes
- FROM qwen2.5:7b is a model name: the model must be available to Ollama.
What the instructions become
- FROM
- qwen2.5:7b
- SYSTEM
- You are a concise assistant. Answer in one or two sentences.
- TEMPLATE
- Not set: taken from the FROM model
PARAMETER
| Name | Type in Ollama | Stored value |
|---|---|---|
| num_ctx | int | 8192 |
| stop | string[] | ["\u003c|im_end|\u003e"] |
| temperature | float32 | 0.7 |
The parameters layer of the new model (JSON):
{"num_ctx":8192,"stop":["\u003c|im_end|\u003e"],"temperature":0.7}Parameters of the FROM model that you do not set here are kept as well.
MESSAGE (example conversation)
- userIs Toronto in Canada?
- assistantyes
The Modelfile as Ollama writes it back
One line per instruction, without comments, in the format of Command.String in Ollama's source.
FROM qwen2.5:7b PARAMETER num_ctx 8192 PARAMETER temperature 0.7 PARAMETER stop <|im_end|> SYSTEM " You are a concise assistant. Answer in one or two sentences. " MESSAGE user Is Toronto in Canada? MESSAGE assistant yes
Checked on 2026-11-09 against Ollama's source (v0.40.15), its documentation and ollama create in Ollama 0.40.11: Ollama: Modelfile Reference · parser.go
Everything runs in your browser. Nothing is sent anywhere.
Import a Safetensors folder
For Safetensors, FROM points to the folder that holds the weights, or FROM . when the Modelfile is in that folder. The Modelfile reference says that the folder should contain the weights for a supported architecture. The documentation does not list those architectures.
In the source code of Ollama v0.40.16, a folder counts as a Safetensors model when it has a config.json and at least one .safetensors file, and it is imported for Ollama's MLX engine. The import stops with an error that says unsupported MLX architecture when the architecture named in config.json is not one the MLX engine knows, and with MLX runtime is not available when that engine cannot be loaded. Ollama's development guide says that the MLX engine runs Safetensors models and that it is on by default on macOS on Apple silicon.
The architectures registered in that version include LlamaForCausalLM, Qwen3ForCausalLM and Gemma4ForCausalLM, but not every architecture on Hugging Face. When a folder is refused, look for a GGUF of the same model. We did not try a Safetensors import on a server without a GPU.
The viewer below reads the headers of the .safetensors files in your browser. Add model.safetensors.index.json to find missing shards, and config.json to see the architecture.
Try it: Safetensors Metadata Viewer
Or drop files here: .safetensors files, model.safetensors.index.json and config.json. Only the start of each .safetensors file is read, and nothing leaves your computer.
curl -L -r 0-1048575 -o model.safetensors https://huggingface.co/OWNER/REPO/resolve/main/model.safetensorsThe parameter count adds up the elements of every tensor in the files (the product of its shape), the same way the Hugging Face Hub counts the parameters it shows for a model. The size of the weights is the data the files hold, in the data types of the files.
KV cache = 2 × layers × KV heads × head size × 2 bytes (f16) × context length. The values come from config.json (num_hidden_layers, num_key_value_heads, head_dim, or hidden_size ÷ num_attention_heads). For models whose text part is in text_config, the tool reads it from there.
Checked on 2026-11-09 against these sources and the official safetensors 0.8.0: safetensors format · Hugging Face: reading the metadata · Ollama: importing a model
Everything runs in your browser. Only the start of each .safetensors file is read, and nothing is sent anywhere.
Which chat template runs
A chat model needs its messages wrapped in the special tokens it was trained with. In Ollama this is the TEMPLATE of the model, written in Go's template syntax.
When the Modelfile of a GGUF import has no TEMPLATE, the source code of Ollama v0.40.16 compares the tokenizer.chat_template in the GGUF with the templates that Ollama ships. If one matches, ollama create prints using autodetected template followed by its name and stores it as the TEMPLATE. If none matches, Ollama runs the chat template from the GGUF itself.
ollama show my-model prints a template source line that says which one runs: go_template for a TEMPLATE, gguf_chat_template for the template in the GGUF. When we imported the GGUF of qwen2.5:7b with only a FROM line on Ollama 0.40.11, ollama create printed no autodetected template, and ollama show listed gguf_chat_template, with tools and completion as its capabilities.
If you write a TEMPLATE yourself, the tester below shows the exact text it produces for a conversation. To compare it with the original Jinja template of the model, which transformers keeps in chat_template.jinja or in tokenizer_config.json, use the Hugging Face Chat Template Tester.
Even with a TEMPLATE, the same source can prefer the template in the GGUF, for example when the GGUF template supports more capabilities, such as tool calls. Check the template source line instead of assuming.
Try it: Ollama Template Tester
ollama show --template <model> prints the template of a model you have. The text between the triple quotes after TEMPLATE in a Modelfile works too.
The same shape as /api/chat: role, content, and optionally thinking, tool_calls, tool_name and images.
The same shape as tools in /api/chat. Leave it empty for no tools.
Advanced: the model's SYSTEM and its GGUF chat template
The SYSTEM line of the Modelfile. Ollama adds it when the first message is not a system message (in /api/generate, when system is empty).
Paste it to see whether Ollama uses TEMPLATE or this one. GGUF Metadata Viewer shows it.
What the model receives
<|im_start|>system
You are a helpful assistant.
# Tools
You may call one or more functions to assist with the user query.
You are provided with function signatures within <tools></tools> XML tags:
<tools>
{"type": "function", "function": {get_weather Get the current weather for a city {object <nil> <nil> [city] {"city":{"type":"string","description":"City name"}}}}}
</tools>
For each function call, return a json object with function name and arguments within <tool_call></tool_call> XML tags:
<tool_call>
{"name": <function-name>, "arguments": <args-json-object>}
</tool_call><|im_end|>
<|im_start|>user
What is the weather in Tokyo?<|im_end|>
<|im_start|>assistant
<tool_call>
{"name": "get_weather", "arguments": {"city":"Tokyo"}}
</tool_call><|im_end|>
<|im_start|>user
<tool_response>
{"temperature": 20}
</tool_response><|im_end|>
<|im_start|>assistant
It is 20°C in Tokyo.<|im_end|>
<|im_start|>user
Thanks!<|im_end|>
<|im_start|>assistant
About this template
- How Ollama ran it
- Once, with the whole conversation in .Messages
- Names it uses
- $, $i, $last, arguments, content, function, messages, name, prompt, response, role, system, toolcalls, tools
- What it supports (for Ollama)
- tools
Check with your own Ollama
Replace MODEL with the name of your model. _debug_render_only is a debug option in Ollama's source, not in the API documentation, so it may change. The answer has the prompt in _debug_info.rendered_template.
curl -s http://localhost:11434/api/chat -d '{"model":"MODEL","messages":[{"role":"system","content":"You are a helpful assistant."},{"role":"user","content":"What is the weather in Tokyo?"},{"role":"assistant","content":"","tool_calls":[{"function":{"name":"get_weather","arguments":{"city":"Tokyo"}}}]},{"role":"tool","content":"{\"temperature\": 20}","tool_name":"get_weather"},{"role":"assistant","content":"It is 20°C in Tokyo."},{"role":"user","content":"Thanks!"}],"tools":[{"type":"function","function":{"name":"get_weather","description":"Get the current weather for a city","parameters":{"type":"object","properties":{"city":{"type":"string","description":"City name"}},"required":["city"]}}}],"stream":false,"_debug_render_only":true}'Models with a RENDERER or PARSER line in the Modelfile, and gpt-oss, use renderers built into Ollama, not TEMPLATE.
When the conversation is longer than the context (num_ctx), Ollama drops the oldest messages but keeps the system messages and the last message. This tool does not count tokens, so it always shows the whole conversation.
For qwen3 models and deepseek-r1, Ollama removes <think>…</think> from assistant messages before the last user message. This tool does not.
Printing a whole message or {{ .Messages }} shows memory addresses in Ollama (0xc…), which change every time. The tool shows a placeholder.
currentDate and yesterdayDate use today's date on your computer. Ollama uses the date on the server.
Checked on 2026-11-09 against Ollama's source (v0.40.15), Go's text/template and Ollama 0.40.11: Ollama: Template · template.go
Everything runs in your browser. Nothing is sent anywhere.
Before you use the model
- Run ollama show my-model and check the architecture, the quantization, the context length, the capabilities and the template source.
- Check the license on the model page on Hugging Face. A LICENSE line in the Modelfile records the license with the model.
- Import support depends on the Ollama version. Check the import page and the Modelfile reference of the version you run.
Learn more: courses and books (ad)
Where to look if you want to study this topic in more depth. These link to search results; they are not recommendations of a particular course or book.
These are affiliate links (ads). As an Amazon Associate, Toolkitbox AI earns from qualifying purchases.
Sources
- Ollama documentation: Importing a Model
- Ollama documentation: Modelfile Reference
- Hugging Face Hub: Use Ollama with any GGUF Model on Hugging Face Hub
- Hugging Face Hub: Command Line Interface (CLI)
- Hugging Face Hub: GGUF
- ggml: GGUF specification (docs/gguf.md)
- Transformers: Chat templates
- Ollama v0.40.16 source: server/model.go (template detection)
- Ollama v0.40.16 source: server/images.go (template selection)
- Ollama v0.40.16 source: create/create.go (Safetensors folders)
- Ollama v0.40.16 source: create/verify.go (MLX architecture check)
- Ollama v0.40.16: docs/development.md (MLX engine)
Read next
- Store and search Ollama embeddings with pgvectorKeep Ollama embeddings in PostgreSQL with pgvector, search them with HNSW or IVFFlat, and pass the results to /api/chat so that the answer cites its sources.
- Embeddings with Ollama: the basics for search and RAGMake embeddings with Ollama's /api/embed: the models and their sizes, truncate and dimensions, unit-length vectors and cosine similarity, from the official docs.
- Watch the memory of Ollama on a small VPSSee what Ollama holds in memory, how long it stays there and which settings change it: ollama ps, keep_alive and the limits, from the official FAQ.
- Connect Dify to Ollama on your own VPSRun Dify on your own VPS and use Ollama as its model: install, the plugin fields, the Docker address problem and embeddings, from the official documentation.