WSL搭建深度强化学习环境

WSL搭建深度强化学习环境

https://zhuanlan.zhihu.com/p/683058297

假定你已经安装好wsl

安装miniconda

https://docs.anaconda.com/miniconda/install/

curl -O https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh
bash ~/Miniconda3-latest-Linux-x86_64.sh

安装过程中需要同意协议,是否自动配置环境变量,最后还可以自定义miniconda安装路径

miniconda更换清华源

https://mirrors.tuna.tsinghua.edu.cn/help/anaconda/

若在最后安装自定义过安装路径,则修改路径下的.condarc​文件

编辑${PATH_TO_INSTALL_PATH}/.condarc

channels:
  - defaults
show_channel_urls: true
default_channels:
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/r
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/msys2
custom_channels:
  conda-forge: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
  pytorch: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud

随后清除缓存

conda clean -i

创建conda虚拟环境并激活

一般不同应用或开发以来的python版本不同,本次为深度强化学习单独创建虚拟环境

# 创建python3.9虚拟环境,命名为deep_rl
conda create -n deep_rl python=3.9

# 后续使用该环境需要先激活
conda activate deep_rl

# 退出虚拟环境
conda deactivate 

安装cudatoolkit

https://developer.nvidia.com/cuda-12-1-1-download-archive?target_os=Linux&target_arch=x86_64&Distribution=WSL-Ubuntu&target_version=2.0&target_type=deb_local

image

wget https://developer.download.nvidia.com/compute/cuda/repos/wsl-ubuntu/x86_64/cuda-wsl-ubuntu.pinsudo 
mv cuda-wsl-ubuntu.pin /etc/apt/preferences.d/cuda-repository-pin-600
wget https://developer.download.nvidia.com/compute/cuda/12.1.1/local_installers/cuda-repo-wsl-ubuntu-12-1-local_12.1.1-1_amd64.deb
sudo dpkg -i cuda-repo-wsl-ubuntu-12-1-local_12.1.1-1_amd64.deb
sudo cp /var/cuda-repo-wsl-ubuntu-12-1-local/cuda-*-keyring.gpg /usr/share/keyrings/
sudo apt-get update
sudo apt-get -y install cuda

添加环境变量

# >>> cuda config >>>
# add nvcc compiler to path
export PATH=$PATH:/usr/local/cuda/bin
# add cuBLAS, cuSPARSE, cuRAND, cuSOLVER, cuFFT to path
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/cuda/lib64
# <<< cuda config <<<

安装pytorch

https://pytorch.org

image

# 这里以cuda 12.1版本安装为例,需要更换为自己设备cuda的版本
# conda安装
conda install pytorch torchvision torchaudio pytorch-cuda=12.1 -c pytorch -c nvidia

# pip安装
# pip3 install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu121

验证是否安装成功

import torch # 如果pytorch安装成功即可导入
print(torch.cuda.is_available()) # 查看CUDA是否可用
print(torch.cuda.device_count()) # 查看可用的CUDA数量
print(torch.version.cuda) # 查看CUDA的版本号
————————————————

                            版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
                      
原文链接:https://blog.csdn.net/qq_41340996/article/details/124326865

image

posted @ 2024-12-14 08:49  biiigwang  阅读(9)  评论(0编辑  收藏  举报