笔者的服务器配置为2核+4GB+3Mbps,系统为Ubuntu 18.04.4 LTS x86_64

安装Anaconda3

  1. 下载安装包:前往 Anaconda官网 或者 THU开源软件镜像站,下载 Anaconda3-2021.05-Linux-x86_64.sh

  2. 进入安装包所在路径,运行该 .sh 文件:

    1
    $ bash Anaconda3-2021.05-Linux-x86_64.sh
  3. 进入注册信息页面后,根据提示输入 yes 以及 按Enter键。安装完成后,会有提示语:“Thank you for installing Anaconda3!”

  4. 终端输入命令:

    1
    $ python

    进入python命令模式,可观察到 Anaconda 标识

安装(CPU版)PyTorch

以下步骤基于Anaconda3已经安装成功

  1. 为 PyTorch 创建虚拟环境,该环境名称叫做 pytorch

    保证在安装其他框架如 Tensorflow 时,避免冲突产生

    1
    $ conda create -n pytorch python=3.8

    执行该命令后,你会注意到终端中用户名左侧显示 (base)

  2. ⭐进入虚拟环境(常用):

    1
    $ conda activate pytorch

    此时,终端中用户名左侧显示 pytorch

    退出环境,可输入以下命令:

    1
    $ conda deactivate
  3. 为了保证下载速度,需要为 conda 配置清华源,输入命令:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    # pytorch
    $ conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/pytorch/

    # menpo
    $ conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/menpo/

    # bioconda
    $ conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/bioconda/

    # msys2
    $ conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/msys2/

    # conda-forge
    conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge/

    $ conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/
    $ conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/

    # 设置安装路径可见,可通过 cat ~/.condarc 来查看是否安装成功
    $ conda config --set show_channel_urls yes
  4. 根据PyTorch官网提示,

    其给出命令如下:

    1
    $ conda install pytorch torchvision torchaudio cpuonly -c pytorch

    为了使用清华源,我们需要将上面命令的 -c pytorch 删了才能够执行:

    1
    $ conda install pytorch torchvision torchaudio cpuonly
  5. 等待其安装成功后,进入 Python 模式,输入下列代码,检查PyTorch是否安装成功。

    1
    2
    3
    4
    import torch
    import torchvision
    x = torch.rand(5, 3)
    print(x)

安装并且配置Jupyter Notebook

安装Jupyter Notebook

1
$ conda install jupyter

配置Jupyter Notebook实现远程访问

  1. 生成 jupyter notebook 配置文件:

    1
    $ jupyter notebook --generate-config
  2. 生成网站进入的密钥:

    首先在终端进入Python编辑模式,输入下列代码:

    1
    2
    from notebook.auth import passwd
    passwd()

    此时终端会返回一个密钥密文:

    1
    argon2:$argon2id$v=19$m=10240,t=10,p=8$T3rb43TrSnkHY+ecCvDxWw$N4yeBlkuHipEx16srIdRFg
  3. 修改 jupyter notebook 配置文件:

    进入刚生成的配置文件 jupyter_notebook_config(文件目录一般在: ~/.jupyter/jupyter_notebook_config.py

    修改下面的内容(记得去掉注释 #):

    1
    2
    3
    4
    5
    c.NotebookApp.ip='*'										# 任何IP地址均可访问
    c.NotebookApp.password = u'argon2:$............' # 将刚才复制的那个密文粘贴到此
    c.NotebookApp.open_browser = False # 禁止自动打开浏览器
    c.NotebookApp.port =8888 # 服务端口默认为8888,可自行设置
    c.NotebookApp.notebook_dir = '/home/ubuntu/myprojects' # 网站首页打开的目录,即笔记存放的位置。可自行设置
  4. 修改完后,在终端启动Jupyter Notebook服务:

    1
    $ jupyter notebook

    若终端出现以下报错:

    1
    Running as root is not recommended. Use --allow-root to bypass.

    则需要在上述配置文件jupyter_notebook_config中修改:

    1
    c.NotebookApp.allow_root = True   # 设置默认开启root模式

添加拓展的插件

用 conda 来安装:

1
2
$ conda install -c conda-forge jupyter_contrib_nbextensionsconda install -c
conda-forge jupyter_nbextensions_configurator

插件介绍可见此篇文章

参考文章