Create a local Python pip repository

Oct 1, 2019 by Alexandre - 268 views

https://cylab.be/blog/39/create-a-local-python-pip-repository

Python is a very interesting programming language because it is possible to prototype a lot of things in a few minutes. The Python Package manager, called pip, is a very simple tool that allows the user to import/install easily any Python package and its dependencies.

Sometimes, it is necessary to use Python on an offline computer. Int this situation, it is not possible to install Python packages from the official repository (pypi.org).

To avoid this problem, it is possible to install your own repository on any machine.

Install Apache on an offline computer

The first step to set up a local Python repository is to install an Apache server.

If it is possible to install Apache with an Internet connection, use simply:

sudo apt-get install apache2

If it is not allowed or possible to connect the computer on the Internet, you have to install apache manually. The following script download all needed dependencies:

#!/bin/sh
wget -c http://nl.archive.ubuntu.com/ubuntu/pool/main/a/apr/libapr1_1.5.2-3_amd64.deb
wget -c http://nl.archive.ubuntu.com/ubuntu/pool/main/a/apr-util/libaprutil1_1.5.4-1build1_amd64.deb
wget -c http://nl.archive.ubuntu.com/ubuntu/pool/main/a/apr-util/libaprutil1-dbd-sqlite3_1.5.4-1build1_amd64.deb
wget -c http://nl.archive.ubuntu.com/ubuntu/pool/main/a/apr-util/libaprutil1-ldap_1.5.4-1build1_amd64.deb
wget -c http://nl.archive.ubuntu.com/ubuntu/pool/main/l/lua5.1/liblua5.1-0_5.1.5-8ubuntu1_amd64.deb
wget -c http://nl.archive.ubuntu.com/ubuntu/pool/main/a/apache2/apache2-bin_2.4.18-2ubuntu3.13_amd64.deb
wget -c http://nl.archive.ubuntu.com/ubuntu/pool/main/a/apache2/apache2-utils_2.4.18-2ubuntu3.13_amd64.deb
wget -c http://nl.archive.ubuntu.com/ubuntu/pool/main/a/apache2/apache2-data_2.4.18-2ubuntu3.13_all.deb
wget -c http://nl.archive.ubuntu.com/ubuntu/pool/main/a/apache2/apache2_2.4.18-2ubuntu3.13_amd64.deb

Copy them on the offline computer and install them manually.

Create local pip repository

If you need only some specific dependencies, pypi-mirror (https://pypi.org/project/python-pypi-mirror/) is the perfect tool. Install, on an online computer, pypi-mirror (works only with pip3):

pip install python-pypi-mirror

Then, run the two following commands for each your required dependencies:

pypi-mirror -d path/to/folder download package
pypi-mirror -d path/to/folder create -m simple

The first command will download the Python package and its dependencies on the folder path/to/folder. The second command will create an index of the dependencies and store it in the simple folder.

For a large number of dependencies, it could be interesting to use a script to read and to download all dependencies from a requirements.txt file.

Copy the path/to/folder and simple folders in /var/www/html on the offline machine. The local repository is ready!

Use your local repository

To use your new local repository on this offline machine (or any machine connected on the same local network), use the following command:

pip install -i http://ip.of.srv/simple --trusted-host ip.of.srv package

Of course,pip must be installed on the offline computer on which you want to install the package. It is possible to do it manually (as for the Apache server), or, longer but easier, use a local apt-mirror as described: https://cylab.be/blog/38/create-a-local-apt-mirror-for-offline-usage.