在Centos服务器上搭建Anaconda和Jupyter环境以及安装Tensorflow

1.购买云服务器及搭建Anaconda

  1. 购买云服务器,选择Centos X86_64bit系统,会获得一个公网ip,可以选择使用远程桌面登陆或者使用SSH登陆。

    提示:内网使用SSH登陆会出现ip冲突问题,如Socket error Event: 32 Error: 10053.Connection closing...Socket close.这时候选择换一个ip登陆是最好的解决方法(手机热点等)。

  2. 在终端上输入
    wget https://mirrors.tuna.tsinghua.edu.cn/anaconda/archive/Anaconda3-5.2.0-Linux-x86_64.sh
    选择下载为Anaconda5.2.0版本,对应Python3.6版本,在对应的目录下输入
    sh Anaconda3-5.2.0-Linux-x86_64.sh
    就可以成功安装anaconda。
    提示:
    1.这里要选择添加到环境变量,不需要安装VS code。
    2.可能会报错,没有uzip之类的,只需把错误粘到百度里就可以找到对应的命令,输入进去就解决了。
  3. source /root/.bashrc
    这样就可以在直接使用conda list,可以在终端输入conda来检查一下是否安装成功。

2.配置Jupyter Notebook

在Anaconda中,已经安装了jupyter,只需要写一个shell脚本来配置
vi jupyter.sh

#!/bin/bash

#指定jupyter工作目录
jupyter_pj=/root/Jupyter_Notebook
#创建工作目录
mkdir -p $jupyter_pj

## 生成配置文件
jupyter notebook --generate-config 
jupyter_conf=".jupyter/jupyter_notebook_config.py"
# 修改ip
sed -i "s|#c.NotebookApp.ip =.*|c.NotebookApp.ip= '*'|" $jupyter_conf

#修改密码 123456
sed -i "s|#c.NotebookApp.password = .*|c.NotebookApp.password = u'sha1:5df252f58b7f:bf65d53125bb36c085162b3780377f66d73972d1'|" $jupyter_conf
sed -i "s|#c.NotebookApp.open_browser = .*|c.NotebookApp.open_browser = False|" $jupyter_conf
sed -i "s|#c.NotebookApp.port = .*|c.NotebookApp.port = 8899|" $jupyter_conf
sed -i "s|#c.NotebookApp.notebook_dir.*|c.NotebookApp.notebook_dir = u'$jupyter_pj'|" $jupyter_conf


##查看结果
sed -n "/c.NotebookApp.ip=.*/ p" $jupyter_conf
sed -n '/c.NotebookApp.password =.*/ p' $jupyter_conf
sed -n '/c.NotebookApp.open_browser.*/ p' $jupyter_conf
sed -n '/c.NotebookApp.port = .*/ p' $jupyter_conf
##工作目录
sed -n '/c.NotebookApp.notebook_dir = .*/ p' $jupyter_conf

运行脚本
sh jupyter.sh
这里的密码进行了加密,如果想修改密码为其他密码,需要生成加密后的密码,请参照博文里的自定义配置。要想对jupyter进行文件修改,使用
vi /root/.jupyter/jupyter_notebook_config.py
运行jupyter
jupyter notebook --allow-root
但是这时候是无法用外网的服务器打开,需要打开服务器官网的控制台,更改安全组规则,添加一组入方向规则,添加的端口号为上面设置的端口号:8899。


这时候运行jupyter,然后用外网就可以打开jupyter网页(网址:服务器ip:8899,输入刚才设置的密码即可进入)。要想在后台一直运行jupyter,就需要使用nohup命令
nohup jupyter notebook --allow-root &
然后记得用exit命令退出服务器终端。
关于后台运行的博文以及关闭方法的链接

在jupyter中还可以安装扩展插件:
conda install -c conda-forge jupyter_contrib_nbextensions
conda install -c conda-forge jupyter_nbextensions_configurator

以方便jupyter更好地使用(安装完成后需要重新启动)。

3.安装Tensorflow

这里选择使用pip安装,注意使用的Python版本要和上面Anaconda的版本对应,直接使用以下命令安装
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple/ https://mirrors.tuna.tsinghua.edu.cn/tensorflow/linux/cpu/tensorflow-1.12.0-cp36-cp36m-linux_x86_64.whl
在Python环境中运行import tensorflow as tf 测试是否安装成功。
若出现下面这个问题则需要更新一下h5py包


pip install h5py==2.8.0rc1

4.Reference

  • https://www.cnblogs.com/xiao-apple36/p/9052102.html
  • https://www.jianshu.com/p/c8540f3c80f8
  • https://blog.csdn.net/weixin_42561002/article/details/85373917
  • https://blog.csdn.net/wzqnls/article/details/53365280
  • https://www.jianshu.com/p/807a58e6d8e6

你可能感兴趣的:(在Centos服务器上搭建Anaconda和Jupyter环境以及安装Tensorflow)