吴恩达机器学习—服务器上离线安装TensorFlow

作为一枚深度学习的小白,首次在服务器上面安装TensorFlow真的是费劲心思,花了十个小时左右的时间去弄明白,不容易,特此在这里写下我的一些心得体会和安装步骤,让新手少走弯路。

注意:我所用的服务器的时红帽子linux,由于服务器不能连接外网,因此以下教程都是离线!!!!!

我这边推荐安装:Anaconda3 + 仅Cpu版TensorFlow.

附上下载地址的连接:

Anaconda3:https://mirrors.tuna.tsinghua.edu.cn/anaconda/archive/ (我所用的版本是Anaconda3-5.2.0-Linux-x86_64.sh,python支持版本是3.6.5)

setuptools: https://pypi.org/project/setuptools/#files (我用的版本是setuptools-41.0.1-py2.py3-none-any.whl)

protobuf:https://www.piwheels.hostedpi.com/simple/protobuf/ (我用的版本是protobuf-3.7.0rc3-py2.py3-none-any.whl)

TensorFlow:https://pypi.tuna.tsinghua.edu.cn/simple/tensorflow/ (我用的版本是tensorflow-1.0.0-cp36-cp36m-manylinux1_x86_64.whl)

msgpack:https://pypi.org/project/msgpack/#files (我用的版本是 msgpack-0.6.1-cp36-cp36m-manylinux1_x86_64.whl)

six:https://pypi.org/project/six/#files (我用的版本是six-1.12.0-py2.py3-none-any)

除了tensorflow和anaconda3的包,其他包可能不需要,如出现提示缺少对应的包再下载即可。

可自行下载不同版本,但是以上下载的安装包所支持的python版本要一致,不然可能出现未知错误。

可在你的电脑上用浏览器下载对应的安装包,然后上传到服务器,但是文件所放目录要记住!!!

1.安装Anaconda3

连接到你的服务器,进入Anaconda3-5.2.0-Linux-x86_64.sh包所在位置

在终端输入一下命令:

 bash Anaconda3-5.2.0-Linux-x86_64.sh

你可以换成你下载的文件名。

后面的步骤比较简单,一路enter和yes就是,最后安装会提示你是否自动配置环境变量,尽量选择yes,不然conda命令无效。

最后输入一下命令:

export PATH=/home/XXX/anaconda3/bin:$PATH
source source ~/.bashrc

其中PATH里面的;/home/XXX是你Anaconda3-5.2.0-Linux-x86_64.sh包所在位置,你安装后会自动生成一个anaconda3目录。

后面的source命令是立即生效的意思。

然后输入一下命令查看是否安装成功。

conda

如果没有出现错误说明安装成功。

2.创建tensorflow环境

由于服务器不能连接外网,因此需要设置一下离线模式,不然会运行出错无法创建环境。

conda config --set offline true

然后创建tensorflow环境。

conda create -n tensorflow python=3.6

此处python版本应该改成你下在的anaconda适配的版本。

成功的话会出现以下结果:

Solving environment: done

## Package Plan ##

  environment location: /home/zhouhuajian/anaconda3/envs/tensorflow

  added / updated specs: 
    - python=3.6


The following NEW packages will be INSTALLED:

    ca-certificates: 2018.03.07-0           
    libedit:         3.1.20170329-h6b74fdf_2
    libffi:          3.2.1-hd88cf55_4       
    libgcc-ng:       7.2.0-hdf63c60_3       
    libstdcxx-ng:    7.2.0-hdf63c60_3       
    ncurses:         6.1-hf484d3e_0         
    openssl:         1.0.2o-h20670df_0      
    python:          3.6.5-hc3d631a_2       
    readline:        7.0-ha6073c6_4         
    sqlite:          3.23.1-he433501_0      
    tk:              8.6.7-hc745277_3       
    xz:              5.2.4-h14c3975_4       
    zlib:            1.2.11-ha838bed_2      

Proceed ([y]/n)? y

Preparing transaction: done
Verifying transaction: done
Executing transaction: done
#
# To activate this environment, use
#
#     $ conda activate tensorflow
#
# To deactivate an active environment, use
#
#     $ conda deactivate

中间 Proceed ([y]/n)? 选择y。

然后激活环境。

 conda activate tensorflow

你会发现你以及进入环境中了,如下图:

[zhouhuajian@node98 ~]$ conda activate tensorflow
(tensorflow) [zhouhuajian@node98 ~]$ python

(tensorflow)代表你已进入创建的tensorflow环境中。

3.安装protobuf

首先将下载的protobuf文件放到服务器上面,然后进入protobuf文件所在目录

运行一下命令:

pip install --upgrade ./protobuf-3.7.0rc3-py2.py3-none-any.whl

如果出现如下错误,说明你缺少setuptools的包:

Collecting setuptools (from protobuf==3.7.0rc3)
Retrying (Retry(total=4, connect=None, read=None, redirect=None, status=None)) after connection broken by \'NewConnectionError(\'<pip._vendor.urllib3.connection.VerifiedHTTPSConnection object at 0x2b77ffe2d048>: Failed to establish a new connection: [Errno -2] Name or service not known\',)\': /simple/setuptools/
Retrying (Retry(total=3, connect=None, read=None, redirect=None, status=None)) after connection broken by \'NewConnectionError(\'<pip._vendor.urllib3.connection.VerifiedHTTPSConnection object at 0x2b77ffe2d160>: Failed to establish a new connection: [Errno -2] Name or service not known\',)\': /simple/setuptools/
Retrying (Retry(total=2, connect=None, read=None, redirect=None, status=None)) after connection broken by \'NewConnectionError(\'<pip._vendor.urllib3.connection.VerifiedHTTPSConnection object at 0x2b77ffe2d240>: Failed to establish a new connection: [Errno -2] Name or service not known\',)\': /simple/setuptools/
Retrying (Retry(total=1, connect=None, read=None, redirect=None, status=None)) after connection broken by \'NewConnectionError(\'<pip._vendor.urllib3.connection.VerifiedHTTPSConnection object at 0x2b77ffe1b208>: Failed to establish a new connection: [Errno -2] Name or service not known\',)\': /simple/setuptools/
Retrying (Retry(total=0, connect=None, read=None, redirect=None, status=None)) after connection broken by \'NewConnectionError(\'<pip._vendor.urllib3.connection.VerifiedHTTPSConnection object at 0x2b77ffe1b2b0>: Failed to establish a new connection: [Errno -2] Name or service not known\',)\': /simple/setuptools/
Could not find a version that satisfies the requirement setuptools (from protobuf==3.7.0rc3) (from versions: )
No matching distribution found for setuptools (from protobuf==3.7.0rc3)

因此需要安装setuptools的包。

在你的主机上面下载好对应的文件上传到服务器上面。运行一下命令:

pip install --upgrade ./setuptools-41.0.1-py2.py3-none-any.whl

出现以下提示说明安装成功。

Processing ./setuptools-41.0.1-py2.py3-none-any.whl
Installing collected packages: setuptools
Successfully installed setuptools-41.0.1

如果安装了还提示没有这个包,刷新一下或者重新安装一遍。

如果你还出现缺少six包的情况,下载对应的six包,跟setup包安装一样的步骤。

six包缺少错误提示如下

Could not find a version that satisfies the requirement six>=1.9 (from protobuf==3.7.0rc3) (from versions: )
No matching distribution found for six>=1.9 (from protobuf==3.7.0rc3)

3.安装msgpack(如果没有出现缺少该包请忽略)

将下载的安装包上传到服务器,然后进入msgpack包目录所在。

运行一下命令:

pip install --upgrade importtensorflow./msgpack-0.6.1-cp36-cp36m-manylinux1_x86_64.whl

你自行改成自己的目录地址就行。

出现如下结果说明安装成功:

Processing ./msgpack-0.6.1-cp36-cp36m-manylinux1_x86_64.whl
Installing collected packages: msgpack
Successfully installed msgpack-0.6.1

4.安装Tensorflow

将tensorflow文件上传到服务器,进入文件所在目录:

执行一下命令:

pip install --upgrade tensorflow-1.0.0-cp36-cp36m-manylinux1_x86_64.whl

此处目录改成你自己的就行。

如出现一下结果说明安装成功。

Processing ./tensorflow-1.0.0-cp36-cp36m-manylinux1_x86_64.whl
Requirement already satisfied: protobuf>=3.1.0 in ./anaconda3/lib/python3.6/site-packages (from tensorflow==1.0.0) (3.7.0rc3)
Requirement already satisfied: six>=1.10.0 in ./anaconda3/lib/python3.6/site-packages (from tensorflow==1.0.0) (1.11.0)
Requirement already satisfied: wheel>=0.26 in ./anaconda3/lib/python3.6/site-packages (from tensorflow==1.0.0) (0.31.1)
Requirement already satisfied: numpy>=1.11.0 in ./anaconda3/lib/python3.6/site-packages (from tensorflow==1.0.0) (1.14.3)
Requirement already satisfied: setuptools in ./anaconda3/lib/python3.6/site-packages (from protobuf>=3.1.0->tensorflow==1.0.0) (39.1.0)
Installing collected packages: tensorflow
Successfully installed tensorflow-1.0.0

成功安装。