Install Python Libraries in an air-gapped environment
A customer asked how can I install Python Libraries in an air-gapped environment that got me thinking about it and led to this article.
It’s straightforward, well not really you can need to understand “wheels” but I will not cover that at all, just the How-To, if you want to learn more about “wheels” check out this great article from RealPython.com
Let’s get started, I’m using an Ubuntu Server, I created a “virtual environment” using the following:
sudo python3 -m venv test
cd test
source bin/activate
For this example, I’m going to use the “ansible” package:
sudo bin/pip3 install ansible
Once the installation is complete we are going to create a “requirements.txt” file
sudo test/bin/pip3 freeze > requirements.txt
Now let’s create a folder called “wheel”
mkdir wheel
This will download all the wheel files that are needed so we can use them offline
sudo test/bin/pip3 download -r requirements.txt -d wheel
ls wheel
ansible-5.0.1.tar.gz
ansible-core-2.12.1.tar.gz
cffi-1.15.0-cp39-cp39-manylinux_2_12_x86_64.manylinux2010_x86_64.whl
cryptography-36.0.0-cp36-abi3-manylinux_2_24_x86_64.whl
Jinja2-3.0.3-py3-none-any.whl
MarkupSafe-2.0.1-cp39-cp39-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_12_x86_64.manylinux2010_x86_64.whl
packaging-21.3-py3-none-any.whl
pycparser-2.21-py2.py3-none-any.whl
pyparsing-3.0.6-py3-none-any.whl
PyYAML-6.0-cp39-cp39-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_12_x86_64.manylinux2010_x86_64.whl
resolvelib-0.5.4-py2.py3-none-any.whl/
Now we move the requirements.txt to the wheel folder
mv requirements.txt wheel
Now we zip the folder using the following commands
tar -zcf wheelhouse.tar.gz wheel
Transfer the files to the air-gapped environment and then unzip the files
tar -zxf wheelhouse.tar.gz
Now we install the Python libraries
pip3 install -r wheel/requirements.txt --no-index --find-links wheel
Pretty straightforward, I hope you find it useful.
As always... Sif dropping great useful information!