1、先查看服务器上的cuda版本

➜  ~ cat /usr/local/cuda/version.txt
CUDA Version 9.0.176

2、根据服务器的cuda版本去docker hub 拉镜像

nvidia/cuda:9.2-devel-ubuntu18.04

3、用镜像创建容器

docker run --name torch_gpu --runtime=nvidia -itd  -v /usr/lib64:/usr/lib64 -v /media/mia/:/project/ -w /project/ nvidia/cuda:9.2-devel-ubuntu18.04 /bin/bash

4、在容器里测试

nvidia-smi

5、在容器里安装python3.7.3

apt-get update
apt-get upgrade
apt install build-essential -y
apt install libncurses5-dev libgdbm-dev libnss3-dev libssl-dev libreadline-dev libffi-dev -y
apt install zlib1g-dev
apt install wget
apt install openssl
apt install curl
apt install libsqlite3-dev
wget https://www.python.org/ftp/python/3.7.3/Python-3.7.3.tgz
tar -xvf Python-3.7.3.tgz
cd Python-3.7.3
./configure --enable-loadable-sqlite-extensions
make
make install
apt-get clean
rm -rf /var/lib/apt/lists/*
ln -s /usr/local/bin/pip3 /usr/bin/pip
ln -s /usr/local/bin/python3 /usr/bin/python


6.pip安装requirements.txt

pip install -r requirements.txt

如果下载慢或者超时,设置源


# 清华源

pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple

参考 https://zhuanlan.zhihu.com/p/109939711

或者手动设置延时

pip --default-timeout=100 install numpy

 

7、查看torch-gpu版本是否安装成功

import torch 
print(torch.cuda.is_available())
True代表GPU版本成功安装