树莓派virtualenv环境下pip install lxml失败:卡顿或报gcc错误
pip install lxml Issues in Raspberry Pi Virtualenv Hey there, let's work through this lxml install problem— I've dealt with this exact headache on my Pi before, so here's what works:
First, Fix the 'arm-linux-gnueabihf-gcc' Error
That error pops up because lxml needs system-level libraries to compile, and they're missing on your Pi. Let's install those first (run these outside your virtualenv, since they're system packages):
sudo apt-get update && sudo apt-get install libxml2-dev libxslt1-dev python3-dev gcc
These packages give gcc all the headers and tools it needs to build lxml successfully.
Fix the "Stuck/Crashing During Install" Issue
Raspberry Pi's CPU is pretty slow, so compiling lxml from source can take ages (it might look like it's frozen, but it's just chugging away). Here are two better ways:
Use Pre-Compiled Wheel Packages (Fastest Option)
PiWheels hosts pre-built lxml wheels for Raspberry Pi, so you don't have to compile anything. Activate your virtualenv first, then run:
pip install --upgrade pip pip install lxml --extra-index-url https://www.piwheels.org/simple
This tells pip to grab the pre-built version instead of compiling from scratch— it should finish in seconds instead of minutes.
Increase Swap Space (If Compiling Is Unavoidable)
If you still need to compile (e.g., PiWheels doesn't have a wheel for your Python version), adding temporary swap space can prevent crashes from running out of memory:
- Create a 2GB swap file:
sudo fallocate -l 2G /swapfile sudo chmod 600 /swapfile sudo mkswap /swapfile sudo swapon /swapfile - Activate your virtualenv and run
pip install lxmlas usual. - After installation, clean up the swap file (swap wears out SD cards over time):
sudo swapoff /swapfile sudo rm /swapfile
Final Check
Once you've tried the above, activate your virtualenv and run python -c "import lxml; print(lxml.__version__)" to confirm it installed correctly.
内容的提问来源于stack exchange,提问作者Akshay

