Code Beginner easy · 5 min

Installing transformers and torch

What you will learn
Set up the transformers library and PyTorch on your machine so you can load and run pre-trained models.

Why this matters

You can't run any model without the right dependencies installed. A broken installation will fail at import time, wasting debugging time. Getting this right first prevents cascading errors later.

Skip if: You don't need to install locally if you're running in Colab, Hugging Face Spaces, or a pre-built Docker container: those environments come with transformers and torch pre-installed.

Explanation

What it is: Installing transformers (the Hugging Face library for working with models) and torch (the deep learning framework transformers depends on). In transformers 5.5.x, the library has major breaking changes from 4.x that affect how models are loaded and quantized.

How it works mechanically: pip install downloads packages from PyPI and installs them into your Python environment. The transformers library depends on torch, but you install torch separately to control which version and which compute backend (CPU, CUDA, Metal) you get. Without explicitly specifying versions, you risk incompatible combinations.

When to use it: Always do this first when setting up a new machine or virtual environment for transformer work. Use a virtual environment (venv or conda) to isolate dependencies and avoid conflicts with other projects.

Analogy

It's like installing a kitchen: torch is the electrical wiring and appliances, transformers is the cookware and recipes. You need to install the wiring before the cookware will work properly.

Code

python
import subprocess
import sys

subprocess.check_call([sys.executable, '-m', 'pip', 'install', '--upgrade', 'pip'])
subprocess.check_call([sys.executable, '-m', 'pip', 'install', 'torch', 'torchvision', 'torchaudio'])
subprocess.check_call([sys.executable, '-m', 'pip', 'install', 'transformers>=5.5.0'])

import torch
import transformers

print(f'PyTorch version: {torch.__version__}')
print(f'Transformers version: {transformers.__version__}')
print(f'CUDA available: {torch.cuda.is_available()}')
print(f'Device: {torch.device("cuda" if torch.cuda.is_available() else "cpu")}')
Output
PyTorch version: 2.4.0
Transformers version: 5.5.0
CUDA available: False
Device: cpu

What just happened?

The code ran pip install three times: first upgrading pip itself, then installing torch and related packages, then installing transformers at version 5.5.0 or higher. Then it imported both libraries and printed their versions along with a check of whether CUDA (GPU support) is available on this machine. The output shows both libraries imported successfully and CUDA is not available on this system (so it defaults to CPU).

Common gotcha

Installing transformers without explicitly pinning the version (e.g., pip install transformers with no version spec) can give you a very old 4.x version or a brand-new 5.x version with breaking changes. Always pin to transformers>=5.5.0 for this course. Also, torch installation is OS and hardware-specific: the command above works for CPU. For CUDA 12.1, you need: pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu121

Error recovery

ModuleNotFoundError: No module named 'torch'
You installed transformers but not torch. Run: pip install torch. Torch must be installed before or alongside transformers.
RuntimeError: CUDA out of memory
You have CUDA but insufficient GPU memory. This happens after successful installation when you load a model. For now, either use a smaller model or add device_map='auto' when loading (covered in later lessons).
ImportError: cannot import name 'AutoTokenizer' from transformers
You have transformers 4.x installed. The 5.5.x API is different. Run: pip install --upgrade 'transformers>=5.5.0'
No module named 'pip'
Python's pip is broken or missing. Run: python -m ensurepip --upgrade

Experienced dev note

In transformers 4.x, you could load a model with AutoModel.from_pretrained(name) and it would work on CPU. In 5.5.x, that same code will often cause OOM errors or slower inference. Always add device_map='auto' and torch_dtype=torch.bfloat16 from the start: it's not optional for production. Also: use virtual environments religiously. Installing torch globally has burned every developer once.

Check your understanding

You have transformers 4.30 installed, but this course requires 5.5+. What single pip command will upgrade just transformers without touching torch?

Show answer hint

A correct answer uses pip install with a version specifier (>=, ==, or similar) and targets only the transformers package. The answer should explain why you don't reinstall torch: because pip is smart enough to skip already-installed dependencies unless forced.

VERSION transformers 5.5.0 (April 2026) introduced breaking changes to model loading and quantization APIs compared to 4.x. The from_pretrained() method now requires explicit device_map and torch_dtype arguments for production use. Code written for 4.x will import but may fail at runtime. If you see errors about missing device_map, you have an old version.
NEXT

Now that you have the libraries installed, load your first pre-trained model using AutoModel.from_pretrained() with the correct device_map parameter.

Community Notes

No notes yetBe the first to share a version-specific fix or tip.