Windows系统WSL2 的ubuntu子系统安装 docker、nvidia-docker调用GPU

安装wsl2

Windows系统WSL2 的ubuntu子系统安装 docker、nvidia-docker调用GPU

如果内部版本低于20150,必须升级否则WSL将无法使用GPU

登录你的windows账户并升级windows至预览版本,(过程可能需要1-2个小时)

Windows系统WSL2 的ubuntu子系统安装 docker、nvidia-docker调用GPU

升级完成后,验证内部版本是否低于20150 ,如果还低说明选错了升级渠道,一定要选择Dev 渠道,最新渠道,获取最新的版本

安装预览版nvdia驱动

https://developer.nvidia.com/cuda/wsl

Windows系统WSL2 的ubuntu子系统安装 docker、nvidia-docker调用GPU

需要登录nvidia账号才能下载,登录账号后下载适合自己的nvidia版本

确认需要开启的系统功能

Windows系统WSL2 的ubuntu子系统安装 docker、nvidia-docker调用GPU

安装WSL2

1.管理员身份PowerShell

dism.exe /online /enable-feature /featurename:VirtualMachinePlatform /all /norestart

重启电脑

2.管理员PowerShell 将WSL2设置为默认版本

wsl --set-default-version 2

提示"WSL 2 需要更新其内核组件”。下载并安装 WSL2 Llinx内核

 

3. 随后进入微软商店下载并安装对应的ubuntu版本即可。或者前往在此选择具体Linux版本

Windows系统WSL2 的ubuntu子系统安装 docker、nvidia-docker调用GPU

4. 验证

管理员PowerShell

wsl --list --verbose

Windows系统WSL2 的ubuntu子系统安装 docker、nvidia-docker调用GPU

只要这个版本是2说明wsl2安装成功。(只有2才可以调用GPU)

两种方式登录wsl2

Windows系统WSL2 的ubuntu子系统安装 docker、nvidia-docker调用GPUWindows系统WSL2 的ubuntu子系统安装 docker、nvidia-docker调用GPU

确保WSL2的linux内核为4.19.121+

Windows系统WSL2 的ubuntu子系统安装 docker、nvidia-docker调用GPU

安装docker

下面代码一行一行执行

sudo apt-get update
sudo apt-get install \
    apt-transport-https \
    ca-certificates \
    curl \
    gnupg \
    lsb-release
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
echo \
  "deb [arch=amd64 signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu \
  $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
sudo apt-get update
sudo apt-get install docker-ce docker-ce-cli containerd.io

安装nvidia-docker

先安装cuda

apt-key adv --fetch-keys http://developer.download.nvidia.com/compute/cuda/repos/ubuntu1804/x86_64/7fa2af80.pub
sh -c 'echo "deb http://developer.download.nvidia.com/compute/cuda/repos/ubuntu1804/x86_64 /" > /etc/apt/sources.list.d/cuda.list'
apt-get update

根据自己的显卡驱动安装对应cuda版本

apt-get install -y cuda-toolkit-11-0

安装nvidia-docker

distribution=$(. /etc/os-release;echo $ID$VERSION_ID)
curl -s -L https://nvidia.github.io/nvidia-docker/gpgkey | sudo apt-key add -
curl -s -L https://nvidia.github.io/nvidia-docker/$distribution/nvidia-docker.list | sudo tee /etc/apt/sources.list.d/nvidia-docker.list
curl -s -L https://nvidia.github.io/libnvidia-container/experimental/$distribution/libnvidia-container-experimental.list | sudo tee /etc/apt/sources.list.d/libnvidia-container-experimental.list
sudo apt-get update
sudo apt-get install -y nvidia-docker2

重启docker

sudo service docker stop
sudo service docker start

验证nvidia-docekr 是否可以调用GPU

创建一个tensorflow容器

docker run --runtime=nvidia  --rm -it --name tensorflow-1.14.0 tensorflow/tensorflow:1.14.0-gpu-py3

不出意外就会报错,因为现最新版的 nvidia-docker有BUG

docker: Error response from daemon: OCI runtime create failed: container_linux.go:367: starting container process caused: process_linux.go:495: container init caused: Running hook #1:: error running hook: exit status 1, stdout: , stderr: nvidia-container-cli: initialization error: driver error: failed to process request: unknown.

对部分包进行降级

apt-get install nvidia-docker2:amd64=2.5.0-1 \
           libnvidia-container-tools:amd64=1.3.3-1 \
           nvidia-container-runtime:amd64=3.4.2-1 \
           libnvidia-container1:amd64=1.3.3-1 \
           nvidia-container-toolkit:amd64=1.4.2-1

再次运行容器

docker run --runtime=nvidia  --rm -it --name tensorflow-1.14.0 tensorflow/tensorflow:1.14.0-gpu-py3

Windows系统WSL2 的ubuntu子系统安装 docker、nvidia-docker调用GPU

测试GPU是否可用

python
import tensorflow as tf
print(tf.test.is_gpu_available())

windows wsl 操作技巧、命令

https://blog.csdn.net/SUNbrightness/article/details/116797665

参考链接

https://docs.microsoft.com/zh-cn/windows/wsl/install-win10

https://docs.nvidia.com/cuda/wsl-user-guide/index.html#running-cuda

https://github.com/NVIDIA/nvidia-docker/issues/1496

https://docs.docker.com/engine/install/ubuntu/

 

上一篇:Err:11 https://developer.download.nvidia.cn/compute/cuda/repos/ubuntu2004/x86_64 Packages 404 No


下一篇:使用nvidia-nsight编译器开发C/C++