在 Ubuntu 12.04 中导入 Python 2.7 中的 Tensorflow 时出错。 'GLIBC_2.17 not found'

Error while importing Tensorflow in Python 2.7 in Ubuntu 12.04. 'GLIBC_2.17 not found'

我已经成功安装了 python 的 Tensorflow 绑定。但是当我尝试导入 Tensorflow 时,出现以下错误。

ImportError: /lib/x86_64-linux-gnu/libc.so.6: version `GLIBC_2.17' not found (required by /usr/local/lib/python2.7/dist-packages/tensorflow/python/_pywrap_tensorflow.so)

我尝试将 GLIBC_2.15 更新到 2.17,但没有成功。

我遇到了同样的问题,所以谷歌搜索我做了这些步骤:

$ sudo pip install --upgrade virtualenv
$ virtualenv --system-site-packages ~/tensorflow
$ cd ~/tensorflow
$ source bin/activate
$ pip install --upgrade https://storage.googleapis.com/tensorflow/linux/cpu/tensorflow-0.5.0-cp27-none-linux_x86_64.whl
$ cd /tmp
$ wget http://launchpadlibrarian.net/137699828/libc6_2.17-0ubuntu5_amd64.deb
$ wget http://launchpadlibrarian.net/137699829/libc6-dev_2.17-0ubuntu5_amd64.deb
$ mkdir libc6_2.17
$ cd libc6_2.17
$ ar p ../libc6_2.17-0ubuntu5_amd64.deb data.tar.gz | tar zx
$ ar p ../libc6-dev_2.17-0ubuntu5_amd64.deb data.tar.gz | tar zx
$ cd -
$ LD_LIBRARY_PATH=/tmp/libc6_2.17/lib/x86_64-linux-gnu/ /tmp/libc6_2.17/lib/x86_64-linux-gnu/ld-2.17.so bin/python local/lib/python2.7/site-packages/tensorflow/models/image/mnist/convolutional.py

并退出:

$ deactivate 

这对我有用。

如果您的 GNU C-Library 不是 up-to-date,就会出现该错误。 您可以使用简单的

检查您的 运行 版本
$ ldd --version

输出应该是这样的:

ldd (Ubuntu EGLIBC 2.19-0ubuntu6.6) 2.19
Copyright (C) 2014 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.
There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
Written by Roland McGrath and Ulrich Drepper.

2.19 是您的 GLIBC 版本。要升级,您可以访问 GNU-C 库项目网站并下载最新版本。 最新 glibc 的 link 在这里:GNU-C library Download 在撰写此答案时,最新的稳定版本是 2.22

我在 Ubuntu 12.04 和 14.04 上都尝试了 运行 tensorflow。 Ubuntu 14.04 不会引发此问题,因为它默认安装了 glibc 2.19。

希望对您有所帮助。

我尝试了 ,但仍然很烦人:

ImportError: /usr/lib64/libstdc++.so.6: version `GLIBCXX_3.4.14' not found

我在 CentOS 6.7 上,它也缺少更新的 c++ 标准库,因此为了构建 BR_User 解决方案,我提取了正确的 libstdc++ 包,但是我发现不需要虚拟环境。

假设你已经安装了tensorflow,它给出:

mkdir ~/my_libc_env
cd ~/my_libc_env
wget http://launchpadlibrarian.net/137699828/libc6_2.17-0ubuntu5_amd64.deb
wget http://launchpadlibrarian.net/137699829/libc6-dev_2.17-0ubuntu5_amd64.deb
wget ftp.riken.jp/Linux/scientific/7.0/x86_64/os/Packages/libstdc++-4.8.2-16.el7.x86_64.rpm
ar p libc6_2.17-0ubuntu5_amd64.deb data.tar.gz | tar zx
ar p libc6-dev_2.17-0ubuntu5_amd64.deb data.tar.gz | tar zx
rpm2cpio libstdc++-4.8.2-7mgc30.x86_64.rpm| cpio -idmv

然后 运行 python 与:

LD_LIBRARY_PATH="$HOME/my_libc_env/lib/x86_64-linux-gnu/:$HOME/my_libc_env/usr/lib64/" $HOME/my_libc_env/lib/x86_64-linux-gnu/ld-2.17.so `which python`

如果不行,我有,但你不会喜欢的。

好的,这是我在 中提到的另一个解决方案,它更棘手,但应该始终适用于 GLIBC>=2.12 和 GLIBCXX>=3.4.13 的系统。 在我的例子中,它是在 CentOS 6.7 上,但它也适用于 Ubuntu 12.04.

我们将需要一个支持 c++11 的 gcc 版本,可以在另一台机器上或独立安装;但暂时没有。

我们在这里要做的是编辑 _pywrap_tensorflow.so 二进制文件,以便 'weakify' 它的 libc 和 libstdc++ 依赖项,以便 ld 接受 link 我们正在创建的存根将会做。然后我们将为丢失的符号制作这些存根,最后我们将 pre-load 所有这些当 运行 宁 python.

首先,我要感谢 James 的精彩文章 (http://www.lightofdawn.org/wiki/wiki.cgi/NewAppsOnOldGlibc) 和宝贵的建议,没有他我无法完成。

所以让我们从弱化依赖关系开始,它只是替换 _pywrap_tensorflow.so 中的正确字节。请注意,此步骤仅适用于当前版本的 tensorflow (0.6.0)。因此,如果它尚未完成,请创建并激活你的 virtualenv 如果你有一个(如果你不是管理员 virtualenv 是一个解决方案,另一个是将 --user 标志添加到 pip 命令),并安装 tensorflow 0.6 .0(如果你想要 gpu 版本,请在 url 中用 gpu 替换 cpu):

pip install --upgrade https://storage.googleapis.com/tensorflow/linux/cpu/tensorflow-0.6.0-cp27-none-linux_x86_64.whl

让我们削弱所有烦人的依赖关系,这里是 cpu 版本的 tensorflow 的命令:

TENSORFLOW_DIR=`python -c "import imp; print(imp.find_module('tensorflow')[1])"`
for addr in 0xC6A93C 0xC6A99C 0xC6A9EC 0xC6AA0C 0xC6AA1C 0xC6AA3C; do printf '\x02' | dd conv=notrunc of=${TENSORFLOW_DIR}/python/_pywrap_tensorflow.so bs=1 seek=$((addr)) ; done

这是 gpu 之一(运行 只有正确的一个,否则你会破坏二进制文件):

TENSORFLOW_DIR=`python -c "import imp; print(imp.find_module('tensorflow')[1])"`
for addr in 0xDC5EA4 0xDC5F04 0xDC5F54 0xDC5F74 0xDC5F84 0xDC5FA4; do printf '\x02' | dd conv=notrunc of=${TENSORFLOW_DIR}/python/_pywrap_tensorflow.so bs=1 seek=$((addr)) ; done

您可以通过以下方式查看:

readelf -V ${TENSORFLOW_DIR}/python/_pywrap_tensorflow.so

如果您想了解这里发生了什么,请查看这篇文章。

现在我们要为缺少的 libc 符号制作存根:

mkdir ~/my_stubs
cd ~/my_stubs
MYSTUBS=~/my_stubs
printf "#include <time.h>\n#include <string.h>\nvoid* memcpy(void *dest, const void *src, size_t n) {\nreturn memmove(dest, src, n);\n}\nint clock_gettime(clockid_t clk_id, struct timespec *tp) {\nreturn clock_gettime(clk_id, tp);\n}" > mylibc.c
gcc -s -shared -o mylibc.so -fPIC -fno-builtin mylibc.c

需要在缺少依赖项的机器(或具有相似版本标准库的机器(例如在集群中))上执行该步骤。

现在我们可能要更换机器,因为我们需要一个支持 c++11 的 gcc,并且它可能不在缺少所有依赖项的机器上(或者您可以使用最近的独立安装海湾合作委员会)。在下文中,我假设我们仍在 ~/my_stubs 中,并且您以某种方式在机器之间共享您的家,否则您只需复制我们将在完成后生成的 .so 文件。

所以,我们可以为 libstdc++ 做一个存根,对于其余缺失的存根,我们将从 gcc 源代码编译它们(克隆存储库可能需要一些时间):

printf "#include <functional>\nvoid std::__throw_bad_function_call(void) {\nexit(1);\n}" > bad_function.cc
gcc -std=c++11 -s -shared -o bad_function.so -fPIC -fno-builtin bad_function.cc
git clone https://github.com/gcc-mirror/gcc.git
cd gcc
mkdir my_include
mkdir my_include/ext
cp libstdc++-v3/include/ext/aligned_buffer.h my_include/ext
gcc -I$PWD/my_include -std=c++11 -fpermissive -s -shared -o $MYSTUBS/hashtable.so -fPIC -fno-builtin libstdc++-v3/src/c++11/hashtable_c++0x.cc
gcc -std=c++11 -fpermissive -s -shared -o $MYSTUBS/chrono.so -fPIC -fno-builtin libstdc++-v3/src/c++11/chrono.cc
gcc -std=c++11 -fpermissive -s -shared -o $MYSTUBS/random.so -fPIC -fno-builtin libstdc++-v3/src/c++11/random.cc
gcc -std=c++11 -fpermissive -s -shared -o $MYSTUBS/hash_bytes.so -fPIC -fno-builtin ./libstdc++-v3/libsupc++/hash_bytes.cc

就是这样!您现在可以通过预加载我们所有的共享库(以及您的本地 libstdc++)来 运行 tensorflow python 脚本:

LIBSTDCPP=`ldconfig -p | grep libstdc++.so.6 | grep 64 | cut -d' ' -f4` #For 64bit machines
LD_PRELOAD="$MYSTUBS/mylibc.so:$MYSTUBS/random.so:$MYSTUBS/hash_bytes.so:$MYSTUBS/chrono.so:$MYSTUBS/hashtable.so:$MYSTUBS/bad_function.so:$LIBSTDCPP" python ${TENSORFLOW_DIR}/models/image/mnist/convolutional.py

:)

我的解决方案与 Theo T 的解决方案类似;尽管针对 Xubuntu 12.04 (CAELinux 2013) 进行了微调

sudo pip install --upgrade https://storage.googleapis.com/tensorflow/linux/cpu/tensorflow-0.6.0-cp27-none-linux_x86_64.whl
sudo su
# prompt for password
TENSORFLOW_DIR=`python -c "import imp; print(imp.find_module('tensorflow')[1])"`
for addr in 0xC6A93C 0xC6A99C 0xC6A9EC 0xC6AA0C 0xC6AA1C 0xC6AA3C; do printf '\x02' | dd conv=notrunc of=${TENSORFLOW_DIR}/python/_pywrap_tensorflow.so bs=1 seek=$((addr)) ; done

readelf -V ${TENSORFLOW_DIR}/python/_pywrap_tensorflow.so
exit

mkdir ~/.my_stubs
cd ~/.my_stubs
MYSTUBS=~/.my_stubs


printf "#include <time.h>\n#include <string.h>\nvoid* memcpy(void *dest, const void *src, size_t n) {\nreturn memmove(dest, src, n);\n}\nint clock_gettime(clockid_t clk_id, struct timespec *tp) {\nreturn clock_gettime(clk_id, tp);\n}" > mylibc.c
gcc -s -shared -o mylibc.so -fPIC -fno-builtin mylibc.c

printf "#include <functional>\nvoid std::__throw_bad_function_call(void) {\nexit(1);\n}" > bad_function.cc
gcc -std=c++11 -s -shared -o bad_function.so -fPIC -fno-builtin bad_function.cc

git clone https://github.com/gcc-mirror/gcc.git

cd gcc
mkdir my_include
mkdir my_include/ext
cp libstdc++-v3/include/ext/aligned_buffer.h my_include/ext
gcc -I$PWD/my_include -std=c++11 -fpermissive -s -shared -o $MYSTUBS/hashtable.so -fPIC -fno-builtin libstdc++-v3/src/c++11/hashtable_c++0x.cc
gcc -std=c++11 -fpermissive -s -shared -o $MYSTUBS/chrono.so -fPIC -fno-builtin libstdc++-v3/src/c++11/chrono.cc
gcc -std=c++11 -fpermissive -s -shared -o $MYSTUBS/random.so -fPIC -fno-builtin libstdc++-v3/src/c++11/random.cc
gcc -std=c++11 -fpermissive -s -shared -o $MYSTUBS/hash_bytes.so -fPIC -fno-builtin ./libstdc++-v3/libsupc++/hash_bytes.cc

以下两个步骤将 运行 位于 tensorflow/models/image/mnist 目录中的 convolution.py 脚本:

LIBSTDCPP=`ldconfig -p | grep libstdc++.so.6 | grep 64 | cut -d' ' -f4` #For 64bit machines
LD_PRELOAD="$MYSTUBS/mylibc.so:$MYSTUBS/random.so:$MYSTUBS/hash_bytes.so:$MYSTUBS/chrono.so:$MYSTUBS/hashtable.so:$MYSTUBS/bad_function.so:$LIBSTDCPP" python ${TENSORFLOW_DIR}/models/image/mnist/convolutional.py

如果您希望 python 始终加载这些减少的依赖项,只需通过以下步骤将其添加到 .bashrc 文件中:

echo "LIBSTDCPP=`ldconfig -p | grep libstdc++.so.6 | grep 64 | cut -d' ' -f4`" >> ~/.bashrc
echo alias python="'"LD_PRELOAD='"''$'MYSTUBS/mylibc.so:'$'MYSTUBS/random.so:'$'MYSTUBS/hash_bytes.so:'$'MYSTUBS/chrono.so:'$'MYSTUBS/hashtable.so:'$'MYSTUBS/bad_function.so:'$'LIBSTDCPP'"' python"'" >> ~/.bashrc

如果你有 ipython:

echo alias ipython="'"LD_PRELOAD='"''$'MYSTUBS/mylibc.so:'$'MYSTUBS/random.so:'$'MYSTUBS/hash_bytes.so:'$'MYSTUBS/chrono.so:'$'MYSTUBS/hashtable.so:'$'MYSTUBS/bad_function.so:'$'LIBSTDCPP'"' ipython"'" >> ~/.bashrc

基本上,我将其更新为在 dist_packages 中完成的 python 安装,而不是 site-packages。此外,我从 gcc-mirror 而不是 gcc 的本机 git 页面克隆。其他都一样。

最后几个步骤确保每次您 运行 python 或 ipython 时,它都会加载减少的依赖项。

我试过了 solution, and its worked perfectly. But libstdc++-4.8.2-7 download link cannot be accessed any more, try to pick up a new one from this page: http://rpm.pbone.net/index.php3/stat/4/idpl/31981489/dir/centos_7/com/libstdc++-4.8.5-4.el7.x86_64.rpm.html

对我来说,我用这个:

wget ftp://mirror.switch.ch/pool/4/mirror/centos/7.2.1511/os/x86_64/Packages/libstdc++-4.8.5-4.el7.x86_64.rpm

@Théo T 的回答中的 link 不再有效,因此我不得不尝试多种替代方案,最终找到一个可行的方案。

mkdir ~/my_libc_env
cd ~/my_libc_env
wget http://launchpadlibrarian.net/137699828/libc6_2.17-0ubuntu5_amd64.deb
wget http://launchpadlibrarian.net/137699829/libc6-dev_2.17-0ubuntu5_amd64.deb
wget ftp://ftp.icm.edu.pl/vol/rzm5/linux-slc/centos/7.0.1406/cr/x86_64/Packages/libstdc++-4.8.3-9.el7.x86_64.rpm
ar p libc6_2.17-0ubuntu5_amd64.deb data.tar.gz | tar zx
ar p libc6-dev_2.17-0ubuntu5_amd64.deb data.tar.gz | tar zx
rpm2cpio libstdc++-4.8.3-9.el7.x86_64.rpm| cpio -idmv

运行 python 使用 tensorflow 的代码

LD_LIBRARY_PATH="$HOME/my_libc_env/lib/x86_64-linux-gnu/:$HOME/my_libc_env/usr/lib64/" $HOME/my_libc_env/lib/x86_64-linux-gnu/ld-2.17.so `which python` your_program.py

注意:您应该根据您的平台下载deb包:

x86:

https://launchpad.net/ubuntu/raring/i386/libc6/2.17-0ubuntu5.1

wget http://launchpadlibrarian.net/151932048/libc6_2.17-0ubuntu5.1_i386.deb

sudo dpkg -i libc6_2.17-0ubuntu5.1_i386.deb

x64:

https://launchpad.net/ubuntu/raring/amd64/libc6/2.17-0ubuntu5.1

wget http://launchpadlibrarian.net/151925896/libc6_2.17-0ubuntu5.1_amd64.deb

sudo dpkg -i libc6_2.17-0ubuntu5.1_amd64.deb

在我的 12.04 Ubuntu x64.

上测试

注意 'weakifying' Theo Trouillon 的解决方案。它仅适用于 Tensorflow 0.6.0。如果你想将它应用于 Tensorflow 0.9.0,它会变得更加棘手。 我的情况是 CPU 模式,Centos 6.7 其中 GLIBC 2.12 GLIBCXX 3.4.13.

正在安装张量流:

pip uninstall protobuf 
pip install --upgrade https://storage.googleapis.com/tensorflow/linux/cpu/tensorflow-0.9.0-cp27-none-linux_x86_64.whl

'weakifying' 代码应更改:

TENSORFLOW_DIR=`python -c "import imp; print(imp.find_module('tensorflow')[1])"`
for addr in 0x376e34 0x376e94 0x376ef4 0x376f14 0x376f24 0x376f44 0x376f54 ; do printf '\x02' | dd conv=notrunc of=${TENSORFLOW_DIR}/python/_pywrap_tensorflow.so bs=1 seek=$((addr)) ; done
for addr in 0x93aa4 0x93b04 ; do printf '\x02' | dd conv=notrunc of=${TENSORFLOW_DIR}/contrib/layers/python/ops/_bucketization_op.so bs=1 seek=$((addr)) ; done
for addr in 0x95bec 0x95c4c ; do printf '\x02' | dd conv=notrunc of=${TENSORFLOW_DIR}/contrib/layers/python/ops/_sparse_feature_cross_op.so bs=1 seek=$((addr)) ; done
for addr in 0x9ffec 0x9fffc 0xa005c ; do printf '\x02' | dd conv=notrunc of=${TENSORFLOW_DIR}/contrib/metrics/python/ops/_set_ops.so bs=1 seek=$((addr)) ; done
for addr in 0x96aa4 0x96b04 0x96b24; do printf '\x02' | dd conv=notrunc of=${TENSORFLOW_DIR}/contrib/linear_optimizer/python/ops/_sdca_ops.so bs=1 seek=$((addr)) ; done

并且应将以下代码添加到较新机器上生成代码的存根中。

mkdir my_include/bits
cp libstdc++-v3/include/bits/shared_ptr_atomic.h my_include/bits/
cp libstdc++-v3/include/std/memory my_include/
gcc -I$PWD/my_include -std=c++11 -fpermissive -s -shared -o ${MYSTUBS}/shared_ptr.so -fPIC -fno-builtin ./libstdc++-v3/src/c++11/shared_ptr.cc
gcc -I$PWD/my_include -std=c++11 -fpermissive -s -shared -o ${MYSTUBS}/list.so -fPIC -fno-builtin ./libstdc++-v3/src/c++98/list.cc
echo "
#include <unistd.h>
#include <stdlib.h>
char * secure_getenv (const char *name) {
          if ((getuid () == geteuid ()) && (getgid () == getegid ())) return getenv (name); else  return NULL;
}" > getenv.cc
gcc -I$PWD/my_include  -std=c++11 -fpermissive -s -shared -o    ${MYSTUBS}/getenv.so -fPIC -fno-builtin getenv.cc

当然,最终的 运行 代码应该更改为包含额外的存根。

LD_PRELOAD="$MYSTUBS/list.so:$MYSTUBS/mylibc.so:$MYSTUBS/shared_ptr.so:$MYSTUBS/getenv.so:$MYSTUBS/random.so:$MYSTUBS/hash_bytes.so:$MYSTUBS/chrono.so:$MYSTUBS/hashtable.so:$MYSTUBS/bad_function.so:$LIBSTDCPP" python  ${TENSORFLOW_DIR}/models/image/mnist/convolutional.py

我刚刚在 CentOS 6.5 和 glibc 2.12 上安装了 tensorflow 0.12rc0,但没有 root 权限。简单地通过 pip 安装 tensorflow 二进制文件给我一个错误,也与 GLIBC 版本有关。

基本上,您有 4 个选项来处理这个问题(每个选项都有一些优点和缺点):

选项 1 - 全局升级您的系统 GLIBC。

This is, probably, the best option, if your system supports this, you have root privileges, and you are confident that this upgrade won't break anything for some weird reason. Ultimately, this goes up to upgrading the whole Linux distribution. Here 是流行发行版默认 GLIBC 版本的简短列表。

选项 2 - 将第二个 GLIBC 添加到您的系统

. The most simple&straightforward option. Especially if you only need to run few simple scripts

  • 在同一个系统上安装多个版本的 glibc is possible,但是应该非常小心。
  • 如果您的所有更改仅限于虚拟环境,您就不会破坏您的系统。
  • 许多程序,installed/compiled 之前可能依赖于旧的 GLIBC, 在您的新环境中(例如您的 python IDE)。包括最基本的 bash 命令,例如 "lc"、"cd" 等
  • 其他副作用如 significant memory leaks 也是可能的。
  • 因此, 是一个非常糟糕的主意,例如通过 .bashrc
  • 另一方面,如果您在新的虚拟环境中需要一些特定的工具,您可以 recompile it, linking against new GLIBC。所以,它会在您的新环境中正常工作。
  • 但是,就个人而言,我很快就放弃了在新环境(没有 root 和包管理器)中重新编译我需要的一切。
  • A 由 GLIBC 开发人员正式提供,用于测试新的 GLIBC 版本。

选项 3 - 修补 tensorflow

may work for TF 0.6.0, but you would probably have to start again from scratch, when each new tensorflow version is released. E.g. here 对 0.9.0 进行了修复。

选项 4 - 从源代码编译 tensorflow

如果您从源代码重新编译它并 link 针对您现有的 GLIBC,将不再需要更新的 GLIBC。不知何故,这里的任何答案都没有提到这个选项。恕我直言,这是最好的选择,“in general”和 "specifically for tensorflow".

  • 这在 r0.11 上工作正常,可能会工作多年,但理论上,如果他们决定实际使用一些新的 GLIBC 功能,它可能会在一些较新的 tensorflow 版本中中断,旧版本中不存在。
  • 老实说,从源代码构建 tensorflow 并不简单,尤其是在过时的系统上。

"building tensorflow on outdated system"的快速总结:

虽然官方指南提供了“installing from sources”部分,但您需要做一些技巧才能在过时的系统上构建它。在这里,我假设您没有 root 权限(如果您有 - 您可能可以使用包管理器安装相同的预请求,而不是他们从源代码手动构建它们)。

我发现了两个有据可查的成功案例:#1, #2 and a number of useful posts on the official github (mostly about a set of libraries to link inside the binary): #1, #2, #3, #4。在我的案例中,我必须结合那里描述的技巧才能成功编译 TF。

  1. 首先,检查你的gcc --versionverify that it supports c++11. Mine was 4.4.7, so it won't work. I've downloaded gcc-4.9.4 source code, and compiled it. This step is pretty straightforward, but the compilation itself may take few hours. As a workaround for an issue in bazel, I've compiled gcc with hardcoded paths to as,ld and nm. However, you may try another workarounds: (1, 2)。

    #!/bin/sh
    
    unset LIBRARY_PATH CPATH C_INCLUDE_PATH 
    unset PKG_CONFIG_PATH CPLUS_INCLUDE_PATH INCLUDE LD_LIBRARY_PATH
    
    cd gcc-4.9.4
    ./contrib/download_prerequisites
    
    mkdir objdir
    cd objdir
    
    
    # I've added --disable-multilib to fix the following error:
    # /usr/bin/ld: crt1.o: No such file: No such file or directory
    # collect2: ld returned 1 exit status
    # configure: error: I suspect your system does not have 32-bit 
    # developement libraries (libc and headers). If you have them,
    # rerun configure with --enable-multilib. If you do not have them, 
    # and want to build a 64-bit-only compiler, rerun configure 
    # with --disable-multilib.           
    
    ../configure --prefix=$HOME/opt/gcc-4.9.4 \
                 --disable-multilib \
                 --disable-nls \
                 --enable-languages=c,c++ \
                 --with-ld=/usr/bin/ld \
                 --with-nm=/usr/bin/nm \
                 --with-as=/usr/bin/as
    
    make        
    make install
    
  2. 检查你的 java --version。 Bazel 需要 JDK 8,如果需要请安装它。 (他们仍然 provide 一些与 jdk7 相关的下载,用于 bazel-0.4.1,但看起来他们认为它已被弃用)

  3. 我创建了一个单独的 use_gcc_4.9.4.sh 文件,其中包含必要的环境变量。当我需要与这个较新的编译器相关的东西时,我使用 source ./use_gcc_4.9.4.sh

    #!/bin/sh
    this=$HOME/opt/gcc-4.9.4
    export PATH=$this/bin:$PATH
    export CPATH=$this/include:$CPATH
    export LIBRARY_PATH=$this/lib:$LIBRARY_PATH
    export LIBRARY_PATH=$this/lib64:$LIBRARY_PATH
    export LD_LIBRARY_PATH=$this/lib:$LD_LIBRARY_PATH
    export LD_LIBRARY_PATH=$this/lib64:$LD_LIBRARY_PATH
    
  4. 当前的 bazel 二进制文件 (0.4.1) requires GLIBC 2.14, so we have to compile bazel from source as well (with our new gcc). Works OK, unless you are only allowed to run a very limited number of threads on the target machine. (This post 描述了一些额外的解决方法,但在我的情况下不需要它们,可能是由于 bazel 最近的更新代码。)

  5. 获取tensorflow源代码git clone https://github.com/tensorflow/tensorflow,并安装您需要的先决条件(CUDA、cuDNN、python等)。见 official guide.

  6. 如果您没有使用默认系统 gcc(例如,如果您必须编译更新的 gcc,如上所述),请添加 the following linker flags to tensorflow/third_party/gpus/crosstool/CROSSTOOL.tpl, line 59:

    linker_flag: "-L/home/username/localinst/opt/gcc-4.9.4/lib64"
    linker_flag: "-Wl,-rpath,/home/username/localinst/opt/gcc-4.9.4/lib64"
    

    如果没有这一步,您可能会 运行 出现类似 this:

    的错误消息
    # ERROR: /home/username/localdistr/src/tensorflow/tensorflow/tensorflow/core/debug/BUILD:33:1: null failed: protoc failed: error executing command bazel-out/host/bin/external/protobuf/protoc '--cpp_out=bazel-out/local_linux-py3-opt/genfiles/' '--plugin=protoc-gen-grpc=bazel-out/host/bin/external/grpc/grpc_cpp_plugin' ... (remaining 8 argument(s) skipped): com.google.devtools.build.lib.shell.BadExitStatusException: Process exited with status 1.
    # bazel-out/host/bin/external/protobuf/protoc: /usr/lib64/libstdc++.so.6: version `GLIBCXX_3.4.20' not found (required by bazel-out/host/bin/external/protobuf/protoc)
    # bazel-out/host/bin/external/protobuf/protoc: /usr/lib64/libstdc++.so.6: version `CXXABI_1.3.8' not found (required by bazel-out/host/bin/external/protobuf/protoc)
    # bazel-out/host/bin/external/protobuf/protoc: /usr/lib64/libstdc++.so.6: version `GLIBCXX_3.4.18' not found (required by bazel-out/host/bin/external/protobuf/protoc)
    
  7. 最后,为了避免 GLIBC 依赖,我们必须静态 link 一些库,通过添加 -lrt linker 标志(maybe -lm)。我发现了多个 post,建议以不同的方式添加:

    没有 -lrt 我 运行 再次进入特定于 GLIBC 版本的错误,试图 import tensorflow:

    # ImportError: /lib64/libc.so.6: version `GLIBC_2.14' not found (required by /home/username/anaconda3/envs/myenvname/lib/python3.5/site-packages/tensorflow/python/_pywrap_tensorflow.so)
    

    没有-lm你可以运行变成this(对我来说,结果是没有必要)。

  8. 运行构建过程。

    source ./use_gcc_4.9.4.sh
    ./configure
    bazel build -c opt --config=cuda //tensorflow/tools/pip_package:build_pip_package
    bazel-bin/tensorflow/tools/pip_package/build_pip_package /tmp/tensorflow_pkg
    pip install --upgrade /tmp/tensorflow_pkg/tensorflow-0.12.0rc0-cp35-cp35m-linux_x86_64.whl
  1. 尝试运行以下简单的python脚本来测试最基本的功能是否正常:

    import tensorflow as tf
    hello = tf.constant('Hello, TensorFlow!')
    sess = tf.Session()
    print(sess.run(hello))
    
    a = tf.constant(10)
    b = tf.constant(32)
    print(sess.run(a + b))
    

上面的答案很好,但我做了之后仍然遇到一些问题。我按照 @Theo 在 rhel 上的回答进行操作,我试图导出引发了一些错误的 LD_LIBRARY_PATH,以及 LD_LIBRARY_PATH 还包括 LD_PRELOAD as

mkdir libstdc
cd libstdc
rpm2cpio ../libstdc++-4.8.3-9.el7.x86_64.rpm| cpio -idmv
LD_LIBRARY_PATH="$HOME/my_libc_env/lib/x86_64-linux-gnu/:$HOME/my_libc_env/lib64/" LD_PRELOAD="$HOME/my_libc_env/libstdc/usr/lib64/libstdc++.so.6.0.19" $HOME/my_libc_env/lib/x86_64-linux-gnu/ld-2.17.so `which python`

你必须在 运行 python 时始终执行此操作,因此制作一个脚本来加载这些变量并调用 python 解释器

vim tmp.sh

LD_LIBRARY_PATH="$HOME/my_libc_env/lib/x86_64-linux-gnu/:$HOME/my_libc_env/lib64/" LD_PRELOAD="$HOME/my_libc_env/libstdc/usr/lib64/libstdc++.so.6.0.19" $HOME/my_libc_env/lib/x86_64-linux-gnu/ld-2.17.so `which python`

在 python 中导入 tensorflow 时得到 "version `GLIBC_2.14' not found"。在 Centos6.6 上使用 virtualenv 在 python3.5 中导入了 tensorflow:

mkdir ~/tensorflow
cd ~/tensorflow
virtualenv --system-site-packages -p python3.5 ~/tensorflow
source bin/activate
wget http://launchpadlibrarian.net/137699828/libc6_2.17-0ubuntu5_amd64.deb
wget http://launchpadlibrarian.net/137699829/libc6-dev_2.17-0ubuntu5_amd64.deb
wget ftp://195.220.108.108/linux/mageia/distrib/4/x86_64/media/core/updates/libstdc++6-4.8.2-3.2.mga4.x86_64.rpm
wget https://rpmfind.net/linux/centos/7.4.1708/updates/x86_64/Packages/glibc-2.17-196.el7_4.2.i686.rpm
ar p libc6_2.17-0ubuntu5_amd64.deb data.tar.gz | tar zx
ar p libc6-dev_2.17-0ubuntu5_amd64.deb data.tar.gz | tar zx
rpm2cpio libstdc++6-4.8.2-3.2.mga4.x86_64.rpm | cpio -idmv
rpm2cpio glibc-2.17-196.el7_4.2.i686.rpm | cpio -idmv
pip3.5 install --upgrade tensorflow
export PYTHONPATH="$HOME/tensorflow/lib/python3.5/site-packages/"
alias tfpython='LD_LIBRARY_PATH="$HOME/tensorflow/lib/x86_64-linux-gnu/:$HOME/tensorflow/usr/lib64/" $HOME/tensorflow/lib/x86_64-linux-gnu/ld-2.17.so /usr/local/bin/python3.5'
tfpython

我在尝试在旧 HPC Linux 系统 运行 上安装 numba 时遇到了类似的问题 SUSE Linux Enterprise Server 11,具有 glibc 2.11.3(根据 ldd --version)。我没有超级用户访问权限,任何常规的 conda 安装(来自频道 defaultsconda-forgenumba)都会失败并显示 OSError: /lib64/libpthread.so.0: versionGLIBC_2.12 not found (required by ...envs/test1-py37/lib/python3.7/site-packages/llvmlite/binding/libllvmlite.so)。我通过以下步骤成功安装了 numba

  1. 创建conda环境
  2. 在 conda 环境中,从 nlsec 环境
  3. 安装 glibc
  4. 然后使用pip安装numballvmlite针对新安装的glibc构建:

因此:

conda create -n test -c nlesc glibc
conda activate test
pip install numba

然后导入 numba 就可以了。但是,随后应注意任何后续 conda install 不会 "upgrade" llvmlitenumba 不兼容的版本或旧问题 returns。您可能需要 一个或两个来实现此效果。您必须固定构建和版本。我创建了一个文件 pinned 包含:

llvmlite==0.27.0-pypi
numba==0.42.0-pypi

并将它放在我的 conda 环境中的一个文本文件中 conda-meta/pinned

我遇到了同样的问题 found the following solution:

conda install tensorflow