Skip to main content

How to Use Hugging Face Models Offline with HF_HUB_OFFLINE

· 5 min read
Jagdish Kumawat
Founder @ Dewiride

Even when a model is already downloaded, Hugging Face libraries normally contact the Hub to check for a newer version. On a plane, behind a strict firewall or on a server with no internet, that check fails or waits. Setting HF_HUB_OFFLINE=1 tells them to use only what's in the cache. This guide shows both sides with huggingface_hub 2.5.0: a cached file loads, a missing one fails.

Syntax​

Syntax
HF_HUB_OFFLINE=1 <command>
PartWhat it means
HF_HUB_OFFLINE=1Turns offline mode on: no calls to the Hub
<command>The program to run offline, such as uv run hf download … or uv run python train.py

The environment variable reference says: "If set, no HTTP calls will be made to the Hugging Face Hub. If you try to download files, only the cached files will be accessed. If no cache file is detected, an error is raised". It also speeds things up: normally, "even if the latest version of a file is cached, calling hf_hub_download still triggers a HTTP request to check that a new version is not available. Setting HF_HUB_OFFLINE=1 will skip this call".

The Example​

A project downloads one file while online, then asks for the same file and for a second, never-downloaded one with offline mode on.

Prerequisites​

  • uv. This guide uses uv 0.13.10 with Python 3.15.2.

Step 1: Create a Project and Download a File Online​

Terminal
uv init --python 3.15 offline-demo
cd offline-demo
uv add huggingface_hub
uv run hf download google-bert/bert-base-uncased config.json
Output (of the download)
Warning: You are sending unauthenticated requests to the HF Hub. Please set a HF_TOKEN to enable higher rate limits and faster downloads.
config.json: 100%|█████████████████████████████████████████████████| 570/570 [00:00<00:00, 4.25MB/s]
✓ Downloaded
path: /Users/you/.cache/huggingface/hub/models--google-bert--bert-base-uncased/snapshots/86b5e0934494bd15c9632b12f734a8a67f723594/config.json

The terminal after uv run hf download google-bert/bert-base-uncased config.json while online: the download and the path of config.json in the cache.

config.json is now in the cache.

Step 2: Ask for the Same File Offline​

In bash and zsh, putting the variable in front of the command sets it for that one command. In PowerShell, set it first; it then stays on for that terminal:

Terminal
HF_HUB_OFFLINE=1 uv run hf download google-bert/bert-base-uncased config.json
Output
✓ Downloaded
path: /Users/you/.cache/huggingface/hub/models--google-bert--bert-base-uncased/snapshots/86b5e0934494bd15c9632b12f734a8a67f723594/config.json

The terminal after the offline download: Downloaded and the same path, with no warning and no progress bar.

The file came straight from the cache. There's no progress bar, and no warning from the Hub either, because the Hub was never contacted. The PowerShell version printed the same.

Step 3: Ask for a File That Isn't Cached​

tokenizer.json was never downloaded:

Terminal
HF_HUB_OFFLINE=1 uv run hf download google-bert/bert-base-uncased tokenizer.json
Output
Error: Local entry not found. Cannot reach https://huggingface.co/google-bert/bert-base-uncased/resolve/main/tokenizer.json: offline mode is enabled. To disable it, please unset the `HF_HUB_OFFLINE` environment variable.
Hint: set HF_DEBUG=1 as environment variable for full traceback.

The terminal after asking for tokenizer.json offline: Error: Local entry not found. Cannot reach https://huggingface.co/google-bert/bert-base-uncased/resolve/main/tokenizer.json: offline mode is enabled.

Offline mode refused to download it and said exactly why. To get it, turn offline mode off and download it once.

Turn Offline Mode Off Again​

In bash and zsh, a variable put in front of one command is already gone. If you used export HF_HUB_OFFLINE=1, remove it with unset HF_HUB_OFFLINE. In PowerShell, remove it with:

PowerShell
Remove-Item Env:HF_HUB_OFFLINE

Common Mistakes​

MistakeWhat happensDo this instead
Going offline before the files are downloadedError: Local entry not found. Cannot reach …: offline mode is enabled.Run the program once online, so everything it needs is cached
Leaving offline mode on after a tripNew models and updates never downloadunset HF_HUB_OFFLINE or Remove-Item Env:HF_HUB_OFFLINE
Setting it inside a Python program after the library is importedThe library has already read the settingSet it before the program starts

FAQ​

Does this work with Transformers models too?​

Yes. Transformers downloads through huggingface_hub, and its installation guide says: "Set the environment variable HF_HUB_OFFLINE=1 to prevent HTTP calls to the Hub when loading a model."

Is there a way to go offline for one model only?​

In Transformers, pass local_files_only=True to from_pretrained, which the same guide describes as "Another option for only loading cached files".

Conclusion​

With HF_HUB_OFFLINE=1, the cached config.json loaded without contacting the Hub, and the never-downloaded tokenizer.json failed with "offline mode is enabled". Download once online, then run offline as often as you like.

Additional Resources​