背景介绍:
由于远程服务器禁止连接外网,因此我在个人服务器上下载了镜像 pytorch/pytorch ,然后传到服务器上进行部署。
远程服务器已预装 Docker、CUDA ,本教程将在此基础上进行。

镜像部署

  1. 在远程服务器上输入 nvidia-smi 检查 CUDA 版本,然后进入Docker Hub搜索相应版本的镜像,注意安装devel后缀的版本,例如:docker pull pytorch/pytorch:2.13.0-cuda13.2-cudnn9-devel,使用docker images检查是否拉取成功
  2. 保存镜像
docker save pytorch/pytorch:2.13.0-cuda13.2-cudnn9-devel > pytorch.tar
  1. 将镜像传至远程服务器:scp ./pytorch.tar [user]@[ip]。这种方式比较简单,但是由于文件较大(约20G)且 scp没有断点续传,导致重传了很多次,后续了解到可以使用 FileZilla 工具
  2. 进入远程服务器,加载镜像:docker load < ./pytorch.tar,使用docker images检查是否加载成功

容器部署

  1. 创建容器
docker run -d -it -p 80:80 --name me-pytorch --gpus all -v ~/workspace:/root/workspace pytorch/pytorch:2.13.0-cuda13.2-cudnn9-devel
  1. 检查容器是否创建/启动成功:docker ps,如果未启动,使用docker start me-pytorch启动容器
  2. 进入容器
docker exec -it me-pytorch /bin/bash

环境配置

  1. 备份配置文件
cp /etc/apt/sources.list /etc/apt/sources.list.bak
  1. 清空原有文件,并写入镜像(阿里源):
cat > /etc/apt/sources.list <<'EOF'
deb http://mirrors.aliyun.com/ubuntu/ noble main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ noble-security main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ noble-updates main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ noble-backports main restricted universe multiverse
EOF
  1. 更新源 apt update,并安装VIM
apt install vim
  1. 配置 ubuntucuda 镜像源:
## ubuntu
vim /etc/apt/sources.list.d/ubuntu.sources
## 替换URIs(两处替换)
URIs: http://mirrors.aliyun.com/ubuntu
## cuda
vim /etc/apt/sources.list.d/cuda.list
## 替换,注意手动修改版本号
deb http://developer.download.nvidia.cn/compute/cuda/repos/ubuntu2404/x86_64 /
  1. 更新源:apt update,如果报重复源错误configured multiple times,删除ubuntu.sources
mv /etc/apt/sources.list.d/ubuntu.sources /etc/apt/sources.list.d/ubuntu.sources.bak
  1. 如果出现警告Key is stored in legacy trusted.gpg keyring (/etc/apt/trusted.gpg),可以忽略,或者执行
# 治标不治本,治本的话
cp /etc/apt/trusted.gpg /etc/apt/trusted.gpg.d
  1. 验证源更新:apt update && apt upgrade
  2. 验证 PyTorch 和 CUDA 的可用性
# 进入python后
import torch
print(torch.__version__)  # 打印 PyTorch 版本
print(torch.cuda.is_available())  # 检查 CUDA 是否可用
print(torch.version.cuda)  # 打印 CUDA 版本
  1. 更新pip镜像源
pip config set global.index-url https://mirrors.tuna.tsinghua.edu.cn/pypi/web/simple

其他配置

  1. 安装nvtop
apt install nvtop
  1. 安装 net-tools
apt install net-tools
  1. 安装 Node
apt install nodejs npm
Logo

汇聚全球AI编程工具,助力开发者即刻编程。

更多推荐