How to Set Up an Offline Local PyPI Mirror on a Raspberry Pi
This guide explains how to turn a low‑cost Raspberry Pi into a Wi‑Fi hotspot, DHCP/DNS server, and offline PyPI mirror using minirepo, pypiserver, and nginx, enabling client devices to install Python packages without Internet access.
First, install Raspberry Pi OS on a Raspberry Pi 4 with a 200 GB SD card, then install the required packages:
$ sudo apt install dnsmasq hostapd nginx $ pip install minirepo pypiserver
Configure a static IP for the Wi‑Fi interface by editing /etc/dhcpcd.conf and set up dnsmasq as the DHCP server, defining the address range 192.168.4.2‑192.168.4.30 with a 24‑hour lease.
Create a hostapd configuration file ( /etc/hostapd/hostapd.conf ) with your SSID and passphrase, then point the system to this file by setting DAEMON_CONF="/etc/hostapd/hostapd.conf" in /etc/default/hostapd .
Enable IP forwarding and NAT by editing /etc/sysctl.conf (set net.ipv4.ip_forward=1 ) and adding an iptables rule:
sudo iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
Save the iptables rules with sudo sh -c "iptables-save > /etc/iptables.ipv4.nat" and ensure they are restored on boot via /etc/rc.local .
Use minirepo to clone the desired PyPI packages (e.g., Python 3 source, sdist, bdist_wheel, bdist_egg) and store them in ~/minirepo . Install minirepo with pip install minirepo and configure its JSON file as needed.
Start the local PyPI server with pypi-server -p 8080 ~/minirepo & , which serves the cloned packages to clients.
Set up nginx as a reverse proxy and cache by creating a virtual host configuration (e.g., /etc/nginx/sites-available/cheeseshop.com ) that forwards port 80 requests to the pypiserver on port 8080 and defines a 10 GB cache.
Enable the site with a symbolic link in /etc/nginx/sites-enabled/ , then restart nginx and dnsmasq services.
Configure DNS resolution for the custom domain by adding an entry to /etc/hosts mapping cheeseshop.com to the Pi’s static IP, and restart dnsmasq .
Create a Systemd service to start pypiserver on boot: write a start script (e.g., /usr/bin/start-pypi-server.sh ), make it executable, then create /etc/systemd/system/pypiserver.service with ExecStart=/usr/bin/start-pypi-server.sh . Enable and start the service with sudo systemctl enable pypiserver and sudo systemctl start pypiserver .
Clients can now install packages from the offline mirror using pip with the --index-url and --trusted-host options, for example:
pip install --trusted-host cheeseshop.com --index-url http://cheeseshop.com/simple/ django
The article concludes that the Raspberry Pi now functions as an offline PyPI repository, providing a low‑cost solution for environments without Internet access.
Python Programming Learning Circle
A global community of Chinese Python developers offering technical articles, columns, original video tutorials, and problem sets. Topics include web full‑stack development, web scraping, data analysis, natural language processing, image processing, machine learning, automated testing, DevOps automation, and big data.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.