How to fix error with xml2-config not found when installing PHP from sources?
When you attempt to build PHP from source on a Linux distribution like Ubuntu, the ./configure script acts as a dependency checker before compilation even begins. If your system lacks specific development libraries, the script halts immediately with an error message like configure: error: xml2-config not found. This isn't just a missing executable; it indicates that the build system cannot locate the XML processing tools required to compile the PHP core, specifically for modules like dom or simplexml.
The root cause is almost always a missing development package. In Debian-based systems, you can find these libraries in the -dev repository section. To resolve this, you need to install libxml2-dev, which provides the header files and the xml2-config binary itself. Run the following command in your terminal:
sudo apt-get update
sudo apt-get install libxml2-dev
Once installed, re-run the ./configure script. The error should vanish, allowing the process to proceed to checking other dependencies like zlib, curl, or mbstring.
However, relying solely on system packages isn't always the final step. Sometimes the binary exists, but the configure script cannot find it because of an unconfigured environment variable. If you have installed libxml2-dev manually via dpkg rather than through the package manager, or if you are working in a container with a custom path, the system might not know where to look. In such cases, the configure script expects the binary to be at /usr/bin/xml2-config. If your installation placed it elsewhere, you must explicitly tell the build system where to find it using the --with-libxml-dir or by setting the XML2_CONFIG environment variable.
Another common pitfall involves version mismatches. The error message might say "not found," but technically the library is present in a different version than what PHP expects. For example, if you are compiling an older PHP 5.3 stable release as mentioned in the source material, your system's libxml2 might be too new (e.g., libxml2-2.9.x) for the legacy PHP build. While modern PHP versions usually handle this gracefully by ignoring minor version differences or requiring a specific minimum, older builds can fail if the API has changed significantly.
If you encounter issues where the library is present but configure still fails to detect it, verify the location of the binary directly:
which xml2-config
xml2-config --version
If these commands return nothing or a version number that seems incompatible, ensure your LD_LIBRARY_PATH or system PATH includes the directory containing the executable. Often, the issue resolves itself simply by running update-alternatives to switch to the correct symlink if multiple versions are installed.
It is also worth noting that building PHP from source is rarely necessary for standard web hosting environments today. Most modern applications, including Laravel projects, rely on pre-compiled binaries provided by your package manager or a dedicated build tool like Homebrew on macOS or Docker containers. Unless you need to patch the source code or support an extremely old legacy system, installing PHP via apt-get install php is generally more stable and secure, as it ensures all dependencies are resolved against the specific Ubuntu version you are running.
If you must compile from source for a custom configuration, remember that the configure script can be verbose. It will list every dependency it finds or misses. Pay attention to any other "not found" messages after fixing xml2-config, as they will likely be similar issues with other libraries like jpeg or freetype. Addressing them one by one ensures a clean build without unexpected runtime errors later.
