Explain Codes LogoExplain Codes Logo

Cannot install Lxml on Mac OS X 10.9

python
virtual-environments
installation-troubleshooting
best-practices
Anton ShumikhinbyAnton Shumikhin·Nov 19, 2024
TLDR

Install lxml on Mac OS X 10.9 with these quick steps. Firstly, ensure Xcode and Command Line Tools are installed. Use Homebrew for dependencies installation:

brew install libxml2 libxslt

Next, proceed with lxml installation adjusting environmental variables to set specific library paths:

STATIC_DEPS=true LIBXML2_LIBS=$(brew --prefix libxml2)/lib LIBXSLT_LIBS=$(brew --prefix libxslt)/lib pip install lxml

With these settings, you're good to go. Now let's dive in deeper.

Preparation: Xcode and Command Tools

Before an adventurous lxml ride, let's gear up with Xcode Command Line Tools. Run:

xcode-select --install

Oh, no! Paths messed up? Clean the slate with a reset:

sudo xcode-select --reset

Troubleshooting: Permission and Linking Issues

Came across permission issues? Point these files to your user account. Homebrew directories are like dogs, they just need to know who's boss:

sudo chown -R $(whoami):admin /usr/local

Next, let's link libraries. Forcibly, if brew link whines about being shy:

brew link libxml2 --force brew link libxslt --force

See, what Homebrew needs is a little encouragement sometimes.

Let's Get Isolated: The Magic of Virtual Environments

Isolation is not just for pandemic times, your Python virtual environments need it too. Encourage social distancing among your dependencies with:

python -m venv venv source venv/bin/activate pip install lxml

Always remember: In virtual environments, we avoid sudo like spoilers.

Temporary plaque for stubborn problems

Stumbling upon the perennial 'libxml/xmlversion.h' file not found issue? Time for a temporary fix:

ln -s $(xcode-select -p)/include/libxml2/libxml /usr/local/include/libxml

Remember, duct tape fixes everything, but check Homebrew's docs for a sustainable solution.

Advancing to expert mode

For those hard-to-please installation issues, meet your advanced toolkit:

Static Dependencies Installation

Use this to always stay linked, like childhood friends:

STATIC_DEPS=true pip install lxml

Set Paths Manually

Dismiss your GPS and set paths manually for libxml2:

CPATH="/usr/include/libxml2" # You should not copy-paste from the internet... except this time.

Post-Xcode Update

Ensure handyman Developer Tools are still around post upgrade:

xcode-select --install

Just like your ex, Xcode too might need some confirming after an update.

The Next Level: Advanced lxml Installs

Alternative Install: MacPorts

Not a Homebrew fan? Dunk your MacPorts in and brew lxml:

sudo port install py39-lxml # MacPorts users do it in the port.

Best Practices Adoption

Embrace conducting yourself better. Use virtualenv and avoid sudo for a cleaner, problem-free virtual life.

Update Checks

Updates are not just for bragging about console apps, they're remedial too. Keep checking.