How to Intermediate · 3 min read

How to run Stable Diffusion locally

Quick answer
To run Stable Diffusion locally, install Python 3.8+, set up a virtual environment, and install the diffusers library from Hugging Face. Use the pre-trained model weights to generate images offline without internet access.

PREREQUISITES

  • Python 3.8+
  • pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu117
  • pip install diffusers transformers scipy
  • A GPU with at least 6GB VRAM (recommended)
  • Git (optional for cloning repos)

Setup

Install Python 3.8 or higher and create a virtual environment. Then install PyTorch with CUDA support (if you have an NVIDIA GPU) and the Hugging Face diffusers library for Stable Diffusion.

bash
python -m venv sd-env
source sd-env/bin/activate  # Linux/macOS
sd-env\Scripts\activate  # Windows
pip install --upgrade pip
pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu117
pip install diffusers transformers scipy

Step by step

Use the following Python script to load the Stable Diffusion model locally and generate an image from a text prompt.

python
import os
from diffusers import StableDiffusionPipeline
import torch

# Load model locally with caching
model_id = "runwayml/stable-diffusion-v1-5"
pipeline = StableDiffusionPipeline.from_pretrained(model_id, torch_dtype=torch.float16)
pipeline = pipeline.to("cuda")

prompt = "A fantasy landscape, vivid colors"
image = pipeline(prompt).images[0]

image.save("output.png")
print("Image saved as output.png")
output
Image saved as output.png

Common variations

  • Use CPU only by changing pipeline.to("cuda") to pipeline.to("cpu"), but expect slower generation.
  • Try different models by changing model_id to other Hugging Face Stable Diffusion variants.
  • Use torch.autocast for mixed precision to optimize VRAM usage.
python
import torch

with torch.autocast("cuda"):
    image = pipeline(prompt).images[0]

Troubleshooting

  • If you get CUDA out of memory errors, reduce batch size or switch to CPU mode.
  • Ensure your GPU drivers and CUDA toolkit are up to date.
  • If model download fails, check your internet connection or manually download weights from Hugging Face.
  • For permission errors, run your terminal as administrator or check file system permissions.

Key Takeaways

  • Use Python 3.8+ and install PyTorch with CUDA for best performance running Stable Diffusion locally.
  • The Hugging Face diffusers library provides easy access to pre-trained Stable Diffusion models for offline use.
  • GPU with at least 6GB VRAM is recommended for smooth image generation; CPU mode is slower but possible.
  • Adjust precision and device settings to optimize memory and speed based on your hardware.
  • Keep your GPU drivers and CUDA toolkit updated to avoid runtime errors.
Verified 2026-04 · runwayml/stable-diffusion-v1-5
Verify ↗