Last modified: May 28, 2025 By Alexander Williams

Install Python Package in Alpine Linux

Alpine Linux is a lightweight OS popular for Docker containers. Installing Python packages here requires extra steps.

Prerequisites

Before installing Python packages, ensure Alpine Linux is updated. Run:


apk update && apk upgrade

This updates the package list and upgrades existing packages.

Install Python

Alpine Linux often uses Python 3 by default. Install it with:


apk add python3 py3-pip

This installs Python 3 and pip, the package installer.

Install Python Packages

Use pip3 to install packages. For example, install requests:


pip3 install requests

This downloads and installs the requests package.

Common Issues and Fixes

Some packages need build tools. Install them first:


apk add build-base python3-dev

This adds compilers and Python headers for building packages.

Using Virtual Environments

Virtual environments keep projects isolated. Create one with:


python3 -m venv myenv
source myenv/bin/activate

Now, pip installs packages only in this environment.

Installing Specific Versions

Specify package versions with pip3:


pip3 install package==1.2.3

This installs version 1.2.3 of the package.

Alternative Methods

For complex setups, consider Docker or Poetry.

Conclusion

Installing Python packages in Alpine Linux is straightforward. Ensure dependencies are met and use pip3 for installation.