Hugging Face Installation and Setup

Hugging Face installation takes a few minutes: create a Python environment, install the libraries with pip, and optionally log in to the Hub with an access token. This lesson walks through each step and explains where downloaded models are stored.

Create a Virtual Environment

Keeping AI libraries in their own environment avoids version conflicts with other projects. Use Python 3.10 or newer.

python -m venv .venv
# Windows
.venv\Scripts\activate
# macOS / Linux
source .venv/bin/activate
(.venv) $

Install transformers and Friends

transformers runs on top of PyTorch, so install torch first. On a machine with an NVIDIA GPU, use the install command from pytorch.org that matches your CUDA version; on CPU or Apple Silicon the plain pip package is fine.

pip install torch
pip install transformers datasets huggingface_hub accelerate
pip install sentence-transformers peft gradio   # used in later lessons
Successfully installed transformers-5.x.x datasets-5.x.x huggingface_hub-2.x.x accelerate-1.x.x ...

Recent major versions of transformers (v5) focus on PyTorch only; TensorFlow and JAX support were removed. If you follow an older blog post that uses TFAutoModel, that code will not work with current releases.

Check the Installation

import transformers, torch
print(transformers.__version__)
print(torch.__version__)
print("CUDA available:", torch.cuda.is_available())
5.22.1
2.x.x
CUDA available: False   (sample output — depends on your machine)

Log In to the Hub

Public models download without an account, but you need a token for gated models (such as some Llama and Gemma releases), private repositories and uploading. Create a token at huggingface.co/settings/tokens, choosing "Read" for downloading or "Write" for uploading. Then log in with the hf command-line tool:

hf auth login
# paste your token when asked

hf auth whoami
user: your-username

The older huggingface-cli command has been replaced by hf. In scripts and CI you can instead set the HF_TOKEN environment variable, which every Hugging Face library reads automatically. Never commit a token to Git.

Where Models Are Cached

  • By default files go to ~/.cache/huggingface/hub (on Windows, C:\Users\<you>\.cache\huggingface\hub).
  • Set HF_HOME to move the whole cache, for example to a bigger drive.
  • Each model is stored once and shared by every project that uses it.
  • Run "hf cache scan" to see what is taking space and "hf cache delete" to clean up.
  • Set HF_HUB_OFFLINE=1 to force libraries to use only cached files.

Frequently asked questions

How do I install Hugging Face transformers?

Install PyTorch, then run pip install transformers in a virtual environment. Add datasets, accelerate and huggingface_hub for the full toolkit used in most tutorials.

Where does Hugging Face store downloaded models?

In ~/.cache/huggingface/hub by default. You can change the location with the HF_HOME environment variable.

Do I need a Hugging Face token?

Only for gated or private models and for uploading. Public models download anonymously, although a token also gives you higher rate limits.

What replaced huggingface-cli login?

The hf command. Use hf auth login to log in and hf auth whoami to check which account is active.