How to install libxml2 with python modules on Mac?

libxml2 is an open-source XML parsing library written in C language that provides all the tools needed to read, parse, modify, validate, and write XML and HTML documents. Python doesn't include libxml2 bindings by default, so we typically use the lxml package to work with libxml2.

Installing libxml2 via Homebrew

Homebrew is a package manager that provides the best way to install libxml2 and libxslt on macOS. First, install Homebrew if you haven't already ?

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

Install Python via Homebrew for better isolation and control ?

brew install python

Install libxml2 and libxslt libraries ?

brew install libxml2
brew install libxslt

Set environment variables to make libxml2 headers and libraries discoverable during package compilation ?

export LDFLAGS="-L$(brew --prefix libxml2)/lib"
export CPPFLAGS="-I$(brew --prefix libxml2)/include"
export PKG_CONFIG_PATH="$(brew --prefix libxml2)/lib/pkgconfig"

Now install the lxml Python package ?

pip install lxml

Verification

Verify the installation by checking the libxml2 version ?

python -c "from lxml import etree; print(etree.LIBXML_VERSION)"

This should print the libxml2 version number without errors. If needed, force-link the libraries ?

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

Troubleshooting

Permission Errors

If you encounter permission errors, install lxml with the --user flag ?

pip install --user lxml

Using Virtual Environment

For better isolation, use a virtual environment ?

python -m venv myenv
source myenv/bin/activate
# Set environment variables as shown above
pip install lxml

Reinstalling

If installation fails, uninstall and reinstall ?

brew unlink libxml2 libxslt
brew uninstall libxml2 libxslt
# Then reinstall using the steps above

Example

Here's how to use lxml to parse and manipulate XML documents ?

from lxml import etree

# Create a simple XML document
root = etree.Element("root")
child = etree.SubElement(root, "child")
child.text = "Hello from TutorialsPoint!"

# Convert to string
xml_string = etree.tostring(root, pretty_print=True, encoding='unicode')
print(xml_string)

# Parse XML string
parsed = etree.fromstring(xml_string)
print(f"Child text: {parsed.find('child').text}")
<root>
  <child>Hello from TutorialsPoint!</child>
</root>

Child text: Hello from TutorialsPoint!

Conclusion

Installing libxml2 on macOS is straightforward using Homebrew. Set the proper environment variables before installing lxml to ensure successful compilation. Use virtual environments for better project isolation and dependency management.

Updated on: 2026-03-24T17:08:17+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements