pip
--no-cache-dir
Python package management
caching
software development
What is pip install --no-cache-dir good for?
Codemia Knowledge Hub ·
`pip install --no-cache-dir` tells pip not to read from or write to its local package cache. Its main use is keeping Docker images small, since cached wheels and source archives can add hundreds of megabytes to a container layer. It also helps in CI pipelines that need guaranteed fresh downloads and on machines that are short on disk.
## What pip caches and where
By default, pip caches two things:
1. **HTTP responses** from PyPI (the index server), so it can skip network requests for packages it has already resolved.
2. **Built wheels**, so it does not need to rebuild packages from source on later installs.
| Platform | Default Cache Directory |
|---|---|
| Linux | `~/.cache/pip` |
| macOS | `~/Library/Caches/pip` |
| Windows | `C:\Users\<username>\AppData\Local\pip\Cache` |
Check the cache location and size with:
```bash
pip cache dir
pip cache info
pip cache list
```
Example output:
```
Package index page cache location (pip v23.3+): /home/user/.cache/pip/http-v2
Package index page cache location (older pips): /home/user/.cache/pip/http
Package index page cache size: 338.4 MB
Number of HTTP files: 812
Locally built wheels location: /home/user/.cache/pip/wheels
Locally built wheels size: 3.7 MB
Number of locally built wheels: 47
```
## Smaller Docker images
During `RUN pip install`, the pip cache is written into the container layer. Docker layers are additive, so that cache stays in the final image even though nothing uses it again.
**Without `--no-cache-dir`:**
```dockerfile
FROM python:3.12-slim
COPY requirements.txt .
RUN pip install -r requirements.txt
# Image includes ~/.cache/pip with all downloaded wheels -- wasted space
```
**With `--no-cache-dir`:**
```dockerfile
FROM python:3.12-slim
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
# No cache stored -- image is smaller
```
How much you save depends on your dependencies. For a typical data science stack (numpy, pandas, scikit-learn), the cache can be 200-400 MB.
### BuildKit cache mounts: caching without the size cost
Docker BuildKit supports cache mounts that persist across builds but never end up in the final image:
```dockerfile
FROM python:3.12-slim
COPY requirements.txt .
RUN --mount=type=cache,target=/root/.cache/pip \
pip install -r requirements.txt
```
Later builds reuse the downloaded packages, and the image stays small. If your Docker version supports BuildKit (18.09+), this is generally better than `--no-cache-dir`.
## Fresh downloads in CI/CD
In CI you may want every build to download packages from the index, to catch problems like yanked releases or incompatible new versions:
```yaml
# GitHub Actions example
- name: Install dependencies
run: pip install --no-cache-dir -r requirements.txt
```
Most CI systems have their own caching (GitHub Actions `actions/cache`, GitLab CI cache directives) that is more efficient than pip's built-in cache, so consider those instead.
## Disk-constrained environments
On small VMs, embedded systems or serverless functions, the cache can take up significant space over time:
```bash
# Check current cache size
pip cache info
# Install without caching
pip install --no-cache-dir some-package
# Or purge existing cache
pip cache purge
```
## Bypassing a corrupted cache
If pip behaves oddly (installing wrong versions, failing with hash mismatches), a corrupted cache may be the cause:
```bash
# Quick fix: bypass cache for this install
pip install --no-cache-dir problematic-package
# Permanent fix: purge and rebuild
pip cache purge
pip install problematic-package
```
## When to keep the cache
- **Local development.** The cache saves a lot of time when reinstalling packages or recreating virtual environments. With a warm cache, `pip install -r requirements.txt` takes seconds instead of minutes, while `--no-cache-dir` forces a full network download every time.
- **Repeated builds on the same machine.** On a persistent build server, the cache speeds up every build after the first.
- **Large packages with compiled C extensions.** numpy, scipy and cryptography must be compiled from source if no pre-built wheel is available. A cached built wheel avoids recompiling, which can take several minutes per package.
## Related flags
| Flag | Effect |
|---|---|
| `--no-cache-dir` | Disables both reading and writing cache |
| `--cache-dir /path` | Overrides the default cache directory |
| `pip cache purge` | Deletes all cached files |
| `pip cache remove <pattern>` | Deletes cached files matching a pattern |
| `--force-reinstall` | Reinstalls even if already installed (still uses cache unless `--no-cache-dir` is added) |
| `--no-binary :all:` | Forces building from source (does not affect HTTP cache) |
```bash
# Force fresh download AND rebuild from source
pip install --no-cache-dir --no-binary :all: numpy
# Force reinstall from cache (uses cached wheel if available)
pip install --force-reinstall numpy
# Force reinstall without cache (downloads and builds fresh)
pip install --force-reinstall --no-cache-dir numpy
```
## Making --no-cache-dir the default
To always disable caching (not recommended for development), set it in pip's configuration:
```ini
# pip.conf (Linux/macOS: ~/.config/pip/pip.conf, Windows: %APPDATA%\pip\pip.ini)
[global]
no-cache-dir = true
```
Or use an environment variable:
```bash
export PIP_NO_CACHE_DIR=1
```
## Mistakes to avoid
- **Expecting it to reinstall installed packages.** `--no-cache-dir` does not make pip ignore packages already installed in the environment; pip still skips them. Use `--force-reinstall` for that.
- **Deleting the cache in a separate Docker layer.** If you run `pip install` without `--no-cache-dir` and delete the cache in a later `RUN`, the cache still exists in the earlier layer, because Docker layers are immutable. Use `--no-cache-dir`, or install and clean up in the same `RUN`:
```dockerfile
RUN pip install -r requirements.txt && rm -rf /root/.cache/pip
```
- **Confusing it with `--no-deps`.** `--no-deps` skips installing dependencies and has nothing to do with caching.
- **Ignoring BuildKit cache mounts.** In modern Docker workflows, `--mount=type=cache` is almost always better than `--no-cache-dir`, because it caches across builds without inflating the final image.
Related reading
- What is Remote Method Invocation (RMI)?
- What is SlidingExpiration Timespan in Dache?
- What is the best project structure for a Python application?
- What is the best way to lock cache in asp.net?
- What is pip's equivalent of npm install package --save-dev?
- What is pkg-resources0.0.0 in output of pip freeze command
- What is the biggest Couchbase cluster nodes number?
- What is the correct design for signals in a UML class diagram?
System design interview problems on this topic
- Design a Key Value Store: solution and walkthrough
- Design a Distributed Unique Id Generator: solution and walkthrough
- Design a Distributed Locking System: solution and walkthrough
- Design a Distributed Counter: solution and walkthrough

Course
Beginner
27 lessons
10 hours
System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseSystem Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.