尧图网站设计 尧图网站设计YAOTU DESIGN
ARTICLE DETAIL

资讯详情

深耕网站设计与一线实操的经验洞察。

Win11/Win10通用!5分钟搞定WSL2国内镜像源,加速安装CUDA 11.8和PyTorch 2.2

Win11/Win10通用!5分钟搞定WSL2国内镜像源,加速安装CUDA 11.8和PyTorch 2.2 Win11/Win10通用5分钟搞定WSL2国内镜像源加速安装CUDA 11.8和PyTorch 2.2在深度学习开发中环境配置往往是第一个拦路虎。特别是对于国内开发者来说缓慢的下载速度和频繁的连接中断让人抓狂。想象一下你正兴奋地准备开始一个新项目却在环境搭建阶段就被卡住几个小时——这种体验实在太糟糕了。本文将为你提供一套完整的WSL2国内镜像源配置方案从系统级软件源到Python包管理一站式解决所有网络问题。无论你是要安装CUDA 11.8还是PyTorch 2.2这套方法都能让你的下载速度飞起来把原本可能耗费数小时的安装过程压缩到几分钟完成。1. WSL2基础环境准备1.1 安装Ubuntu 22.04 LTS首先确保你已经在Windows系统中启用了WSL2功能。如果你尚未安装Ubuntu 22.04可以通过以下步骤快速完成以管理员身份打开PowerShell运行wsl --install -d Ubuntu-22.04安装完成后首次启动会提示设置用户名和密码建议将默认安装位置从C盘迁移到其他分区特别是SSD空间紧张的情况下迁移WSL实例到D盘的命令示例wsl --export Ubuntu-22.04 D:\wsl\ubuntu_backup.tar wsl --unregister Ubuntu-22.04 wsl --import Ubuntu-22.04 D:\wsl D:\wsl\ubuntu_backup.tar1.2 基础工具安装进入Ubuntu环境后先安装一些必要工具sudo apt update sudo apt upgrade -y sudo apt install -y vim wget curl git build-essential提示所有apt命令都会在本教程后续配置镜像源后获得显著速度提升2. 系统级镜像源配置2.1 替换Ubuntu软件源为国内镜像国内主流镜像源包括清华、阿里云、中科大等。这里以清华源为例备份原有源列表sudo cp /etc/apt/sources.list /etc/apt/sources.list.bak编辑源列表文件sudo vim /etc/apt/sources.list替换为以下内容清华源deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy main restricted universe multiverse deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-updates main restricted universe multiverse deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-backports main restricted universe multiverse deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-security main restricted universe multiverse更新软件包索引sudo apt update sudo apt upgrade -y各镜像源速度对比镜像源更新频率稳定性推荐指数清华每小时★★★★★★★★★★阿里云每2小时★★★★☆★★★★☆中科大每4小时★★★★☆★★★★☆2.2 安装NVIDIA驱动依赖在安装CUDA之前需要确保系统有正确的构建工具sudo apt install -y linux-headers-$(uname -r) build-essential libglvnd-dev3. Python环境加速配置3.1 Conda镜像源配置如果你使用Anaconda或Miniconda同样需要配置国内源生成conda配置文件conda config --set show_channel_urls yes编辑~/.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 -i3.2 Pip镜像源配置为pip设置清华源pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple pip config set global.trusted-host pypi.tuna.tsinghua.edu.cn或者创建~/.pip/pip.conf文件[global] index-url https://pypi.tuna.tsinghua.edu.cn/simple trusted-host pypi.tuna.tsinghua.edu.cn4. CUDA 11.8快速安装4.1 使用国内镜像下载CUDANVIDIA官方下载速度通常较慢我们可以使用国内镜像通过清华镜像下载CUDA 11.8wget https://mirrors.tuna.tsinghua.edu.cn/nvidia/cuda/ubuntu2204/x86_64/cuda-repo-ubuntu2204-11-8-local_11.8.0-520.61.05-1_amd64.deb安装CUDAsudo dpkg -i cuda-repo-ubuntu2204-11-8-local_11.8.0-520.61.05-1_amd64.deb sudo cp /var/cuda-repo-ubuntu2204-11-8-local/cuda-*-keyring.gpg /usr/share/keyrings/ sudo apt update sudo apt install -y cuda4.2 环境变量配置将以下内容添加到~/.bashrc文件末尾export PATH/usr/local/cuda-11.8/bin${PATH::${PATH}} export LD_LIBRARY_PATH/usr/local/cuda-11.8/lib64${LD_LIBRARY_PATH::${LD_LIBRARY_PATH}}然后使配置生效source ~/.bashrc验证安装nvcc --version5. PyTorch 2.2闪电安装5.1 使用国内源安装PyTorch通过配置好的conda源安装PyTorch 2.2变得非常简单conda install pytorch2.2.2 torchvision0.17.2 torchaudio2.2.2 pytorch-cuda11.8 -c pytorch或者使用pip安装pip install torch2.2.2 torchvision0.17.2 torchaudio2.2.2 --index-url https://download.pytorch.org/whl/cu1185.2 验证GPU可用性创建一个简单的Python脚本验证CUDA和PyTorch是否正常工作import torch print(fPyTorch版本: {torch.__version__}) print(fCUDA可用: {torch.cuda.is_available()}) print(f当前设备: {torch.cuda.current_device()}) print(f设备名称: {torch.cuda.get_device_name(0)})预期输出应该显示CUDA可用和你的GPU型号信息。6. 常见问题与优化技巧6.1 网络问题排查如果遇到下载速度慢或连接失败可以尝试检查镜像源是否拼写正确临时关闭防火墙测试尝试不同的镜像源阿里云、中科大等6.2 磁盘空间管理WSL2虚拟机默认会不断膨胀需要定期清理查看磁盘使用情况df -h清理apt缓存sudo apt clean清理conda缓存conda clean --all6.3 性能优化建议在Windows Defender中添加WSL目录的排除项避免在/mnt目录下进行大量IO操作定期执行wsl --shutdown重置WSL实例在实际项目中这套配置让我节省了大量等待时间。特别是在团队协作时新成员按照这个流程配置环境通常能在15分钟内完成全部设置而不再需要反复尝试各种下载方法。
返回列表