分享免费的编程资源和教程

网站首页 > 技术教程 正文

wsl2安装ubuntu24.04 wsl2安装教程

goqiw 2024-10-23 11:36:53 技术教程 78 ℃ 0 评论

在windows上安装linux子系统wsl2,然后安装ubuntu等发行版,不用再用虚拟机来运行linux了。

  • 性能可以接受,比裸机略慢但不明显。
  • 和Windows集成紧密,vscode等集成,用起来很方便。
  • 升级到Windows11之后图形界面和音频都有支持,性能尚可,除了重度3D程序之外都不算慢。
  • 宿主机有Nvidia显卡的话可以用CUDA

wsl发展了很长一段时间,最新的wsl2已经相对稳定,因此最好安装较新的win11系统,我的版本如下,win11 23H2,

1.手动安装wsl2

以管理员身份启动powershell(Start menu > PowerShell > right-click > Run as Administrator) ,执行以下2个命令使能wsl和vm,

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

然后,重启电脑

下载wsl2 linux kernel相关的更新安装包,链接为https://wslstorestorage.blob.core.windows.net/wslblob/wsl_update_x64.msi。安装后,将wls2设置为默认的版本,

wsl --set-default-version 2

2.安装ubuntu 24.04

列出目前可安装的版本,

wsl --list -o

输出为,

PS C:\Users\lyf> wsl --list -o
以下是可安装的有效分发的列表。
请使用“wsl --install -d <分发>”安装。
NAME FRIENDLY NAME
Ubuntu Ubuntu
Debian Debian GNU/Linux
kali-linux Kali Linux Rolling
Ubuntu-18.04 Ubuntu 18.04 LTS
Ubuntu-20.04 Ubuntu 20.04 LTS
Ubuntu-22.04 Ubuntu 22.04 LTS
Ubuntu-24.04 Ubuntu 24.04 LTS
OracleLinux_7_9 Oracle Linux 7.9
OracleLinux_8_7 Oracle Linux 8.7
OracleLinux_9_1 Oracle Linux 9.1
openSUSE-Leap-15.5 openSUSE Leap 15.5
SUSE-Linux-Enterprise-Server-15-SP4 SUSE Linux Enterprise Server 15 SP4
SUSE-Linux-Enterprise-15-SP5 SUSE Linux Enterprise 15 SP5
openSUSE-Tumbleweed openSUSE Tumbleweed

安装,

wsl.exe --install -d Ubuntu-24.04

安装完后设置用户名和密码,我的用户名为lyf,后续需要用。

3.修改wsl安装位置

将wsl的安装位置更改到e:\wsl目录下,不占用c盘空间,安装完成后,ubuntu 24.04 已经在运行,

wsl -l -v


wsl --shutdown
# 将ubuntu 24.04导出到e盘e:\ubuntu24.04.tar
wsl --export Ubuntu-24.04 e:\ubuntu24.04.tar
# 删除已经安装的Ubuntu-24.04
wsl --unregister Ubuntu-24.04
# 导入Ubuntu-24.04到E:\wsl目录
wsl --import Ubuntu-24.04 E:\wsl e:\ubuntu24.04.tar
# 配置默认用户
ubuntu2404.exe config --default-user lyf

至此,ubuntu 24.04系统相关的文件已经都转移到了e:/wsl/下的ext4

4.系统配置

修改C:/User/%你的用户名/.wslconfig,如果没有则创建,注意打开文件扩展名和隐藏的文件,

在文件中添加如下的配置,

[experimental]
autoMemoryReclaim=gradual
networkingMode=mirrored
dnsTunneling=true
firewall=true
autoProxy=true

autoMemoryReclaim

Automatically releases cached memory after detecting idle CPU usage. Set to gradual for slow release, and dropcache for instant release of cached memory.

When this is set to gradual, after being idle for 5 minutes, WSL will slowly start to release cached memory in Linux and make it available as free memory back to the Windows host. This means that your WSL VM will automatically shrink in memory size when you’re not using it!

autoMemoryReclaim,wsl在不使用时,内存的自动回收。

networkingMode,默认是NAT,如果是mirrored,wls的网络和主机基本可以理解为一个网络,不需要NAT了。

If the value is mirrored then this turns on mirrored networking mode. Default or unrecognized strings result in NAT networking.

  • dnsTunneling – Changes how WSL resolves DNS requests to improve network compatibility;
  • firewall – Applies Windows firewall rules to WSL, and allows for advanced firewall controls for the WSL VM;
  • autoProxy – Makes WSL automatically use the proxy information from Windows to improve network compatibility

修改ubuntu 24.04的源为国内中科大的源,注意,ubuntu 24.04源相关的文件和之前的版本有变化,

sudo sed -i 's@//.*archive.ubuntu.com@//mirrors.ustc.edu.cn@g' /etc/apt/sources.list.d/ubuntu.sources
sudo sed -i 's/security.ubuntu.com/mirrors.ustc.edu.cn/g' /etc/apt/sources.list.d/ubuntu.sources
sudo sed -i 's/http:/https:/g' /etc/apt/sources.list.d/ubuntu.sources
sudo apt update

本文暂时没有评论,来添加一个吧(●'◡'●)

欢迎 发表评论:

最近发表
标签列表