一、WSL2安装
使用图形界面
- 打开 控制面版
- 访问 程序和功能 子菜单 打开或关闭Windows功能
- 选择“适用于Linux的Windows子系统”与 “虚拟机平台”与“Hyper-V"
- 点击“确定”
- 重启
搜索power shell ,右键“以管理员方式运行”,执行下面的两行命令
wsl --update
#将 WSL 默认版本设置为 WSL 2
wsl --set-default-version 2
二、下载Ubuntu系统
这里直接下载ubuntu镜像文件
https://learn.microsoft.com/en-us/windows/wsl/install-manual#downloading-distributions
用迅雷下载,速度飞快
下载完成后,会得到文件Ubuntuxxxx.AppxBundle
xxxxx根据由你下载的版本决定
将文件名,重命名为Ubuntuxxxx.zip,不用担心文件会损坏,改成zip就是为了方便解压而已。
解压之后,得到文件夹Ubuntuxxx
进入文件夹,找到文件Ubuntu_xxx_x64.appx,重命名为:Ubuntu_xxxx_x64.zip,然后解压。
得到文件夹Ubuntu_xxxx_x64
然后这个文件夹Ubuntu_xxxx_x64中有:
双击运行,让你输入用户名和密码
三、cuda安装
cuda版本安装cuda11.7(和windows系统上安装一样的版本)
https://developer.nvidia.com/cuda-toolkit-archive?login=from_csdn
然后它会生成安装命令:
这些命令需要在ubuntu命令行中运行,搜索power shell ,输入命令
wsl
即可进入ubuntu命令行
wget https://developer.download.nvidia.com/compute/cuda/repos/wsl-ubuntu/x86_64/cuda-wsl-ubuntu.pin
sudo mv cuda-wsl-ubuntu.pin /etc/apt/preferences.d/cuda-repository-pin-600
wget https://developer.download.nvidia.com/compute/cuda/11.7.0/local_installers/cuda-repo-wsl-ubuntu-11-7-local_11.7.0-1_amd64.deb
sudo dpkg -i cuda-repo-wsl-ubuntu-11-7-local_11.7.0-1_amd64.deb
sudo cp /var/cuda-repo-wsl-ubuntu-11-7-local/cuda-*-keyring.gpg /usr/share/keyrings/
sudo apt-get update
sudo apt-get -y install cuda
在执行完上述命令后,执行下面的系统环境配置:
echo 'export PATH=/usr/local/cuda/bin:$PATH' >> ~/.bashrc
echo 'export LD_LIBRARY_PATH=/usr/local/cuda/lib64:$LD_LIBRARY_PATH' >> ~/.bashrc
source ~/.bashrc
验证是否安装成功:
nvcc -V
显示下面的信息,表示成功
安装好的cuda在路径\wsl.localhost\Ubuntu\usr\local中:
四、cudnn安装
https://developer.nvidia.com/rdp/cudnn-archive
cuda11.7对应的cudnn版本是8.8.1.3,因此下载:
下载后的tar包在win系统中
在\home\syh新建cudnn文件夹:
将下载后的tar包通过复制粘贴到cudnn文件夹中:
然后打开ubuntu命令行,执行下面的命令:
tar -xvf cudnn-linux-x86_64-8.8.1.3_cuda11-archive.tar.xz
sudo cp cudnn-*-archive/include/cudnn*.h /usr/local/cuda/include
sudo cp -P cudnn-*-archive/lib/libcudnn* /usr/local/cuda/lib64
sudo chmod a+r /usr/local/cuda/include/cudnn*.h /usr/local/cuda/lib64/libcudnn*
验证是否安装成功,输入如下命令,显示如下图即安装成功
cat /usr/local/cuda/include/cudnn_version.h | grep CUDNN_MAJOR -A 2
安装Anaconda
https://repo.anaconda.com/archive/
如果这样直接下载会下载到win中,我们直接在ubuntu的命令行中执行下面的命令:
wget -P /tmp https://repo.anaconda.com/archive/Anaconda3-2023.03-1-Linux-x86_64.sh
会下载到tmp文件夹中
然后安装Anaconda3:
bash /tmp/Anaconda3-2023.03-1-Linux-x86_64.sh
按回车阅读用户协议
输入yes。
输入安装位置,默认为/home/用户名/anaconda3。
输入yes,运行conda初始化。
重新打开一个conda命令行,前面会自动进入base环境,证明安装成功:
五、Ubuntu换源
步骤一、apt换源
打开清华镜像官网
https://mirrors.tuna.tsinghua.edu.cn/
使用如下命令进入配置文件:
sudo gedit /etc/apt/sources.list
将里面的文本替换为下面的代码,然后保存
# 默认注释了源码镜像以提高 apt update 速度,如有需要可自行取消注释
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ noble main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ noble main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ noble-updates main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ noble-updates main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ noble-backports main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ noble-backports main restricted universe multiverse
# 以下安全更新软件源包含了官方源与镜像站配置,如有需要可自行修改注释切换
deb http://security.ubuntu.com/ubuntu/ noble-security main restricted universe multiverse
# deb-src http://security.ubuntu.com/ubuntu/ noble-security main restricted universe multiverse
# 预发布软件源,不建议启用
# deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ noble-proposed main restricted universe multiverse
# # deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ noble-proposed main restricted universe multiverse
ps:清华源镜像会不定期更新,所以最好按作者所说步骤去复制此镜像源
然后更新源:
sudo apt update # apt更新
步骤二、conda换源
使用如下代码即可换源:
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/linux-64/
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/linux-64/
conda config --set show_channel_urls yes
步骤三、pip换源
创建pip.conf文件
cd ~/.pip
如果提示目录不存在的话,我们要自行创建一个,再进入目录
mkdir ~/.pip
cd ~/.pip
在.pip目录下创建一个pip.conf文件
touch pip.conf
编辑pip.conf文件
sudo gedit ~/.pip/pip.conf
打开pip.conf文件窗口,将以下内容复制到文件中:
[global]
index-url = http://pypi.douban.com/simple
[install]
trusted-host=pypi.douban.com
ctrl+s保存退出
六、创建虚拟环境,安装pytorch和torchvision
创建虚拟环境:
conda create -n your_env_name python=x.x
例如:conda create -n ghrp python=3.8
查看虚拟环境:
conda env list
切换虚拟环境:
conda activate your_env_name
在切换到你虚拟环境后,安装Pytorch,对应版本下载命令在官网中查找
https://pytorch.org/get-started/previous-versions/
我安装这个版本的:
pip install torch==1.13.1+cu117 torchvision==0.14.1+cu117 torchaudio==0.13.1 --extra-index-url https://download.pytorch.org/whl/cu117
安装完成之后可以在命令行中输入python,依次输出以下命令,输出如下图所示则表明安装成功:
import torch
torch.__version__
torch.cuda.is_available()
torch.version.cuda
七、Pycharm远程WSL2
我的配置是代码和**解释器(conda环境)**都在WSL中
点击 File > Open
选择位于 WSL 里面的项目
点击Pycharm 右下角的 Python Interpreter selector,选择 On WSL…
点击 Next
使用conda环境
选择创建的conda虚拟环境
八、vscode远程wsl
点击左下方图标【Open a Remote Windows】
弹出【远程连接】浮窗 ,选择【Connect to WSL】,等待几分钟即可连接到wsl的ubuntu系统
点击左下方的WSL的ubuntu子系统,弹出【远程连接】浮窗,点击【Close Remote Connection】选项就可以退出WSL的ubuntu子系统。
直接在设置(ctrl + shift + p)里面搜索,点击“Python:Select Interpreter”选项,然后选择自己对应的虚拟环境即可