|
| 1 | +--- |
| 2 | +title: 从零开始配置WSL2下的Python开发环境,看这一篇就够了 |
| 3 | +author: me |
| 4 | +date: 2024-09-16 20:00:00 +0800 |
| 5 | +categories: [Handbook, Configuration] |
| 6 | +tags: [Ubuntu, WSL, Python, VSCode] |
| 7 | +--- |
| 8 | + |
| 9 | +## 零、WSL2 简介 |
| 10 | + |
| 11 | +WSL2 是 Windows Subsystem for Linux 的第二代,是一个在 Windows 上运行 Linux 的子系统。WSL2 与 WSL1 最大的区别在于 WSL2 使用了真正的 Linux 内核,而 WSL1 则是通过 Windows 内核的适配层来运行 Linux。所以 WSL2 的性能更接近于真正的 Linux 系统。 |
| 12 | + |
| 13 | +## 一、安装 WSL2 |
| 14 | + |
| 15 | +### 1. 启用 WSL 功能 |
| 16 | + |
| 17 | +方法一:在 PowerShell 中执行以下两条命令,分别启用 WSL 和虚拟机功能: |
| 18 | + |
| 19 | +```powershell |
| 20 | +dism.exe /online /enable-feature /featurename:Microsoft-Windows-Subsystem-Linux /all /norestart |
| 21 | +dism.exe /online /enable-feature /featurename:VirtualMachinePlatform /all /norestart |
| 22 | +``` |
| 23 | + |
| 24 | +方法二:在“运行”中输入 `optionalfeatures` 打开“Windows 功能”,勾选“适用于 Linux 的 Windows 子系统”和“虚拟机平台”。 |
| 25 | + |
| 26 | +之后按照要求重启电脑。 |
| 27 | + |
| 28 | +### 2. 安装 WSL2 |
| 29 | + |
| 30 | +1. 下载并安装 [WSL2 Linux 内核更新包](https://wslstorestorage.blob.core.windows.net/wslblob/wsl_update_x64.msi)。这是为了将 WSL1 升级为 WSL2,否则在 WSL2 下安装 Linux 发行版时会出现错误。 |
| 31 | + |
| 32 | +2. 在 PowerShell 中执行以下命令,将 WSL2 设置为默认版本: |
| 33 | + |
| 34 | +```powershell |
| 35 | +wsl --set-default-version 2 |
| 36 | +``` |
| 37 | + |
| 38 | +### 3. 安装 Linux 发行版 |
| 39 | + |
| 40 | +在 PowerShell 中输入 `wsl -l -o` 查看可用的 Linux 发行版: |
| 41 | + |
| 42 | +```powershell |
| 43 | +$ wsl -l -o |
| 44 | +以下是可安装的有效分发的列表。 |
| 45 | +请使用“wsl --install -d <分发>”安装。 |
| 46 | +
|
| 47 | +NAME FRIENDLY NAME |
| 48 | +Ubuntu Ubuntu |
| 49 | +Debian Debian GNU/Linux |
| 50 | +kali-linux Kali Linux Rolling |
| 51 | +Ubuntu-18.04 Ubuntu 18.04 LTS |
| 52 | +Ubuntu-20.04 Ubuntu 20.04 LTS |
| 53 | +Ubuntu-22.04 Ubuntu 22.04 LTS |
| 54 | +Ubuntu-24.04 Ubuntu 24.04 LTS |
| 55 | +OracleLinux_7_9 Oracle Linux 7.9 |
| 56 | +OracleLinux_8_7 Oracle Linux 8.7 |
| 57 | +OracleLinux_9_1 Oracle Linux 9.1 |
| 58 | +openSUSE-Leap-15.6 openSUSE Leap 15.6 |
| 59 | +SUSE-Linux-Enterprise-15-SP5 SUSE Linux Enterprise 15 SP5 |
| 60 | +SUSE-Linux-Enterprise-15-SP6 SUSE Linux Enterprise 15 SP6 |
| 61 | +openSUSE-Tumbleweed openSUSE Tumbleweed |
| 62 | +``` |
| 63 | + |
| 64 | +选择一个发行版,执行 `wsl --install -d <NAME>` 命令安装。例如,安装 Ubuntu 24.04: |
| 65 | + |
| 66 | +```powershell |
| 67 | +wsl --install -d Ubuntu-24.04 |
| 68 | +``` |
| 69 | + |
| 70 | +接下来在弹出的窗口中设置用户名和密码,安装完成后即可使用。 |
| 71 | + |
| 72 | +## 二、配置 WSL2 |
| 73 | + |
| 74 | +### 1. 更换国内镜像源 |
| 75 | + |
| 76 | +在 WSL2 中,可以更换软件源为国内镜像(如清华源),提高软件下载速度。 |
| 77 | + |
| 78 | +- 清华源:[Ubuntu 软件仓库](https://mirrors.tuna.tsinghua.edu.cn/help/ubuntu/) |
| 79 | + |
| 80 | +> 在 Ubuntu 24.04 之前,Ubuntu 的软件源配置文件使用传统的 One-Line-Style,路径为 `/etc/apt/sources.list`; |
| 81 | +> |
| 82 | +> 从 Ubuntu 24.04 开始,Ubuntu 的软件源配置文件变更为 DEB822 格式,路径为 `/etc/apt/sources.list.d/ubuntu.sources`。 |
| 83 | +{: .prompt-tip } |
| 84 | + |
| 85 | +#### Ubuntu 24.04 及之前版本 |
| 86 | + |
| 87 | +如果你安装的是 Ubuntu 24.04 之前的版本,可以复制清华源的 One-Line-Style 配置到 `/etc/apt/sources.list`: |
| 88 | + |
| 89 | +```bash |
| 90 | +cd /etc/apt/ |
| 91 | +sudo cp sources.list sources.list.bak |
| 92 | +vim sources.list |
| 93 | +``` |
| 94 | + |
| 95 | +点击 `i` 进入输入模式,之后将清华源的 One-Line-Style 配置粘贴到 `sources.list` 文件中,点击 `<ESC>`,输入 `:wq` 保存退出。 |
| 96 | + |
| 97 | +或者直接在 Windows 系统的资源管理器中打开 `\\wsl$\<你的WSL系统名称,如Ubuntu-24.04>\etc\apt` 文件夹,编辑 `sources.list` 文件。 |
| 98 | + |
| 99 | +#### Ubuntu 24.04 及之后版本 |
| 100 | + |
| 101 | +如果你安装的是最新的 Ubuntu 24.04,可以复制清华源的 DEB822 格式配置到 `/etc/apt/sources.list.d/ubuntu.sources`: |
| 102 | + |
| 103 | +```bash |
| 104 | +cd /etc/apt/sources.list.d/ |
| 105 | +sudo cp ubuntu.sources ubuntu.sources.bak |
| 106 | +vim ubuntu.sources |
| 107 | +``` |
| 108 | + |
| 109 | +点击 `i` 进入输入模式,之后将清华源的 DEB822 格式配置粘贴到 `ubuntu.sources` 文件中,点击 `<ESC>`,输入 `:wq` 保存退出。 |
| 110 | + |
| 111 | +或者直接在 Windows 系统的资源管理器中打开 `\\wsl$\<你的WSL系统名称,如Ubuntu-24.04>\etc\apt\sources.list.d` 文件夹,编辑 `ubuntu.sources` 文件。 |
| 112 | + |
| 113 | +### 2. 安装 Anaconda |
| 114 | + |
| 115 | +在 WSL2 中安装 Anaconda,可以方便地管理 Python 环境。 |
| 116 | + |
| 117 | +- 清华源:[Anaconda 安装包](https://mirrors.tuna.tsinghua.edu.cn/anaconda/archive/) |
| 118 | +- 清华源:[Anaconda 软件仓库](https://mirrors.tuna.tsinghua.edu.cn/help/anaconda/) |
| 119 | + |
| 120 | +#### 下载安装包 |
| 121 | + |
| 122 | +在清华源的 Anaconda 安装包页面中,选择 `Linux-x86_64` 体系下的最新版本下载,如 `Anaconda3-2024.06-1-Linux-x86_64.sh`(可以点击Date右侧的箭头按时间排序)。 |
| 123 | + |
| 124 | +在浏览器复制下载链接,使用 `wget` 命令下载: |
| 125 | + |
| 126 | +```bash |
| 127 | +cd ~ |
| 128 | +wget https://mirrors.tuna.tsinghua.edu.cn/anaconda/archive/Anaconda3-2024.06-1-Linux-x86_64.sh |
| 129 | +``` |
| 130 | + |
| 131 | +运行安装脚本: |
| 132 | + |
| 133 | +```bash |
| 134 | +bash Anaconda3-2024.06-1-Linux-x86_64.sh |
| 135 | +``` |
| 136 | + |
| 137 | +按照提示安装 Anaconda,安装完成后可以使用 `conda` 命令。 |
| 138 | + |
| 139 | +#### 配置 Anaconda 国内镜像源 |
| 140 | + |
| 141 | +在清华源的 Anaconda 软件仓库页面中,找到更换为清华源推荐的配置内容: |
| 142 | + |
| 143 | +```yaml |
| 144 | +channels: |
| 145 | + - defaults |
| 146 | +show_channel_urls: true |
| 147 | +default_channels: |
| 148 | + - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main |
| 149 | + - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/r |
| 150 | + - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/msys2 |
| 151 | +custom_channels: |
| 152 | + conda-forge: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud |
| 153 | + pytorch: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud |
| 154 | +``` |
| 155 | +
|
| 156 | +将上面的配置粘贴到 `~/.condarc` 文件中: |
| 157 | + |
| 158 | +```bash |
| 159 | +vim ~/.condarc |
| 160 | +``` |
| 161 | + |
| 162 | +点击 `i` 进入输入模式,之后将清华源的配置粘贴到 `.condarc` 文件中,点击 `<ESC>`,输入 `:wq` 保存退出。 |
| 163 | + |
| 164 | +### 3. 创建 PyTorch 环境 |
| 165 | + |
| 166 | +在 WSL2 中使用 Anaconda 创建 PyTorch 环境。 |
| 167 | + |
| 168 | +先查看当前系统的 CUDA 版本: |
| 169 | + |
| 170 | +```bash |
| 171 | +$ nvidia-smi |
| 172 | +Mon Sep 16 18:28:37 2024 |
| 173 | ++---------------------------------------------------------------------------------------+ |
| 174 | +| NVIDIA-SMI 545.46 Driver Version: 546.80 CUDA Version: 12.3 | |
| 175 | +|-----------------------------------------+----------------------+----------------------+ |
| 176 | +| GPU Name Persistence-M | Bus-Id Disp.A | Volatile Uncorr. ECC | |
| 177 | +| Fan Temp Perf Pwr:Usage/Cap | Memory-Usage | GPU-Util Compute M. | |
| 178 | +| | | MIG M. | |
| 179 | +|=========================================+======================+======================| |
| 180 | +| 0 NVIDIA GeForce RTX 4060 ... On | 00000000:01:00.0 Off | N/A | |
| 181 | +| N/A 37C P8 1W / 140W | 76MiB / 8188MiB | 0% Default | |
| 182 | +| | | N/A | |
| 183 | ++-----------------------------------------+----------------------+----------------------+ |
| 184 | +
|
| 185 | ++---------------------------------------------------------------------------------------+ |
| 186 | +| Processes: | |
| 187 | +| GPU GI CI PID Type Process name GPU Memory | |
| 188 | +| ID ID Usage | |
| 189 | +|=======================================================================================| |
| 190 | +| No running processes found | |
| 191 | ++---------------------------------------------------------------------------------------+ |
| 192 | +``` |
| 193 | + |
| 194 | +说明当前系统的 CUDA 版本为 12.3。接着在 PyTorch 官网查看最接近当前 CUDA 版本的安装命令。 |
| 195 | + |
| 196 | +- PyTorch 官网:[PyTorch 安装指南](https://pytorch.org/get-started/locally/) |
| 197 | + |
| 198 | +```bash |
| 199 | +conda create -n my_pytorch_env python=3.9 |
| 200 | +conda activate my_pytorch_env |
| 201 | +conda install pytorch torchvision torchaudio pytorch-cuda=12.4 -c pytorch -c nvidia # 此处更换为你在 PyTorch 官网查到的安装命令 |
| 202 | +``` |
| 203 | + |
| 204 | +安装完成后,可以使用 `python` 命令进入 Python 环境,使用 `import torch` 检查 PyTorch 是否安装成功。使用 `torch.cuda.is_available()` 检查是否支持 GPU 加速。 |
| 205 | + |
| 206 | +``` |
| 207 | +>>> import torch |
| 208 | +>>> torch.cuda.is_available() |
| 209 | +True |
| 210 | +``` |
| 211 | +
|
| 212 | +## 三、配置 VSCode |
| 213 | +
|
| 214 | +点击左下角的远程连接图标,选择“连接到 WSL”,选择你的 WSL 系统,VSCode 将会自动连接到 WSL2 中的 Linux 系统。按照要求安装插件,即可在 VSCode 中编写 Python 代码。 |
| 215 | +
|
| 216 | + |
| 217 | +
|
| 218 | +## 四、配置 GitHub SSH Key |
| 219 | +
|
| 220 | +由于 GitHub 更新了安全策略,需要使用 SSH Key 连接 GitHub。我们需要在 WSL2 中生成 SSH Key 并手动添加到 GitHub,方可使用 Git 命令。 |
| 221 | +
|
| 222 | +### 1. 生成 SSH Key 密钥对 |
| 223 | +
|
| 224 | +在 WSL2 中执行以下命令生成 SSH Key 密钥对: |
| 225 | +
|
| 226 | +```bash |
| 227 | +ssh-keygen -t rsa -C "<[email protected]>" |
| 228 | +``` |
| 229 | + |
| 230 | +按照提示输入文件名和密码(均可留空,直接回车即可),生成 SSH Key。 |
| 231 | + |
| 232 | +### 2. 添加 SSH Key 私钥到 SSH Agent |
| 233 | + |
| 234 | +```bash |
| 235 | +eval "$(ssh-agent -s)" |
| 236 | +ssh-add ~/.ssh/id_rsa |
| 237 | +``` |
| 238 | + |
| 239 | +### 3. 添加 SSH Key 公钥到 GitHub |
| 240 | + |
| 241 | +查看生成的 SSH RSA 公钥: |
| 242 | + |
| 243 | +```bash |
| 244 | +cat ~/.ssh/id_rsa.pub |
| 245 | +``` |
| 246 | + |
| 247 | +RSA 公钥格式为: |
| 248 | + |
| 249 | +``` |
| 250 | +ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQDfZt... [email protected] |
| 251 | +``` |
| 252 | + |
| 253 | +将输出的公钥添加到到 GitHub 的 SSH Key 设置中。 |
| 254 | + |
| 255 | +具体位置在:GitHub -> Settings -> [SSH and GPG keys](https://github.com/settings/keys) -> New SSH Key,将公钥粘贴到 Key 中,点击 Add Key 即可。 |
| 256 | + |
| 257 | +### 4. 测试 SSH Key 连接 |
| 258 | + |
| 259 | +测试 SSH Key 是否配置成功: |
| 260 | + |
| 261 | +```bash |
| 262 | + |
| 263 | +``` |
| 264 | + |
| 265 | +如果出现 `Hi <your_username>! You've successfully authenticated, but GitHub does not provide shell access.` 说明配置成功。 |
| 266 | + |
| 267 | +## 参考链接 |
| 268 | + |
| 269 | +- [WSL2 官方文档](https://docs.microsoft.com/zh-cn/windows/wsl/) |
| 270 | +- [清华大学开源软件镜像站](https://mirrors.tuna.tsinghua.edu.cn/) |
| 271 | +- [PyTorch 安装指南](https://pytorch.org/get-started/locally/) |
| 272 | +- [GitHub SSH Key 设置](https://docs.github.com/en/github/authenticating-to-github/connecting-to-github-with-ssh) |
| 273 | +- [VSCode 连接 WSL2](https://code.visualstudio.com/docs/remote/wsl) |
0 commit comments