Install Python (PIP) packages without Internet

Python comes with simple and powerful package manager (PIP). While setting up any environment we use pip to install python dependencies required for application. PIP by default download and install packages from internet (pypi).

Installing python packages with internet access is straight forward you need to use pip install command, it will download package from pypi, build it and install for that particular machine.

Packages from pypi can be downloaded as wheel files and can be installed offline on machines having no internet access, just make sure to download wheel files for your machine architecture (Windows, Linux).

When we want to install package on production environment or where internet access is restricted then we have two options:

  1. Download and transfer required packages
  2. Setup local PIP repository

Download and transfer packages

You can directly download required packages to some directory on machine which is having internet access and transfer these packages to production machine and install required packages from directory with all dependencies.

For downloading python packages you need python package manager PIP. It will get installed with python-pip utility, you can check handy pip commands to get started with it.

Make sure that you download packages on environment with similar architecture as of target.

  • Download all required packages with dependencies using PIP
# Download wheel packages for django and it's dependencies
python -m pip wheel --wheel-dir <DIRECTORY> django

# Download all packages in requirements.txt file
python -m pip wheel --wheel-dir <DIRECTORY> -r requirements.txt
  • Transfer directory to target machine and install required packages from it using --find-links and --no-index option:
# Install Django Package
python -m pip install --no-index --find-links=<DIRECTORY> django

# Install all packages from requirements.txt file
python -m pip install --no-index --find-links=<DIRECTORY> -r requirements.txt

Setup Local PIP repository

For enterprises there may be need to setup and use local repository for their internal development as well as for maintaining external packages without internet access.

For setting up local repo you can use any artifactory such as Jfrog/Nexus and upload all required packages to the artifactory. Once uploaded you can use same artifactory all over your organization by configuring index url as artifactory url.

You can setup index-url in /etc/pip.conf  file which is global configuration file for all users on the machine. Also XDG_CONFIG_DIRS environment variable can be used to find pip.conf file.

index-url = https://<your artifactory url>

Once index-url is set all PIP packages can be deployed from this artifactory.

Suhas Adhav
In love with web3, DApps and Blockchain Technology | DevOps Expert | Kubernetes | Docker | Jenkins | Cloud | Hadoop
The Internet