You can install Python packages without an internet connection by pre-downloading them and their dependencies to a local repository, then pointing pip to that local repository.
Let’s see it in action. Imagine you have a server that needs a specific set of packages, say requests and beautifulsoup4, but this server has no internet access. First, on a machine with internet access, you’ll create a directory to hold your offline packages.
mkdir ~/offline-packages
Now, download the packages and their dependencies into this directory. The --dest flag tells pip where to put them, and --platform and --python-version ensure compatibility with your target offline environment. For example, if your offline server is running Linux with Python 3.9:
pip download --dest ~/offline-packages --platform manylinux2014_x86_64 --python-version 3.9 requests beautifulsoup4
This command will fetch all the necessary wheels (pre-compiled packages) and place them in ~/offline-packages. Now, copy this entire ~/offline-packages directory to your offline server.
On the offline server, you can install these packages using pip, but you need to tell pip to look in your local directory. You can do this by specifying the --no-index flag (to prevent pip from trying to access the internet) and the --find-links flag to point to your local repository.
pip install --no-index --find-links=/path/to/your/offline-packages/ requests beautifulsoup4
The --no-index flag is crucial; it tells pip to only use the packages you provide locally and not even attempt to connect to PyPI. --find-links tells pip where to find those local packages. Pip will then scan the specified directory, find the downloaded wheels for requests, beautifulsoup4, and all their dependencies, and install them.
This approach is invaluable for air-gapped environments, secured networks, or situations where bandwidth is a concern. It ensures reproducible builds because you’re always installing from a known, pre-approved set of package versions. You can even automate the creation of these offline package repositories as part of your CI/CD pipeline.
The real power comes from understanding how pip resolves dependencies. When you run pip download, it doesn’t just grab the top-level packages; it recursively finds all their dependencies. --platform and --python-version are essential for ensuring that the wheels you download are actually compatible with the target system. If you omit these, you might download wheels for a different OS or Python version, leading to installation errors on the offline machine. For instance, downloading a .tar.gz source distribution instead of a wheel might require build tools on the offline machine, which you might not have or want. Always aim to download wheels.
You can also create a full, self-contained pip index within your offline directory. After downloading packages, you can run pip index versions --format json > index.json within your offline-packages directory and then use pip install --no-index --find-links=file:///path/to/your/offline-packages/ --index-url=file:///path/to/your/offline-packages/ to make pip treat your local directory as a full PyPI mirror.
The next hurdle you’ll likely face is managing the security and integrity of these offline package repositories.