wsl2环境配置+docker+wt+vscode

序言

最近新换的电脑又坏了,拿回来还得重新配置环境,还得挨个找教程进行配置。这次就简单地全写一起吧。

作为wsl的忠实爱好者,我已经使用了wsl两年了,相比虚拟机来说,实在是不要方便太多了,而且配置要求也没这么高。

之前windows 商店也发布了wt(windows terminal),相比cmdpowershell以及原本bash漂亮太多了,而且集成了多个窗口,切换很方便。

这是我现在的界面,大家可以自己定义其他的外观等等。

overview

最近看了一下,居然wsl2已经出好久了,还支持docker。我居然才知道,我想了想那时我应该在准备考研,emmm 。好了,废话不多说我们开始配置。

另外说一句,wsl2还没有推送到正式版的windows 10中,目前还是在windows的测试版中。

对比

比较 WSL 2 和 WSL 1

wsl1wsl2底层架构不同,所以wsl2能相比wsl1更多的实现一些linux内的功能。包括这次的docker,同时在windows 10最新的测试版中已经提供了GPU计算,有兴趣的可以自己升到最新的版本使用。

开始配置

更新windows 10

wsl2只有在windows 10版本2004的内部版本19041或者更高的版本中有提供

  • 你可以win+R中输入winver中查看windows的版本
    • winver
  • 如果你的版本没有达到要求的话,你可以先试图通过windows 10的系统更新。
  • 如果依旧没达到版本要求的话:
    • 加入微软的windows 预览体院计划,并选择Beta渠道
    • preview
    • 进行windows 10的系统更新

系统设置

如果你没有安装过wsl

那么,你必须先启用适用于 Linux 的 Windows 子系统功能。

以管理员身份打开 PowerShell 并运行:

1
dism.exe /online /enable-feature /featurename:Microsoft-Windows-Subsystem-Linux /all /norestart

并重启电脑

如果你安装过wsl

安装 WSL 2 之前,必须启用虚拟机平台功能。

以管理员身份打开 PowerShell 并运行:

1
dism.exe /online /enable-feature /featurename:VirtualMachinePlatform /all /norestart

重新启动计算机,以完成 WSL 安装并更新到 WSL 2。

将wsl2设置为默认版本

在powershell中输入:

1
wsl --set-default-version 2

你可能会遇到错误信息:WSL 2 requires an update to its kernel component. For information please visit https://aka.ms/wsl2kernel
请安装wsl2kernel

wsl1 to wsl2

先查看wsl的版本信息

1
2
3
wsl --list --verbose
# or
wsl -l -v

如果你是版本1的话,切换至版本2

1
2
3
wsl --set-version -name> 2
# in my case
wsl --set-version Ubuntu 2

从wsl1切换到wsl2可能会有错误,可能这能够解决卸载并删除任何旧分发

安装 linux 发行版与 windows terminal

Microsoft Store搜索windows terminal与想要的linux 发行版

windows-ternimal

你可以搜索你想要的 linux 发行版,基本上都有

  • Ubuntu 16.04 LTS
  • Ubuntu 18.04 LTS
  • Ubuntu 20.04 LTS
  • openSUSE Leap 15.1
  • SUSE Linux Enterprise Server 12 SP5
  • SUSE Linux Enterprise Server 15 SP1
  • Kali Linux
  • Debian GNU/Linux
  • Fedora Remix for WSL
  • Pengwin
  • Pengwin Enterprise
  • Alpine WSL

linux环境配置

我安装的是ubuntu18.04,所以接下来的流程都在该发行版下进行。

在使用wt中使用发行版时,先启动该发行版完成预安装和预配置。完成安装之后便可以通过win+r输入wt,打开windows ternimal点击+号选择你安装的发行版,就可以了,wt的配置我会在后面贴出。

换源

1
2
sudo cp /etc/apt/sources.list /etc/apt/sources.list.bak
sudo vim /etc/apt/sources.list

清空里面所有内容,换入国内源。

例如阿里源:

1
2
3
4
5
6
7
8
9
10
deb http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse
deb 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 main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse

更新source包,安装更新,开发基础内容包

1
2
3
sudo apt-get update
sudo apt-get upgrade
sudo apt-get install build-essential

zsh

将zsh替代bash

1
sudo apt install zsh # 安装zsh

更改默认的shell

1
chsh -s $(which zsh)

下次再打开分发版时默认就是zsh

Oh My ZSH

oh my zsh是一个开源的社区驱动的框架,用于管理zsh配置。

下载安装:

1
sh -c "$(curl -fsSL https://raw.github.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"

zsh的配置文件在

1
vim ~/.zshrc

可以更改主题

1
2
3
4
# 默认主题
ZSH_THEME="robbyrussell"
# 新主题
ZSH_THEME="agnoster"

可以安装插件Syntax Highlighting, Auto Suggestions
分别是语法高亮和自动补齐

1
2
3
4
mkdir ~/.zsh
cd ~/.zsh
git clone https://github.com/zsh-users/zsh-syntax-highlighting.git
git clone https://github.com/zsh-users/zsh-autosuggestions.git

同时wt里面文件夹名比较丑,下载dircolors

1
curl https://raw.githubusercontent.com/seebi/dircolors-solarized/master/dircolors.ansi-dark --output ~/.dircolors

然后

1
vim ~/.zshrc

在末尾加上

1
2
3
source ~/.zsh/zsh-autosuggestions/zsh-autosuggestions.zsh
source ~/.zsh/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh
eval `dircolors ~/.dircolors`

这样的话在每一次开启终端,都会启用插件

proxychains

在wsl2中的网络有了新的变化,系统会生成一个虚拟网卡,也就是说,在windows上会多一个网卡,且Ip是变化的。

所以在linux中它的hosts是自动生成的、沿袭原本windows内容的hostsnameserver也是一样的。

所以window上的小飞机是无法直接给wsl2使用的,但是小飞机可以走本地端口代理。

而proxychains就是这样以一个工具可以利用http协议,走1080端口本地代理。

1
sudo apt-get install proxychains

它的配置文件在

1
cat /etc/proxychains.conf

因为wsl2的ip是变化的,host的ip也是变化的
所以要让proxychains.conf的代理信息自动变化

1
2
3
4
cd ~/.zsh
mkdir proxy
cd proxy
vim proxy.sh

输入以下内容

1
2
3
4
5
6
7
#!/bin/sh
export HOSTIP=$(cat /etc/resolv.conf | grep 'nameserver' | cut -f 2 -d ' ')
export WSLIP=$(ip addr show eth0 | grep 'inet ' | cut -f 6 -d ' ' | cut -f 1 -d '/')
echo HOSTIP $HOSTIP
echo WSLIP $WSLIP

echo Your password | sudo -S sed -i "/http/c http\t$HOSTIP 1080" /etc/proxychains.conf

保存退出后

1
vim ~/.zshrc

输入

1
source ~/.zsh/proxy/proxy.sh

以后只要想要下载某些外网内容就可以直接在前面加proxychains

1
2
3
4
# 例如
sudo proxychains apt-get update
sudo proxychains apt-get upgrade
proxychian git clone ......

Nodejs

方法一:利用n安装nodejs

1
2
3
4
5
sudo apt-get install nodejs
sudo apt-get install npm
sudo npm install -g npm
sudo npm install -g n
sudo n stable //稳定版

方法二:利用nvm管理nodejs

1
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.34.0/install.sh | bash

之后重启wt

输入

1
2
3
4
5
6
command -v nvm
# return nvm

# 安装node
nvm install node # 最新版本
nvm use node

Docker

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
# 更新索引包依赖
sudo apt update
sudo apt install apt-transport-https ca-certificates curl software-properties-common

# 添加官方的GPG-key
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -


# 验证
sudo apt-key fingerprint 0EBFCD88
# 答案 9DC8 5822 9FC7 DD38 854A E2D8 8D81 803C 0EBF CD88

# 添加 Docker repository
sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"

# 安装
sudo apt update
sudo apt install docker-ce

# 启动docker
sudo service docker start


# 测试docker
sudo docker run hello-world

Docker Compose

docker容器的管理工具。

1
2
3
4
5
6
7
8
# 下载当前的稳定发行版并将其放在/usr/local/bin下。
sudo curl -L "https://github.com/docker/compose/releases/download/1.24.0/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose

# 修改权限
sudo chmod +x /usr/local/bin/docker-compose

# 测试
docker-compose --version

Docker Desktop

在windows上下载
Docker Desktop

提供了docker的gui

docker-desktop

连接vscode

请先下载vscode安装插件:remote-wslterminal

remote-wsl
terminal-plugin

这样你就可以通过点击连接wsl

通过ctrl+p输入terminal,选择打开一个内部的teriminal。就可以了。

修改默认的shell为zsh。

字体问题

你可能会遇到某些字无法正常显示。

下载DejaVuSansMono,并安装。

同时在vscode的设置中,将terminal的字体选择为这个名字。

windows terminal

这个是我的配置,不做过多介绍了

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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
// This file was initially generated by Windows Terminal 1.0.1401.0
// It should still be usable in newer versions, but newer versions might have additional
// settings, help text, or changes that you will not see unless you clear this file
// and let us generate a new one for you.

// To view the default settings, hold "alt" while clicking on the "Settings" button.
// For documentation on these settings, see: https://aka.ms/terminal-documentation
{
"$schema": "https://aka.ms/terminal-profiles-schema",

"defaultProfile": "{c6eaf9f4-32a7-5fdc-b5cf-066e8a4b1e40}",

// You can add more global application settings here.
// To learn more about global settings, visit https://aka.ms/terminal-global-settings

// If enabled, selections are automatically copied to your clipboard.
"copyOnSelect": false,

// If enabled, formatted data is also copied to your clipboard
"copyFormatting": false,

// A profile specifies a command to execute paired with information about how it should look and feel.
// Each one of them will appear in the 'New Tab' dropdown,
// and can be invoked from the commandline with `wt.exe -p xxx`
// To learn more about profiles, visit https://aka.ms/terminal-profile-settings
"profiles":
{
"defaults":
{
"fontFace": "DejaVu Sans Mono for Powerline",
"acrylicOpacity": 0.8, //背景透明度
"useAcrylic": true, // 启用毛玻璃
// "backgroundImage": "D:\\OneDrive\\图片\\stack.jpg", //背景图片
// "backgroundImageOpacity": 0.5, //图片透明度
// "backgroundImageStretchMode": "fill", //填充模式
// "icon": "ms-appx:///ProfileIcons/{9acb9455-ca41-5af7-950f-6bca1bc9722f}.png", //图标
"fontSize": 10, //文字大小
"colorScheme": "Solarized Dark", //主题
"cursorColor": "#FFFFFF", //光标颜色
"cursorShape": "bar" //光标形状
// "startingDirectory":"D://Projects//" //起始目录
// Put settings here that you want to apply to all profiles.
},
"list":
[
{
// Make changes here to the powershell.exe profile.
"guid": "{61c54bbd-c2c6-5271-96e7-009a87ff44bf}",
"name": "Windows PowerShell",
"commandline": "powershell.exe",
"hidden": false
},
{
// Make changes here to the cmd.exe profile.
"guid": "{0caa0dad-35be-5f56-a8ff-afceeeaa6101}",
"name": "命令提示符",
"commandline": "cmd.exe",
"hidden": false
},
{
"guid": "{c6eaf9f4-32a7-5fdc-b5cf-066e8a4b1e40}",
"hidden": false,
"name": "Ubuntu-18.04",
"source": "Windows.Terminal.Wsl"
},
{
"guid": "{b453ae62-4e3d-5e58-b989-0a998ec441b8}",
"hidden": false,
"name": "Azure Cloud Shell",
"source": "Windows.Terminal.Azure"
},
{
"guid": "{07b52e3e-de2c-5db4-bd2d-ba144ed6c273}",
"hidden": false,
"name": "Ubuntu-20.04",
"source": "Windows.Terminal.Wsl"
}
]
},

// Add custom color schemes to this array.
// To learn more about color schemes, visit https://aka.ms/terminal-color-schemes
"schemes": [
{
"name": "Solarized Dark",
"black": "#002831",
"red": "#d11c24",
"green": "#738a05",
"yellow": "#a57706",
"blue": "#2176c7",
"purple": "#c61c6f",
"cyan": "#259286",
"white": "#eae3cb",
"brightBlack": "#475b62",
"brightRed": "#bd3613",
"brightGreen": "#475b62",
"brightYellow": "#536870",
"brightBlue": "#708284",
"brightPurple": "#5956ba",
"brightCyan": "#819090",
"brightWhite": "#fcf4dc",
"background": "#001e27",
"foreground": "#708284"
},
{
"name": "Solarized Darcula",
"black": "#25292a",
"red": "#f24840",
"green": "#629655",
"yellow": "#b68800",
"blue": "#2075c7",
"purple": "#797fd4",
"cyan": "#15968d",
"white": "#d2d8d9",
"brightBlack": "#25292a",
"brightRed": "#f24840",
"brightGreen": "#629655",
"brightYellow": "#b68800",
"brightBlue": "#2075c7",
"brightPurple": "#797fd4",
"brightCyan": "#15968d",
"brightWhite": "#d2d8d9",
"background": "#3d3f41",
"foreground": "#d2d8d9"
}
],

// Add custom keybindings to this array.
// To unbind a key combination from your defaults.json, set the command to "unbound".
// To learn more about keybindings, visit https://aka.ms/terminal-keybindings
"keybindings":
[
// Copy and paste are bound to Ctrl+Shift+C and Ctrl+Shift+V in your defaults.json.
// These two lines additionally bind them to Ctrl+C and Ctrl+V.
// To learn more about selection, visit https://aka.ms/terminal-selection
{ "command": {"action": "copy", "singleLine": false }, "keys": "ctrl+c" },
{ "command": "paste", "keys": "ctrl+v" },

// Press Ctrl+Shift+F to open the search box
{ "command": "find", "keys": "ctrl+shift+f" },

// Press Alt+Shift+D to open a new pane.
// - "split": "auto" makes this pane open in the direction that provides the most surface area.
// - "splitMode": "duplicate" makes the new pane use the focused pane's profile.
// To learn more about panes, visit https://aka.ms/terminal-panes
{ "command": { "action": "splitPane", "split": "auto", "splitMode": "duplicate" }, "keys": "alt+shift+d" }
]
}

参考

WSL2, zsh, and docker. Linux through Windows.
WSL2中自动配置Windows IP地址
适用于 Linux 的 Windows 子系统安装指南 (Windows 10)