<环境配置> 集群服务器的远程登录和pytorch环境配置

256 阅读2分钟

背景

在实验室科研场景下,往往需要用到实验室的GPU集群资源,这时候需要登录远程服务器并从零配置环境。这里以ssh登录、anaconda+pytorch+jupyterlab的配置。

远程登录

  1. 联系管理员获取主机ip地址、账号和密码
  2. 通过IDEA、pycharm、finalshell等方法建立ssh连接,以IDEA为例 image.png image.png
  3. 连接后可以浏览远程主机的文件系统,也可以终端控制

pytorch环境配置

  1. 下载和安装anaconda
wget https://mirrors.tuna.tsinghua.edu.cn/anaconda/archive/Anaconda3-2022.05-Linux-x86_64.sh

如果报403的话,可能是触发了反爬,加个user-agent即可,见blog.csdn.net/qq_45934285…:

wget -U NoSuchBrowser/1.0 https://mirrors.tuna.tsinghua.edu.cn/anaconda/archive/Anaconda3-2022.05-Linux-x86_64.sh

安装

bash Anaconda3-2022.05-Linux-x86_64.sh

激活

source ~/.bashrc

如果激活后输入conda还是显示command not found,则加个环境变量,注意路径改成自己的anaconda路径,见blog.csdn.net/qq_45175818…

vim ~/.bashrc
//i进入插入模式
export PATH=$PATH:/home/root/anaconda3/bin
//:wq保存并退出
source ~/.bashrc
  1. 添加清华源和激活虚拟环境
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/free/
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/
conda config --append channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/fastai/
conda config --append channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/pytorch/
conda config --append channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/bioconda/
//torch是环境命名
conda create -n torch python=3.7
//进入虚拟环境
conda activate torch
  1. 安装pytorch,python和cuda版本对应blog.csdn.net/Z_zfer/arti…
conda install pytorch==1.7.0 torchvision==0.8.0 torchaudio==0.7.0 cudatoolkit=10.2 -c pytorch
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple tensorflow==2.2.0rc1
  1. 配置jupyter 安装(已进入虚拟环境)
conda install jupyter notebook

生成配置文件

jupyter notebook --generate-config

生成密码(先进入ipython逐行输入)

ipython
from notebook.auth import passwd
passwd()
#输入你的设置密码,并复制所生成的密文

进入.jupyter文件夹,修改配置文件jupyter_notebook_config.py

cd .jupyter
vim jupyter_notebook_config.py
//复制并替换成自己的密文
c.NotebookApp.allow_remote_access = True               # 允许远程访问          
c.NotebookApp.ip='*'                                   # 就是设置所有ip皆可访问     
c.NotebookApp.open_browser = False                     # 禁止自动打开浏览器    
c.NotebookApp.password = u'刚才复制的那个密文(类似sha:ce...)' 
c.NotebookApp.password_required = True                 #  每次登录需要输入密码             
c.NotebookApp.port =8888                               #这是缺省端口,也可指定其他端口     

设置后台运行、登录所用的ip地址和端口(替换成服务器ip地址和自己设置的端口),jupyter输出记录在Jpyter_out.log

nohup jupyter notebook --ip xxx.xxx.xxx.xxx --port 3330 > jupyter_out.log 2>&1 &

用ps查看进程

ps -ef|grep python

要关闭时可以找到自己的进程号并kill掉

kill -9 xxxxx