最近在做毕设的过程中发现windows下的开发环境配置起来好复杂,遂想到用wsl2来配置开发环境,但是在使用过程中也遇到了一些问题,记录下来以供未来参考

安装

第一步肯定是安装并启用相关功能首先检查系统和硬件支持情况:windows机器需要支持虚拟化,并且需要在BIOS中开启虚拟化技术,因为WSL2基于hyper-V。

1
systeminfo

找到Hyper-V Requirements:如果有4个yes说明一切正常。如果是A hypervisor has been detected. Features required for Hyper-V will not be displayed.也行然后运行以下指令安装并设置wsl默认版本,如果想使用ubuntu发行版就去掉--no-distribution,会默认安装最新的ubuntu lts版本。

1
2
wsl.exe --install --no-distribution
wsl.exe --set-default-version 2
  • 也可以通过wsl --install -d <Distribution Name>。 将 <Distribution Name> 替换为要安装的发行版的名称。
  • 若要查看可通过在线商店下载的可用 Linux 发行版列表,请输入:wsl --list --onlinewsl -l -o
  • 若要在初始安装后安装其他 Linux 发行版,还可使用命令:wsl --install -d <Distribution Name>。安装完成后重启计算机即可

安装位置的迁移

以上方法安装的发行版默认位置应该是c盘,但我不想把它放在C盘

  • 首先通过wsl --shutdown关闭正在运行的子系统。

命令 wsl --shutdown 是重启 WSL 2 发行版的快速路径,但它会关闭所有正在运行的发行版,因此请谨慎使用。 还可以使用 wsl
–terminate 来终止立即运行的特定发行版。

  • 利用wsl --export <DistributionName> <FileName>来备份子系统,结果会得到一个tar归档文件。例如我默认的名为Ubuntu的子系统,想要将结果文件保存为Ubuntu.tar,则调用的命令为wsl --export Ubuntu G:\wsl\Ubuntu.tar
  • 使用wsl --unregister Ubuntu注销原始子系统并删除文件系统(可以之后做)。
  • 然后利用wsl --import <DistributionName> <InstallLocation> <FileName>来恢复子系统。例如wsl --import Ubuntu G:\wsl\Ubuntu\ G:\wsl\Ubuntu.tar(将新恢复的子系统命名为Ubuntu)
  • 设置默认登陆用户为安装时用户名Ubuntu config --default-user <USERNAME>
  • 删除Ubuntu.tar文件del G:\wsl\Ubuntu.tar

如果以上办法没有办法修改默认登陆用户名,可以先进入子系统后vim /etc/wsl.conf然后在里面添加如下内容。将<USERNAME>替换为你的用户名

1
2
[user]
default=<USERNAME>

.wslconfig 配置

需要对wsl做一些自定义配置

wsl.conf 和 .wslconfig 之间有什么差别?

可以为已安装的 Linux 发行版配置设置,使它们在你每次启动 WSL 时自动应用,有两种方法:

  • .wslconfig 用于在 WSL 2 上运行的所有已安装发行版中配置全局设置。
  • wsl.conf 用于为在 WSL 1 或 WSL 2 上运行的每个 Linux 发行版按各个发行版配置本地设置。具体参考官方文档

.wslconfig的配置

参考配置

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
[wsl2]
# 默认8G 要分配给 WSL 2 VM 的内存量 GB MB
# memory=4GB

# 逻辑处理器 默认全部
# processors=2

# 交换分局大小 默认windows内存的25%
swap=8GB

# 交换分区的默认windows路径 默认 %USERPROFILE%\AppData\Local\Temp\swap.vhdx
# swapfile=C:\\temp\\wsl-swap.vhdx

# 已弃用!!! 设置使 Windows 能够回收分配给 WSL 2 虚拟机的未使用内存。
# pageReporting=false

# Turn on default connection to bind WSL 2 localhost to Windows localhost. Setting is ignored when networkingMode=mirrored
# localhostforwarding=true

# 嵌套虚拟化,使其他嵌套 VM 能够在 WSL 2 中运行。 仅适用于 Windows 11
nestedVirtualization=false

# Turns on output console showing contents of dmesg when opening a WSL 2 distro for debugging
# debugConsole=true

# 用于在 WSL 中打开或关闭对 GUI 应用程序 (WSLg) 的支持
guiApplications=false

# 如果值为 mirrored,则会启用镜像网络模式。 默认或无法识别的字符串会生成 NAT 网络。
networkingMode=mirrored

# 如果设置为 true,则 Windows 防火墙规则以及特定于 Hyper-V 流量的规则可以筛选 WSL 网络流量。
firewall=true

# 更改将 DNS 请求从 WSL 代理到 Windows 的方式
dnsTunneling=true

# 强制 WSL 使用 Windows 的 HTTP 代理信息
autoProxy=true

# Enable experimental features
[experimental]
# 如果设置为 true,则任何新创建的 VHD 将自动设置为稀疏。
# sparseVhd=false

# 自动释放缓存的内存。 设置为 gradual 以慢速释放,设置为 dropcache 以立即释放缓存的内存。 gradual | dropcache | disabled
autoMemoryReclaim=gradual

修改软件源

添加ppa软件源

ppa:ubuntu-toolchain-r是常用的自定义软件源,提供了工具链的更新,我们把它加上

1
2
sudo add-apt-repository ppa:ubuntu-toolchain-r/test
sudo add-apt-repository ppa:ubuntu-toolchain-r/ppa

修改至镜像源

加速国内访问软件源的速度,修改配置文件中的地址

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
sudo mv /etc/apt/sources.list /etc/apt/sources.list.bak #备份原始文件
sudo touch /etc/apt/sources.list
sudo chmod 777 /etc/apt/sources.list
sudo cat >> /etc/apt/sources.list <<DEV
# 默认注释了源码镜像以提高 apt update 速度,如有需要可自行取消注释
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy main restricted universe multiverse
# deb-src 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-src 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-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-backports main restricted universe multiverse

deb http://security.ubuntu.com/ubuntu/ jammy-security main restricted universe multiverse
# deb-src http://security.ubuntu.com/ubuntu/ jammy-security main restricted universe multiverse

# 预发布软件源,不建议启用
# deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-proposed main restricted universe multiverse
# # deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-proposed main restricted universe multiverse

DEV

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
sudo mv /etc/apt/sources.list.d/ubuntu.sources /etc/apt/sources.list.d/ubuntu.sources.bak #备份原始文件
sudo touch /etc/apt/sources.list.d/ubuntu.sources
sudo chmod 777 /etc/apt/sources.list.d/ubuntu.sources
sudo cat >> /etc/apt/sources.list.d/ubuntu.sources <<DEV
Types: deb
URIs: https://mirrors.tuna.tsinghua.edu.cn/ubuntu
Suites: noble noble-updates noble-backports
Components: main universe restricted multiverse
Signed-By: /usr/share/keyrings/ubuntu-archive-keyring.gpg

## Ubuntu security updates. Aside from URIs and Suites,
## this should mirror your choices in the previous section.
Types: deb
URIs: http://security.ubuntu.com/ubuntu
Suites: noble-security
Components: main universe restricted multiverse
Signed-By: /usr/share/keyrings/ubuntu-archive-keyring.gpg

DEV

还有要对之前添加的ppa软件源进行修改,使用ustc的反代加速,每个ppa都会在/etc/apt/sources.list.d
目录下创建一个list文件。只需要将其中的https://ppa.launchpad.net替换为https://launchpad.proxy.ustclug.org就行。然后更新一下软件

1
sudo apt update &&  sudo apt upgrade

安装编译工具链

sudo apt install build-essential git 安装ubuntu2204默认支持的编译工具链

Ninja 构建工具

我平时使用Ninja会多一点,所以先把它装了

1
sudo apt install ninja-build

gcc12/gcc13

  • sudo apt install aptitude 安装好用的包管理工具 安装后代码里的apt都可以用aptitude替换
  • sudo apt install gcc-12 g++-12 gdb 安装新版本gcc编译器
  • sudo apt install gdb 安装调试器
  • sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-12 60 --slave /usr/bin/g++ g++ /usr/bin/g++-12 将默认编译器切换到gcc12编译器

切换清华源

  • sudo apt install python3 python3-pip 安装python
1
2
python3 -m pip install --upgrade pip
pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple

如果有提示

WARNING: The scripts pip, pip3 and pip3.10 are installed in ‘/home/cs/.local/bin’ which is not on PATH.
Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.
echo 'export PATH=/home/cs/.local/bin:$PATH' >>~/.bashrc

使用中得问题

/sbin/ldconfig.real: /usr/lib/wsl/lib/libcuda.so.1 is not a symbolic link

这是一个文件链接的问题 需要在windows中解决,且需要管理员权限

1
2
3
4
5
6
cd C:\Windows\System32\lxss\lib
rm libcuda.so
rm libcuda.so.1
wsl -e /bin/bash
ln -s libcuda.so.1.1 libcuda.so.1
ln -s libcuda.so.1.1 libcuda.so

或者在cmd中运行以下指令

1
cd C:\Windows\System32\lxss\lib && del libcuda.so && del libcuda.so.1 && mklink libcuda.so libcuda.so.1.1 && mklink libcuda.so.1 libcuda.so.1.1

然后重启wsl

安装cuda

https://docs.nvidia.com/cuda/wsl-user-guide/index.html

https://developer.nvidia.com/cuda-downloads?target_os=Linux&target_arch=x86_64&Distribution=WSL-Ubuntu&target_version=2.0&target_type=deb_local

出现报错nsight-systems-2023.4.4 : Depends: libtinfo5 but it is not installable

1
2
3
wget http://archive.ubuntu.com/ubuntu/pool/universe/n/ncurses/libtinfo5_6.4-2ubuntu0.1_amd64.deb
sudo dpkg -i libtinfo5_6.4-2ubuntu0.1_amd64.deb