可可西

Win10下安装WSL(Windows Subsystem for Linux)

启用WSL(Windows Subsystem for Linux)

管理员权限运行powershell并运行下面的命令

C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe

Enable-WindowsOptionalFeature -Online -FeatureName Microsoft-Windows-Subsystem-Linux

 

 

或者:控制面板->程序和功能->启用或关闭Windows功能->勾选 适用于Linux的Windows子系统

 

然后,重启计算机

 

在Microsoft Store中搜索下载安装WSL

C:\Program Files\WindowsApps\Microsoft.WindowsStore_22410.1401.4.0_x64__8wekyb3d8bbwe\WinStore.App.exe

 

通常,对最新Ubuntu版本的支持到下一个版本就会停止。注:每6个月发布一次Ubuntu

对于稳定性和可持续性而言,是一个大问题。为了解决这一问题,官方提供了LTS(Long Term Support,长期支持)版本,有更长的升级周期。

 

win11下使用wsl.exe命令来安装Ubuntu-26.04

可手动从https://github.com/microsoft/WSL/releases/下载最新的wsl工具

wsl --help   // 查看帮助

wsl.exe --list --online   // 查看当前线上可用的wsl包体

以下是可安装的有效分发的列表。
使用“wsl.exe --install <Distro>”安装。

NAME                            FRIENDLY NAME
Ubuntu                          Ubuntu
Ubuntu-26.04                    Ubuntu 26.04 LTS
Ubuntu-24.04                    Ubuntu 24.04 LTS
Ubuntu-22.04                    Ubuntu 22.04 LTS
openSUSE-Tumbleweed             openSUSE Tumbleweed
openSUSE-Leap-16.0              openSUSE Leap 16.0
SUSE-Linux-Enterprise-15-SP7    SUSE Linux Enterprise 15 SP7
SUSE-Linux-Enterprise-16.0      SUSE Linux Enterprise 16.0
kali-linux                      Kali Linux Rolling
Debian                          Debian GNU/Linux
AlmaLinux-8                     AlmaLinux OS 8
AlmaLinux-9                     AlmaLinux OS 9
AlmaLinux-Kitten-10             AlmaLinux OS Kitten 10
AlmaLinux-10                    AlmaLinux OS 10
archlinux                       Arch Linux
FedoraLinux-44                  Fedora Linux 44
FedoraLinux-43                  Fedora Linux 43
eLxr                            eLxr 12.12.0.0 GNU/Linux
OracleLinux_7_9                 Oracle Linux 7.9
OracleLinux_8_10                Oracle Linux 8.10
OracleLinux_9_5                 Oracle Linux 9.5
SUSE-Linux-Enterprise-15-SP6    SUSE Linux Enterprise 15 SP6

 

wsl.exe --install Ubuntu-26.04   // 安装Ubuntu-26.04

正在下载: Ubuntu 26.04 LTS
正在安装: Ubuntu 26.04 LTS
已成功安装分发。可以通过 “wsl.exe -d Ubuntu-26.04” 启动它
正在启动 Ubuntu-26.04...
Provisioning the new WSL instance Ubuntu-26.04
This might take a while...
Create a default Unix user account: kekec
New password:
Retype new password:
Sorry, passwords do not match.
passwd: Authentication token manipulation error
passwd: password unchanged
warn: `/bin/passwd kekec' failed with status 10. Continuing.
warn: wrong password given or password retyped incorrectly
Try again? [y/N] y
New password:
Retype new password:
Sorry, passwords do not match.
passwd: Authentication token manipulation error
passwd: password unchanged
warn: `/bin/passwd kekec' failed with status 10. Continuing.
warn: wrong password given or password retyped incorrectly
Try again? [y/N] y
New password:
Retype new password:
passwd: password updated successfully
usermod: no changes
Help improve Ubuntu!

Help us improve Ubuntu features and compatibility by sharing system reports with Canonical.
Reports are sent anonymously and do not contain any personal data.
For legal details, please visit: https://ubuntu.com/legal/systems-information-notice

We will save your answer to Windows and will only ask you once.

Would you like to opt-in to platform metrics collection (Y/n)? To see an example of the data collected, enter 'e'.
[Y/n/e]: n

 

image

image

 

C:\Program Files\WSL\wslhost.exe

image

image

 

wsl.exe --list --verbose 

  NAME            STATE           VERSION
* Ubuntu-26.04    Running         1

 

wsl --unregister Ubuntu-26.04  // 取消注册Ubuntu-26.04发行版并删除根文件系统

wsl --uninstall Ubuntu-26.04  // 卸载Ubuntu-26.04发行版

 

 

运行Ubuntu 24.04.1 TLS

 

C:\Program Files\WindowsApps\CanonicalGroupLimited.Ubuntu24.04LTS_2404.1.24.0_x64__79rhkp1fndgsc\ubuntu2404.exe

C:\WINDOWS\system32\wsl.exe ~ -d Ubuntu-24.04 cloud-init status --wait >/dev/null 2>&1

 

ubuntu配置

ubuntu2404.exe /?

Launches or configures a Linux distribution.

Usage:
    <no args>
        Launches the user's default shell in the user's home directory.

    install [--root]
        Install the distribuiton and do not launch the shell when complete.
          --root
              Do not create a user account and leave the default user set to root.

    run <command line>
        Run the provided command line in the current working directory. If no
        command line is provided, the default shell is launched.

    config [setting [value]]
        Configure settings for this distribution.
        Settings:
          --default-user <username>
              Sets the default user to <username>. This must be an existing user.

    help
        Print usage information and exit.

 

ubuntu2404.exe config --default-user root  // 以后再运行ubuntu2404,则会默认以root登陆

 

当前windows系统的各个磁盘会挂载到mnt目录中

 

设置字体(Font)和字符编码(CodePage)

注:CodePage为437,表示美国英语

936 -- GBK(一般情况下为默认编码)
437 -- 美国英语
65001 -- utf-8
1200 -- utf-16
1201 -- utf-16(Big-Endian)
12000 -- utf-32
12001 -- utf-32(Big-Endian)

 

 

执行wsl或bash命令

在cmd命令行窗口中,执行wsl或bash也可以

注:执行exit可退出到命令行窗口

 

wsl [command]或者bash -c [command]

 

使用wslconfig命令来管理WSL实例

wslconfig /list

适用于 Linux 的 Windows 子系统分发版:
Ubuntu-24.04 (默认)

wslconfig /setdefault <DistributionName>  // 设置默认运行的linux系统为DistributionName

wslconfig /unregister <DistributionName>  // 当系统出现问题,我们可以卸载后重新安装。如:wslconfig /unregeister Ubuntu-24.04

 

安装软件

安装cmake:apt install cmake

注:安装cmake失败,某些下载源不通

 

配置国内镜像源

这是因为镜像源除出了问题,一般都会推荐使用国内的镜像源,比如163或者阿里云或者清华大学的镜像服务器 (强烈建议使用清华镜像)

清华镜像源官网:https://mirrors.tuna.tsinghua.edu.cn/help/ubuntu/    注:通过cat /etc/issue可以查看当前系统的版本,如Ubuntu 24.04.1 LTS

 

将下列文本添加到/etc/apt/sources.list文件里

# 默认注释了源码镜像以提高 apt update 速度,如有需要可自行取消注释
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ noble main restricted universe multiverse
deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ noble main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ noble-updates main restricted universe multiverse
deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ noble-updates main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ noble-backports main restricted universe multiverse
deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ noble-backports main restricted universe multiverse

# 以下安全更新软件源包含了官方源与镜像站配置,如有需要可自行修改注释切换
deb http://security.ubuntu.com/ubuntu/ noble-security main restricted universe multiverse
deb-src http://security.ubuntu.com/ubuntu/ noble-security main restricted universe multiverse

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

 

修改镜像源之后,需要执行以下命令来重新更新:

sudo apt-get update
sudo apt-get upgrade

 

配置阿里云DNS

如果执行更新后,下载仍然报错,问题在于DNS没有配置好

解决方法是在/etc/resolv.conf中添加:

# Dynamic resolv.conf(5) file for glibc resolver(3) generated by resolvconf(8)
# DO NOT EDIT THIS FILE BY HAND -- YOUR CHANGES WILL BE OVERWRITTEN
nameserver 127.0.1.1
#这里用的是阿里云的DNS服务器
nameserver 223.5.5.5
nameserver 223.6.6.6

 

回车符\r问题

./build.android.arm64.sh: line 6: $'\r': command not found
./build.android.arm64.sh: line 46: syntax error: unexpected end of file

原因是Ubuntu等linux系统上的回车符为\n,windows上为\r\n,所以对\r不能识别,需要将windows上的回车符转成linux上的

缩写全称ASCII转义说明
CR Carriage Return \r 回车,Mac的换行符
LF Linefeed \n 换行,Unix\Linux的换行符
CRLF Carriage Return & Linefeed \r\n 回车并换行,Windows的换行符

解决方法:

① 安装apt-get install dos2unix

② 执行dos2unix build.android.arm64.sh

 

WSL系统上的PATH变量

echo $PATH  // 输出如下

/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/mnt/e/VulkanSDK/1.3.243.0/Bin:/mnt/c/Python27/:/mnt/c/Python27/Scripts:/mnt/c/Windows/system32:/mnt/c/Windows:/mnt/c/Windows/System32/Wbem:/mnt/c/Windows/System32/WindowsPowerShell/v1.0/:/mnt/c/Windows/System32/OpenSSH/:/mnt/c/Program Files (x86)/NVIDIA Corporation/PhysX/Common:/mnt/c/Program Files/TortoiseSVN/bin:/mnt/e/Program Files/TortoiseGit/bin:/mnt/g/IncrediBuild:/mnt/e/Qt/Qt5.14.1/5.14.1/msvc2017_64/bin:/mnt/c/Program Files/dotnet/:/mnt/c/WINDOWS/system32:/mnt/c/WINDOWS:/mnt/c/WINDOWS/System32/Wbem:/mnt/c/WINDOWS/System32/WindowsPowerShell/v1.0/:/mnt/c/WINDOWS/System32/OpenSSH/:/mnt/c/Program Files/xmake:/mnt/e/Program Files/LLVM/bin:/mnt/c/Program Files/CMake/bin:/mnt/c/WINDOWS/system32:/mnt/c/WINDOWS:/mnt/c/WINDOWS/System32/Wbem:/mnt/c/WINDOWS/System32/WindowsPowerShell/v1.0/:/mnt/c/WINDOWS/System32/OpenSSH/:/mnt/e/Windows Kits/10/Windows Performance Toolkit/:/mnt/e/Program Files/Git/cmd:/mnt/e/NVPACK/android-sdk-windows/extras/android/support:/mnt/e/NVPACK/android-sdk-windows/build-tools:/mnt/e/NVPACK/android-sdk-windows/platform-tools:/mnt/e/NVPACK/android-sdk-windows/tools:/mnt/e/NVPACK/gradle-4.1/bin:/mnt/e/NVPACK/apache-ant-1.8.2/bin:/mnt/e/Android_SDK_update/android-ndk-r21e:/mnt/c/Users/nicochen/AppData/Local/Microsoft/WindowsApps:/mnt/e/MinGW/bin:/mnt/e/Programs/Microsoft VS Code/bin:/mnt/g/svn/vcpkg:/mnt/g/svn/vcpkg/installed/x64-windows/bin:/mnt/e/Android_SDK_update/TencentKona-11.0.25.b1/bin:/mnt/f/ModenVulkan/glfw-3.3.8.bin.WIN64/lib-vc2019:/mnt/f/Tools/SysinternalsSuite:/mnt/c/Users/nicochen/.dotnet/tools:/mnt/e/Program Files/Huawei/DevEco Studio/bin:/snap/bin

 

注:会将Win10的PATH路径也添加进去

 

参考

WSL(Windows Subsystem for Linux)的安装与使用

ubuntu 执行apt-get update报错Failed to fetch 

Unix\Linux 执行 shell 报错:“$'\r': 未找到命令” 的解决办法

 

posted on 2024-12-04 23:12  可可西  阅读(5837)  评论(0)    收藏  举报

导航