一、告别虚拟机,拥抱WSL2+docker
win10 上使用 linux 系统最轻量的选择。
1、安装wsl+docker
请移步官网查看吧:在 Windows 10 上安装 WSL | Microsoft Docs
ps: 请在使用前查看自己的Windows 版本是否满足要求
1.1 安装ubuntu 18.04
2、安装docker (Windows 安装docker和普通软件差不多,只需要点击下一步就好了)
2.1 安装
请在安装的时候注意勾选(默认勾选),这样你在wsl2 就可以愉快的使用docker,当然你也可以选择,后续在docker设置里面更改。
2.12更改docker镜像源
二、安装开发工具(VS code +Pycharm)
1、安装VS code + 切换预言版本
下载地址:Download Visual Studio Code
2、安装Pycharm(至于怎么安装学习板,请自行百度吧)
PS:如果你有教育网邮箱,完全可以申请免费使用,而不需要去找破解版
下载地址:下载 PyCharm:JetBrains 为专业开发者提供的 Python IDE
三、配置ubuntu + pytorch 环境
1、更换ubuntu 18.04默认镜像源及python 版本
1.1 查看镜像源
cat /etc/apt/sources.list
1.2 ubuntu更改阿里云镜像源
备份源文件
cp /etc/apt/sources.list /etc/apt/sources.list.bak
更改镜像源
vi /etc/apt/sources.list
deb http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse
更新源
apt update
1.3 update-alternatives 管理 python 版本
(1)查看python默认版本
python --version
(2)查看系统中python版本
ls /usr/bin/python*
(3)update-alternatives 配置
sudo update-alternatives --install /usr/bin/python python /usr/bin/python2.7 10
sudo update-alternatives --install /usr/bin/python python /usr/bin/python3.6 20
sudo update-alternatives --install /usr/bin/python python /usr/bin/python3.7 100
(4)查看配置python 版本
update-alternatives --list python
(5)切换python 版本
sudo update-alternatives --config python
2 安装cuda 及cudnn
wget https://mirrors.aliyun.com/nvidia-cuda/ubuntu1804/x86_64/cuda-ubuntu1804.pin 注: 加入环境变量 export PATH=$PATH:/usr/local/cuda/bin sudo vim /etc/profile source /etc/profile (PS:安装cudnn 的时候 注意与cuda 版本对应) 安装cudnn-11.3-linux-x64-v8.2.0.53.tgz () sudo tar -zxvf cudnn-11.3-linux-x64-v8.2.0.53.tgz sudo cp cuda/include/cudnn.h /usr/local/cuda/include sudo cp cuda/lib64/libcudnn* /usr/local/cuda/lib64 sudo chmod a+r /usr/local/cuda/include/cudnn.h sudo chmod a+r /usr/local/cuda/lib64/libcudnn* export PATH=/usr/local/cuda/bin:$PATH export LD_LIBRARY_PATH=/usr/local/cuda/lib64:$LD_LIBRARY_PATH 验证: Nvcc -V |
3、安装PyTorch
3.1 安装pip3
sudo apt install python3-pip
# 验证安装
pip3 --version
修改pip 源
mkdir ~/.pip touch ~/.pip/pip.conf vim ~/.pip/pip.conf [global] index-url=http://mirrors.aliyun.com/pypi/simple/ [install] trusted-host=mirrors.aliyun.com 升级pip sudo python -m pip install --upgrade --force-reinstall pip |
3.2 安装PyTorch-cuda
pip3 install torch==1.9.0+cu111 torchvision==0.10.0+cu111 torchaudio==0.9.0 -f https://download.pytorch.org/whl/torch_stable.html --no-cache-dir
Wsl2会出现 安装程序被kill 情况,原因未知
3.3 conda installPpyTorch
3.3.1 安装miniconda
安装:
bash ./Miniconda3-latest-Linux-x86_64.sh4
conda 命令加入环境变量
source .bashrc
3.3.2 安装pytorch
conda install pytorch==1.8.0 torchvision==0.9.0 torchaudio==0.8.0 cudatoolkit=11.1 -c pytorch -c conda-forge
四、实际开发
1、vs code 连接wsl2 服务器
(1) 安装插件 Remote - WSL 、Python等
(2)创建工程
2、pycharm 连接wsl2 服务器
(1)安装rsync
sudo apt install rsync
(2)配置pycharm环境
File --->Settings
PS: 由于最后选择了miniconda 安装pytorch 所以要找到miniconda3 下的python3
验证程序:
import torch
tensor([[0.7227, 0.6376, 0.1485], [0.6444, 0.4452, 0.6889], [0.7929, 0.2143, 0.0493], [0.7046, 0.9750, 0.5610], [0.4209, 0.9568, 0.4138]])
cuda 是否可用: import torch print(torch.cuda.is_available())
True |
- 对于Windows10系统来说,启动docker之后,右键点击桌面右下角中的docker图标,选择Setting