pip installs an older cached package version when an offline/local cache (--find-links), pinned constraint file, outdated index metadata, or a locally built wheel in pip’s cache directory matches your requirement specifier (or when the newly released version lacks a compatible wheel for your current Python/OS version)—you can bypass the cache and force a fresh index lookup and download with python -m pip install --no-cache-dir --upgrade <package> (or python -m pip cache purge to clear stale cached wheels permanently).
Basic Example
# 1. Recommended: Bypass pip's HTTP & wheel cache and upgrade to the latest release
python -m pip install --no-cache-dir --upgrade requests
# 2. Force a clean reinstall of the exact target version even if already installed:
python -m pip install --no-cache-dir --force-reinstall requests==2.32.3
# 3. Inspect or clear pip's persistent cache directory permanently:
python -m pip cache info
python -m pip cache purge
Key Details & Edge Cases
- Why
pipselects an older version even without--no-cache-dir:- No specifier forcing an upgrade (
pip install pkgvs.pip install --upgrade pkg): If an older version ofpkgis already installed in the active environment and satisfies the requirement,pip install pkgexits immediately withRequirement already satisfiedwithout checking PyPI for newer versions. - Python version or platform incompatibility (
Requires-Python): Newer releases often drop support for older Python versions (e.g., Python 3.8/3.9) or lack prebuilt wheels for your OS/architecture.pipsilently backtracks to the newest historical version whose metadata matches your interpreter (python --version). Pass--only-binary=:all:or run with-v(python -m pip install -v --upgrade pkg) to see whypipskipped newer candidate releases. - Custom index / mirror lag or
--find-links: Private PyPI mirrors (Nexus, Artifactory, DevPI) orPIP_FIND_LINKSenvironment variables can serve stale index metadata or local wheels ahead of PyPI. - Pre-release versions (
--pre): By default,pipignores alpha, beta, and release-candidate tags (a,b,rc) unless you pass--pre.
- No specifier forcing an upgrade (
--no-cache-dirvs.--force-reinstallvs.--ignore-installed(-I):--no-cache-dir: Disables both reading from and writing topip’s disk cache (~/.cache/pipon Linux,~/Library/Caches/pipon macOS,LocalAppData\pip\Cacheon Windows), forcing fresh network fetches and fresh wheel builds from source (sdist).--force-reinstall: Reinstalls the target package (and its dependencies unless--no-depsis added) even if the same version is already installed insite-packages.
- Setting
--no-cache-dirglobally in Dockerfiles or CI:- In Docker images, set
ENV PIP_NO_CACHE_DIR=1(or pass--no-cache-dir) so downloaded wheels do not bloat image layers while ensuring every build pulls fresh artifacts.
- In Docker images, set
- Clearing only one corrupt/stale package from the cache (
pip cache remove):- Instead of wiping your entire cache with
pip cache purge, remove cached wheels for a single package viapython -m pip cache remove <package_pattern>(e.g.,python -m pip cache remove numpy).
- Instead of wiping your entire cache with
Comparison of pip Cache-Bypass & Upgrade Flags
| Command / Flag | Bypasses Disk Cache? | Queries Index for Newer Versions? | Reinstalls if Same Version Already Present? | Best Used When |
|---|---|---|---|---|
python -m pip install --no-cache-dir --upgrade <pkg> (Recommended) |
Yes | Yes | No (upgrades only if newer exists) | Pulling the latest release cleanly without stale cached wheels or HTTP responses. |
python -m pip install --no-cache-dir --force-reinstall --no-deps <pkg> |
Yes | Yes | Yes (target package only) | Replacing a corrupted or locally modified installation of <pkg> without churning dependencies. |
python -m pip cache purge (or cache remove <pkg>) |
Yes (deletes cached files) | N/A (cache management) | N/A | Freeing disk space or permanently clearing corrupted locally built wheels. |
python -m pip install -v --upgrade <pkg> |
No (unless paired with --no-cache-dir) |
Yes | No | Debugging why pip’s resolver skipped a newer version (e.g., Requires-Python mismatch). |
You must log in or register to comment.
