setup.py error
error: command 'gcc' failed with exit code 1
Traceback
Traceback (most recent call last):
File "main.py", line 2, in <module>
# error: command 'gcc' failed: libxml/xpath.h: No such file
error: command 'gcc' failed with exit code 1What causes this error
The setup.py build process failed, usually due to C/C++ compilation errors. Missing headers, libraries, or compilers are the typical root causes.
How to fix it
Install system development packages for required libraries. Check the full error output for the specific missing header or library. Set CFLAGS/LDFLAGS if libraries are in non-standard locations. Use binary alternatives.
Code that causes this error
pip install lxml # error: command 'gcc' failed: libxml/xpath.h: No such file
Fixed code
# Install the required development libraries: # Ubuntu/Debian: # sudo apt install libxml2-dev libxslt-dev python3-dev # macOS: # brew install libxml2 libxslt pip install lxml
About setup.py error
This error occurs during the setup.py execution phase of package installation, typically when compiling C extensions fails. The setup.py script runs the build process, and if the C compiler encounters errors — missing headers, incompatible library versions, or compiler bugs — the installation fails with compiler-specific error messages. The error output often contains the exact compiler command that failed and the compiler's error messages.
Common fixes include installing development packages for the required libraries, setting environment variables like `CFLAGS`, `LDFLAGS`, and `LIBRARY_PATH`, and ensuring the correct compiler version is installed. For packages that frequently cause build issues, check if a binary wheel or a '-binary' variant is available. The Python community is gradually moving away from setup.py to pyproject.toml with modern build backends that handle compilation more robustly.
Common scenarios
Running scripts without sufficient file system permissions
Connecting to servers that are not running or are unreachable
Installing Python packages without proper environment setup
Running out of disk space or file descriptors during I/O operations