Install libxml2 with Python Modules on Mac



libxml2 is an open-source XML parsing library which is written in C language and provides all the tools needed to read, parse, modify, validate, and write XML and HTML documents.

Installing libxml2

Working with XML data in Python requires parsing libraries. One of the most widely used library for this purpose is libxml2. It is an XML toolkit implemented in C, originally developed for the GNOME project.

Python doesn't include libxml2 bindings by default, the most common way to work with libxml2 is through the lxml package. Installing it on macOS is quite challenging. This article tells you about to install libxml2 with python modules on macOS.

Installing libxml2 via Homebrew

The Homebrew is a package manager that provides a better way to install libxml2 and libxslt(companion library), along with seamless integration with your Python environment. You can install Homebrew using the following commands -

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

While macOS comes with Python pre-installed, using Homebrew's Python version provides better isolation and control -

brew install python

The command above installs the latest Python3 version.

You can install libxml2 using the following commands -

brew install libxml2
brew install libxslt

Just installing libxml2 is not enough, especially when you want to compile Python packages that depend on it. This is because tools like pip rely on environment variables to locate the correct headers and compiled libraries in the building process. These variables act like signs pointing to where Homebrew installed libxml2. You can use the following commands on your terminal session to make the libxml2 headers and libraries discoverable -

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

Install the lxml Python package using the command below -

pip install lxml

After the installation, to verify whether everything works run the command below -

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

This command should print the libxml2 version number without any error.

Once installed, force-link the libraries to make sure that Python and other tools can access them correctly. This can be done through below commands -

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

If you had already installed libxml2 but it had failed or any other error, you can uninstall using the following commands and reinstall similarly by using the above commands -

$ brew unlink libxml2
$ brew uninstall libxml2
$ brew unlink libxslt
$ brew uninstall libxslt

Permission Errors

Suppose if you get the "Permission denied" errors during installation, you can either add --user to your pip command as shown below or use a virtual environment.

pip install --user lxml

Using a Virtual Environment

For better isolation, use a virtual environment. The following commands implement this -

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

Example

Here is a sample example to how o use lxml to parse and manipulate an XML document -

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).decode('utf-8')
print(xml_string)

# Parse XML string
parsed = etree.fromstring(xml_string)
print(parsed.find("child").text)  

The output for the above code is as follows -

<root>
  <child>Hello! from Tutorialspoint!</child>
</root>
Hello! from Tutorialspoint!
Updated on: 2025-04-17T18:44:09+05:30

786 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements