在linux上安装TensorFlow(解决error while loading shared libraries问题)

在linux上安装TensorFlow(解决error while loading shared libraries问题)(无root解决GLIBC_x.xx not found)

python3运行后import tensorflow,报错
ImportError: /lib64/libc.so.6: version GLIBC_2.17 not found (required by /data2/myname/anaconda2/envs/tensorflow/lib/python3.5/site-packages/tensorflow/python/\_pywrap_tensorflow_internal.so)
系统版本/lib64/libc.so.6包含最新为GLIBC_2.12,新装gcc-6.2.0中的libstdc++.so.6也仅包含GLIBC_2.14。
下载glibc-2.28后,./configure报错,日志中含有
gcc: /lib64/libc.so.6: version GLIBC_2.14' not found (required by gcc)
。这个gcc是自己装的6.2。若用系统gcc,则说版本不够。因此委曲求全,下载glibc-2.17。
采用系统本身gcc进行编译,能够正常完成编译,但将lib添加至LD_LIBRARY_PATH后结果导致任何一个命令都无法运行,如ls: error while loading shared libraries: __vdso_time: invalid mode for dlopen(): Invalid argument
但是通过.so直接调用python的绝对路径,进入python后可实现TensorFlow的import。(/data2/myname/glibc-2.17/makehere/lib/ld-2.17.so /data2/myname/anaconda2/envs/tensorflow/bin/python)
看起来必需要用到root权限才行,因此暂时放弃。
stackoverflow中有人提问如何设置多个glibc环境,第一名提到需要在进行某需要高版本的glibc的程序进行编译时就将glibc环境作为参数加入:

It is very possible to have multiple versions of glibc on the same system (we do that every day).

However, you need to know that glibc consists of many pieces (200+ shared libraries) which all must match. One of the pieces is ld-linux.so.2, and it must match libc.so.6, or you’ll see the errors you are seeing.

The absolute path to ld-linux.so.2 is hard-coded into the executable at link time, and can not be easily changed after the link is done.

To build an executable that will work with the new glibc, do this:

g++ main.o -o myapp …
-Wl,–rpath=/path/to/newglibc
-Wl,–dynamic-linker=/path/to/newglibc/ld-linux.so.2

The -rpath linker option will make the runtime loader search for libraries in /path/to/newglibc (so you wouldn’t have to set LD_LIBRARY_PATH before running it), and the -dynamic-linker option will “bake” path to correct ld-linux.so.2 into the application.

If you can’t relink the myapp application (e.g. because it is a third-party binary), not all is lost, but it gets trickier. One solution is to set a proper chroot environment for it. Another possibility is to use rtldi and a binary editor.

重装TensorFlow可能会涉及到重装anaconda,过于复杂,所以今后沿用/data2/myname/glibc-2.17/makehere/lib/ld-2.17.so /data2/myname/anaconda2/envs/tensorflow/bin/python方式来打开TensorFlow。

不想这么麻烦,继续研究的方案

patchelf
是linux自带程序,可以为二进制文件改变rpath(library path)和解释器(interpreter)。用法如下:
patchelf --set-interpreter /data2/myname/tools/glibc-2.17/makehere/lib/ld-2.17.so --set-rpath /data2/myname/tools/glibc-2.17/makehere/lib:/data2/myname/anaconda2/envs/tensorflow/lib/:$LD_LIBRARY_PATH /data2/myname/anaconda2/envs/tensorflow/bin/python
其中set-interpreter是设置解释器,解释器是什么?实际上你用这个so文件为前缀就可以运行任何二进制文件了。set-rpath类似于设置LD_LIBRARY_PATH,只不过不是全局变量。
这个答案是与上一个答案都在stackoverflow上,上次只看了最多赞回答,失之交臂,pity。
但是patchelf后的python就不太正常了,进入python命令行后backspace、delete、上下左右等就无法正常使用,比如backspace需要摁住ctrl才行。有方案可以解决,但是貌似要root,如果需要的话以后再解决。

需要sh结尾的安装包

复制/bin/sh到自己的文件夹后,自行patchelf,用这个sh进行安装。如果遇到有x权限且头上标明了运行bash路径的sh,要么修改它的头,要么。。。不怕死而且有root的话可以直接修改系统本身bash和sh的运行环境。

#-----------------
linux真是复杂,想想他们是怎么编出来的,厉害!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值