配置 WSL 开发环境
2022-11-10

背景

安装步骤

启用虚拟化和WSL

启用或关闭Windows功能

完成之后需要重启。

启用WSL
1
wsl --install

完成之后可能需要重启。

安装Windows Terminal

在 Microsoft Store (应用商城)中搜索 Windows Terminal 即可一键安装,此处省。

安装Ubuntu

有多种方式可以安装 Ubuntu。最简单的方式,就是和上一步一样,在 Microsoft Store (应用商城)中搜索 Ubuntu 即可一键安装,此处省。

配置 apt 国内软件源镜像
1
2
sudo cp /etc/apt/sources.list /etc/apt/sources.list.20221110.bak # 备份是个好习惯
sudo vim /etc/apt/sources.list # 替换国内镜像源以加速,根据 Ubuntu 不同版本号,选择不同的路径

因为我在上海,使用的又是 Ubuntu 22.04,所以我的配置如下:

1
2
3
4
5
6
7
8
9
10
11
deb https://mirror.sjtu.edu.cn/ubuntu/ jammy main restricted universe multiverse
# deb-src https://mirror.sjtu.edu.cn/ubuntu/ jammy main restricted universe multiverse
deb https://mirror.sjtu.edu.cn/ubuntu/ jammy-updates main restricted universe multiverse
# deb-src https://mirror.sjtu.edu.cn/ubuntu/ jammy-updates main restricted universe multiverse
deb https://mirror.sjtu.edu.cn/ubuntu/ jammy-backports main restricted universe multiverse
# deb-src https://mirror.sjtu.edu.cn/ubuntu/ jammy-backports main restricted universe multiverse
deb https://mirror.sjtu.edu.cn/ubuntu/ jammy-security main restricted universe multiverse
# deb-src https://mirror.sjtu.edu.cn/ubuntu/ jammy-security main restricted universe multiverse

# deb https://mirror.sjtu.edu.cn/ubuntu/ jammy-proposed main restricted universe multiverse
# deb-src https://mirror.sjtu.edu.cn/ubuntu/ jammy-proposed main restricted universe multiverse
初始化

安装完成之后,打开 Windows Terminal,选择 Ubuntu:

Windows Terminal

第一次打开会提示设置用户名密码(超级用户权限)。

Windows 环境必要软件安装

根据各开发者技术栈不同,需要安装的软件会有所不同。包括但不限于:Docker DesktopVisual Studio CodeRemote Development Extension Pack

WSL 环境必要软件安装

根据各开发者技术栈不同,需要安装的软件会有所不同。包括但不限于:gitnode.jspython

GitHub 访问加速

其实,现在好多办法都可以加速访问 GitHub,我这里介绍一种特别原始的办法,就是修改 hosts 文件。

  1. 以管理员权限,打开记事本
  2. 记事本中打开 C:\Windows\System32\drivers\etc\hosts 文件
  3. 添加以下 IP 映射:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# 需要定期更新如下代码
# New!欢迎使用基于DNS的新方案
# https://gitlab.com/ineo6/hosts/-/raw/master/next-hosts
# 地址可能会变动,请务必关注GitHub、Gitlab获取最新消息
# 也可以关注公众号:湖中剑,保证不迷路
# GitHub Host Start

140.82.114.3 central.github.com
140.82.114.3 assets-cdn.github.com
151.101.1.6 github.map.fastly.net
151.101.1.6 github.global.ssl.fastly.net
140.82.114.3 gist.github.com
185.199.108.153 github.io
140.82.114.3 github.com
140.82.114.3 api.github.com
140.82.114.3 codeload.github.com
72.21.206.80 github-cloud.s3.amazonaws.com
72.21.206.80 github-com.s3.amazonaws.com
72.21.206.80 github-production-release-asset-2e65be.s3.amazonaws.com
72.21.206.80 github-production-user-asset-6210df.s3.amazonaws.com
72.21.206.80 github-production-repository-file-5c1aeb.s3.amazonaws.com
185.199.108.153 githubstatus.com
140.82.113.18 github.community
140.82.114.3 raw.github.com

# Please Star : https://github.com/ineo6/hosts
# Mirror Repo : https://gitlab.com/ineo6/hosts

# Update at: 2022-11-10 16:20:33

# GitHub Host End
  1. 运行命令 ipconfig /flushdns 刷新 DNS。

当然,也有其他的提供最新 hosts(比如 https://raw.hellogithub.com/hosts ) 或者自动 switchhosts(比如 https://github.com/oldj/SwitchHosts ),或者镜像 github 网站,或者其他科学上网方式,感兴趣的请自行搜索。

Github SSH 配置
  1. 生成 ssh 公钥和私钥(如果没有的话) Generating a new SSH key and adding it to the ssh-agent
  2. 添加公钥至 Github 账户 Adding a new SSH key to your GitHub account
  3. 更改 Repository 至 SSH 协议。
1
2
3
4
5
6
7
8
# Deployment
## Docs: https://hexo.io/docs/deployment.html
deploy:
type: 'git'
# repo: https://github.com/ZXS66/ZXS66.github.io.git
repo: git@github.com:ZXS66/ZXS66.github.io.git
branch: master
message: "Blog committed on {{ now('YYYY/MM/DD HH:mm') }}"

参考链接

本文链接:
content_copy https://zxs66.github.io/2022/11/10/setup-wsl-development-environment/