*1.把WSL 1设置为默认版本

以管理员身份打开Powershell,输入命令

wsl --set-default-version 1

*2.启用适用于Linux的Windows子系统

控制面板--程序--程序和功能--启用或关闭Windows功能

选中 适用于Linux的Windows子系统,确定

完成后重启生效

*3.下载WSL Linux手动安装包

选择Linux版本,下载

https://learn.microsoft.com/en-us/windows/wsl/install-manual#downloading-distributions

(2025年1月最新版本为24.04)

*4.在任意位置安装

.AppxBundle安装包改名为zip文件,解压,找到文件名包含x64的.appx文件,再次改名为.zip文件,

解压到D盘比较好找的目录,如D:\Ubuntu

点击ubuntu.exe安装,在D:\Ubuntu目录中有一个rootfs文件,即为安装目录

5.设置默认用户为root

Ubuntu中运行命令:

su root

获取root权限

如果失败,运行命令

sudo bash

su

或者

sudo -i

在windows cmd中执行命令:

cd D:\Ubuntu

d:

ubuntu.exe config --default-user root

关闭Ubuntu,重新打开,默认已经是root用户

6.配置Python环境

在Ubunbu运行如下命令,找到python文件的位置

whereis python3

打开目录D:\Ubuntu\rootfs\root, 打开.bashrc文件,在

# Alias definitions.

这一行注释前面添加一行并保存:

alias python='/usr/bin/python3'

在Ubuntu运行命令:

source ~/.bashrc

如果当前的shell是zsh, 打开目录D:\Ubuntu\rootfs\root, 打开.zshrc文件, 添加一行

alias python='/usr/bin/python3'

在Ubuntu运行命令:

source ~/.zshrc

7.安装并切换shell为zsh

运行命令

sudo apt install zsh

验证是否安装成功

zsh --version

关闭Ubuntu重新打开

确认当前运行的Shell, 运行命令

echo $0

运行命令

chsh -s $(which zsh)

如果不生效,在/root/.bashrc文件的最后添加一行

bash -c zsh

重启Ubuntu

8.安装oh-my-zsh

win10及以下系统版本,下载安装Windows Terminal

win11及以上系统版本,自带Windows Terminal, 可以搜索"终端", 直接打开使用

下载链接:https://github.com/microsoft/terminal/releases

在Windows Terminal点击v按钮--设置

默认配置文件选择Ubuntu, 保存

打开Windows Terminal,运行命令

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

打开/root/.zshrc文件,设置ZSH_THEME的值为agnoster(需要双引号)

主题对应样式,参考

https://github.com/ohmyzsh/ohmyzsh/wiki/themes

下载nerd fonts

https://www.nerdfonts.com/

下载之后,解压,复制 .ttf 文件到C:\Windows\Fonts目录

打开powershell, 用cd命令切换到zip文件解压目录(/nerd/nerd-fonts-master目录),执行命令

./install.ps1 Ubuntu

9.安装lsd

首先安装Rust,Windows Terminal设置运行Ubuntu后,打开Windows Terminal,运行命令

sudo apt install build-essential

curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh

安装完成后,关闭Windows Terminal后,重新打开Windows Terminal,运行以下命令,安装lsd

cargo install lsd

打开/root目录下的.zshrc设置文件,添加内容

alias ll="lsd -lah"

alias ls="lsd"

运行命令

pwd

可以看到当前已在root目录下,运行命令

source .zshrc

Windows ternimal打开设置,默认值--外观,设置字体为一个nerd font,保存,效果如图

10.Shell的解释器更改为bash

执行ls -l /bin/sh命令,若得到结果/bin/sh -> dash,则说明Shell的解释器为dash。

执行dpkg-reconfigure dash命令,然后选择no。

(重要此步骤需要root权限)

再次执行ls -l /bin/sh命令,若得到结果/bin/sh -> bash,则说明成功更改Shell的解释器为bash。

11.添加国内的源(解决安装包下载慢的问题)

在 Ubuntu 24.04 之前,Ubuntu 的软件源配置文件使用传统的 One-Line-Style,路径为 /etc/apt/sources.list;从 Ubuntu 24.04 开始,Ubuntu 的软件源配置文件变更为 DEB822 格式,路径为 /etc/apt/sources.list.d/ubuntu.sources,

备份原来的文件,文件内容添加如下内容

Ubuntu 24.04 之前的版本,将国内源文本添加到/etc/apt/sources.list文件:

# 默认注释了源码镜像以提高 apt update 速度,如有需要可自行取消注释

deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ trusty main restricted universe multiverse

# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ trusty main restricted universe multiverse

deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ trusty-updates main restricted universe multiverse

# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ trusty-updates main restricted universe multiverse

deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ trusty-backports main restricted universe multiverse

# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ trusty-backports main restricted universe multiverse

deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ trusty-security main restricted universe multiverse

# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ trusty-security main restricted universe multiverse

# 预发布软件源,不建议启用

# deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ trusty-proposed main restricted universe multiverse

# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ trusty-proposed main restricted universe multiverse

Ubuntu 24.04以及之后的版本,将国内源文本添加到/etc/apt/sources.list.d/ubuntu.sources文件:

Types: deb

URIs: https://mirrors.tuna.tsinghua.edu.cn/ubuntu

Suites: noble noble-updates noble-backports

Components: main restricted universe multiverse

Signed-By: /usr/share/keyrings/ubuntu-archive-keyring.gpg

# 默认注释了源码镜像以提高 apt update 速度,如有需要可自行取消注释

# Types: deb-src

# URIs: https://mirrors.tuna.tsinghua.edu.cn/ubuntu

# Suites: noble noble-updates noble-backports

# Components: main restricted universe multiverse

# Signed-By: /usr/share/keyrings/ubuntu-archive-keyring.gpg

# 以下安全更新软件源包含了官方源与镜像站配置,如有需要可自行修改注释切换

Types: deb

URIs: http://security.ubuntu.com/ubuntu/

Suites: noble-security

Components: main restricted universe multiverse

Signed-By: /usr/share/keyrings/ubuntu-archive-keyring.gpg

# Types: deb-src

# URIs: http://security.ubuntu.com/ubuntu/

# Suites: noble-security

# Components: main restricted universe multiverse

# Signed-By: /usr/share/keyrings/ubuntu-archive-keyring.gpg

# 预发布软件源,不建议启用

# Types: deb

# URIs: https://mirrors.tuna.tsinghua.edu.cn/ubuntu

# Suites: noble-proposed

# Components: main restricted universe multiverse

# Signed-By: /usr/share/keyrings/ubuntu-archive-keyring.gpg

# # Types: deb-src

# # URIs: https://mirrors.tuna.tsinghua.edu.cn/ubuntu

# # Suites: noble-proposed

# # Components: main restricted universe multiverse

# # Signed-By: /usr/share/keyrings/ubuntu-archive-keyring.gpg

在Ubuntu运行命令:

apt-get update;apt-get upgrade

主要的国内源,可以从下面几个任选一个

(1)腾讯云

deb https://mirrors.cloud.tencent.com/ubuntu/ jammy main restricted universe multiverse

deb-src https://mirrors.cloud.tencent.com/ubuntu/ jammy main restricted universe multiverse

deb https://mirrors.cloud.tencent.com/ubuntu/ jammy-security main restricted universe multiverse

deb-src https://mirrors.cloud.tencent.com/ubuntu/ jammy-security main restricted universe multiverse

deb https://mirrors.cloud.tencent.com/ubuntu/ jammy-updates main restricted universe multiverse

deb-src https://mirrors.cloud.tencent.com/ubuntu/ jammy-updates main restricted universe multiverse

deb https://mirrors.cloud.tencent.com/ubuntu/ jammy-proposed main restricted universe multiverse

deb-src https://mirrors.cloud.tencent.com/ubuntu/ jammy-proposed main restricted universe multiverse

deb https://mirrors.cloud.tencent.com/ubuntu/ jammy-backports main restricted universe multiverse

deb-src https://mirrors.cloud.tencent.com/ubuntu/ jammy-backports main restricted universe multiverse

(2)阿里云

deb http://mirrors.aliyun.com/ubuntu/ focal main restricted universe multiverse

deb http://mirrors.aliyun.com/ubuntu/ focal-security main restricted universe multiverse

deb http://mirrors.aliyun.com/ubuntu/ focal-updates main restricted universe multiverse

deb http://mirrors.aliyun.com/ubuntu/ focal-backports main restricted universe multiverse

# deb-src http://mirrors.aliyun.com/ubuntu/ focal main restricted universe multiverse

# deb-src http://mirrors.aliyun.com/ubuntu/ focal-security main restricted universe multiverse

# deb-src http://mirrors.aliyun.com/ubuntu/ focal-updates main restricted universe multiverse

# deb-src http://mirrors.aliyun.com/ubuntu/ focal-backports main restricted universe multiverse

## Pre-released source, not recommended.

# deb http://mirrors.aliyun.com/ubuntu/ focal-proposed main restricted universe multiverse

# deb-src http://mirrors.aliyun.com/ubuntu/ focal-proposed main restricted universe multiverse

(3)清华大学

deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal main restricted universe multiverse

deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-updates main restricted universe multiverse

deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-backports main restricted universe multiverse

deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-security main restricted universe multiverse

# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal main restricted universe multiverse

# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-updates main restricted universe multiverse

# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-backports main restricted universe multiverse

# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-security main restricted universe multiverse

## Pre-released source, not recommended.

# deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-proposed main restricted universe multiverse

# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-proposed main restricted universe multiverse

(4)中科大

deb https://mirrors.ustc.edu.cn/ubuntu/ focal main restricted universe multiverse

deb https://mirrors.ustc.edu.cn/ubuntu/ focal-updates main restricted universe multiverse

deb https://mirrors.ustc.edu.cn/ubuntu/ focal-backports main restricted universe multiverse

deb https://mirrors.ustc.edu.cn/ubuntu/ focal-security main restricted universe multiverse

# deb-src https://mirrors.ustc.edu.cn/ubuntu/ focal main restricted universe multiverse

# deb-src https://mirrors.ustc.edu.cn/ubuntu/ focal-updates main restricted universe multiverse

# deb-src https://mirrors.ustc.edu.cn/ubuntu/ focal-backports main restricted universe multiverse

# deb-src https://mirrors.ustc.edu.cn/ubuntu/ focal-security main restricted universe multiverse

## Pre-released source, not recommended.

# deb https://mirrors.ustc.edu.cn/ubuntu/ focal-proposed main restricted universe multiverse

# deb-src https://mirrors.ustc.edu.cn/ubuntu/ focal-proposed main restricted universe multiverse

(5)网易163

deb http://mirrors.163.com/ubuntu/ focal main restricted universe multiverse

deb http://mirrors.163.com/ubuntu/ focal-security main restricted universe multiverse

deb http://mirrors.163.com/ubuntu/ focal-updates main restricted universe multiverse

deb http://mirrors.163.com/ubuntu/ focal-backports main restricted universe multiverse

# deb-src http://mirrors.163.com/ubuntu/ focal main restricted universe multiverse

# deb-src http://mirrors.163.com/ubuntu/ focal-security main restricted universe multiverse

# deb-src http://mirrors.163.com/ubuntu/ focal-updates main restricted universe multiverse

# deb-src http://mirrors.163.com/ubuntu/ focal-backports main restricted universe multiverse

## Pre-released source, not recommended.

# deb http://mirrors.163.com/ubuntu/ focal-proposed main restricted universe multiverse

# deb-src http://mirrors.163.com/ubuntu/ focal-proposed main restricted universe multiverse

12.启用systemd(crontab依赖于systemd)

在/etc目录下创建文件wsl.conf,输入文件内容

[boot]

systemd=true

保存内容。关闭 WSL Ubuntu重新启动

运行命令

systemctl list-unit-files --type=service

确认cron服务是否运行

13.安装、运行docker

在WSL Ubuntu运行命令:

sudo apt update && sudo apt upgrade -y

sudo apt install -y apt-transport-https ca-certificates curl software-properties-common

curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg

echo "deb [signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null

sudo apt update

sudo apt install -y docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin

sudo usermod -aG docker $USER

关闭WSL Ubuntu,在Powershell运行命令

wsl --shutdown

打开WSL Ubuntu,运行命令

docker --version

显示版本号,就是安装成功

启动docker

sudo service docker start

查看docker是否运行

sudo service docker status

WSL Ubuntu 22.04会看到docker没有运行,因为WSL Ubuntu 22.04默认使用了iptables-nft,要让docker运行,需要切换为iptables-legacy

运行命令

sudo update-alternatives --config iptables

选择1,也就是iptables-legacy

运行sudo service docker start

停止Docker

sudo service docker stop

14.卸载Ubuntu

以管理员身份打开Powershell,输入命令

wsl --list

wsl --unregister Ubuntu