lxml编译为何查找32位库?64位环境安装失败排查
问题
在64位Docker环境中安装lxml==4.6.2时编译失败,提示32位.so文件不兼容,报错日志如下:
gcc -pthread -shared -Wl,-z,relro -Wl,-z,now -g -Wl,-z,relro -Wl,-z,now -g build/temp.linux-x86_64-3.8/src/lxml/etree.o -L/usr/lib64 -L/usr/lib64 -lxslt -lexslt -lxml2 -lrt -lz -lm -o build/lib.linux-x86_64-3.8/lxml/etree.cpython-38-x86_64-linux-gnu.so /usr/bin/ld: skipping incompatible /usr/lib/gcc/x86_64-redhat-linux/8/../../../libxml2.so when searching for -lxml2 /usr/bin/ld: skipping incompatible //lib/libxml2.so when searching for -lxml2 /usr/bin/ld: skipping incompatible //usr/lib/libxml2.so when searching for -lxml2 /usr/bin/ld: cannot find -lxml2 collect2: error: ld returned 1 exit status error: command 'gcc' failed with exit status 1
疑问:64位镜像默认应在/usr/lib64查找库,为何会搜索/usr/lib路径?已尝试设置LD_LIBRARY_PATH但无效。使用的Dockerfile如下:
FROM registry.access.redhat.com/ubi8/ubi-minimal RUN microdnf -y install python38 RUN microdnf -y install shadow-utils RUN microdnf -y install --nodocs \ gcc-c++ \ python38-devel \ python38-wheel \ unixODBC-devel \ bzip2 \ bzip2-devel \ expat \ expat-devel \ gcc \ git \ glibc-langpack-en \ libffi \ libffi-devel \ libxml2 \ libxml2-devel \ libxslt \ libxslt-devel \ unzip \ wget \ yum-utils \ zip \ make \ openssl \ openssl-devel \ sqlite-devel RUN pip3 install --no-binary :all: lxml==4.6.2
解决方案
原因分析
64位系统虽默认优先查找/usr/lib64,但libxml2-devel包安装后可能在/usr/lib下生成了32位库的链接,且编译器默认搜索路径中仍包含/usr/lib,导致链接器尝试加载这些不兼容的32位库,进而报错找不到64位的libxml2。另外LD_LIBRARY_PATH仅影响运行时库查找,对编译时的链接器搜索路径作用有限。
修复步骤
- 强制指定64位库路径给编译器
修改Dockerfile中的pip install指令,通过CFLAGS和LDFLAGS直接指定64位库的位置:
RUN CFLAGS="-I/usr/include/libxml2" LDFLAGS="-L/usr/lib64" pip3 install --no-binary :all: lxml==4.6.2
此操作会让编译器直接去/usr/lib64寻找64位的libxml2库,跳过/usr/lib下的32位文件。
- 清理无效库链接(备选)
若第一种方法无效,可手动删除/usr/lib下的32位libxml2.so链接,重新指向64位库:
RUN rm -f /usr/lib/libxml2.so && ln -s /usr/lib64/libxml2.so /usr/lib/libxml2.so
- 优化Dockerfile(可选)
合并多个RUN指令以减少镜像层数,同时清理安装缓存:
FROM registry.access.redhat.com/ubi8/ubi-minimal RUN microdnf -y install python38 shadow-utils && \ microdnf -y install --nodocs \ gcc-c++ python38-devel python38-wheel unixODBC-devel \ bzip2 bzip2-devel expat expat-devel gcc git glibc-langpack-en \ libffi libffi-devel libxml2 libxml2-devel libxslt libxslt-devel \ unzip wget yum-utils zip make openssl openssl-devel sqlite-devel && \ microdnf clean all && \ CFLAGS="-I/usr/include/libxml2" LDFLAGS="-L/usr/lib64" pip3 install --no-binary :all: lxml==4.6.2
内容的提问来源于stack exchange,提问作者Nishant
相关产品推荐
相关产品推荐

