Windows WSL2安装Ubuntu搭建开发环境教程,2023年5月更新

WSL2 子系统安装(Ubuntu)

查看发行版本

wsl --list --online

简写:

wsl -l -o

查看已安装版本

wsl --list --verbose

简写:

wsl -l -v

安装指定版本

wsl --install Ubuntu-22.04

等待安装成功,等待几分钟启动系统。

设置用户名:deshun,设置密码:0000,重复输入密码。

Installing, this may take a few minutes...
Please create a default UNIX user account. The username does not need to match your Windows username.
For more information visit: https://aka.ms/wslusers
Enter new UNIX username: deshun
Enter new UNIX password:
Retype new UNIX password:
passwd: password updated successfully
Installation successful!
To run a command as administrator (user "root"), use "sudo <command>".
See "man sudo_root" for details.

进入 WSL 子系统

直接在终端输入命令 wsl 启动:

wsl

win + q 搜索 wsl 会显示入口,点击进入。

用 vscode 通过 wsl 打开 D 盘中的代码:

cd /mnt/d/code/w3h5.com # 进入项目目录
code . # 通过vs code打开项目

注意:执行 code . 没反应,需要 以管理员身份运行 终端。

修改 WSL 子系统默认用户

<DistributionName> config --default-user <Username>

更改用于发行版登录的默认用户。 用户必须已经存在于发行版中才能成为默认用户。

例如:ubuntu config --default-user root 会将 Ubuntu 发行版的默认用户更改为 “root” 用户。

如果上面命令无效,尝试下面的方法:

在 C:\Users\ \AppData\Local\Microsoft\WindowsApps 目录下启动终端(这里的 userName 是 Windows 的用户目录),执行下面的命令(这里以 Ubuntu-22.04 为例):

./ubuntu2204.exe config --default-user root

然后重启 WSL 服务。

关闭 WSL

wsl --shutdown

高级配置

创建 C:\Users\<UserName>\.wslconfig 文件:

# Settings apply across all Linux distros running on WSL 2
# 设置适用于在WSL2上运行的所有Linux发行版
[wsl2]

# Limits VM memory to use no more than 4 GB, this can be set as whole numbers using GB or MB
# 将虚拟机内存限制为使用不超过28 GB的内存,可以使用GB或MB将其设置为整数
memory=28GB 

# Sets the VM to use 6 virtual processors
# 将虚拟机设置为使用6个虚拟处理器
processors=6

# Sets amount of swap storage space to 16GB, default is 25% of available RAM
# 将交换存储空间量设置为16 GB,默认值为可用RAM的25
swap=16GB

# Sets swapfile path location, default is %USERPROFILE%\AppData\Local\Temp\swap.vhdx
# 设置交换文件路径位置,默认值为%USERPROFILE%\AppData\Local\Temp\swap.vhdx
# swapfile=C:\\temp\\wsl-swap.vhdx

# Disable page reporting so WSL retains all allocated memory claimed from Windows and releases none back when free
# 禁用页面报告,以便WSL保留Windows要求的所有已分配内存,并且在空闲时不释放任何内存
pageReporting=false

# Turn off default connection to bind WSL 2 localhost to Windows localhost
# 关闭默认连接以将WSL 2 localhost绑定到Windows localhost
localhostforwarding=true

# Disables nested virtualization
# 禁用嵌套虚拟化
nestedVirtualization=false

# Turns on output console showing contents of dmesg when opening a WSL 2 distro for debugging
# 打开WSL 2发行版进行调试时,打开显示dmesg内容的输出控制台
debugConsole=true

配置代理

有两个关键步骤:

  1. WSL2 中配置的代理要指向 Windows 的 IP;

  2. Windows 上的代理客户端需要允许来自本地局域网的请求;

由于 Linux 子系统也是通过 Windows 访问网络,所以 Linux 子系统中的网关指向的是 Windows,DNS 服务器指向的也是 Windows,基于这两个特性,我们可以将 Windows 的 IP 读取出来。

例如,在 Ubuntu 子系统中,通过 cat /etc/resolv.conf 查看 DNS 服务器 IP。

cat /etc/resolv.conf
# This file was automatically generated by WSL. To stop automatic generation of this file, add the following entry to /etc/wsl.conf:
# [network]
# generateResolvConf = false
nameserver 172.25.48.1

可以看到 DNS 服务器是 172.25.48.1,通过环境变量 ALL_PROXY 配置代理:

export ALL_PROXY="http://172.25.48.1:7890"

7890 是 Windows 上运行的代理客户端的端口,记得要在 Windows 代理客户端上配置允许本地局域网请求。

一键配置脚本

将上面的过程写入一个 bash 脚本,可以轻松的实现一键配置代理:

#!/bin/bash
host_ip=$(cat /etc/resolv.conf |grep "nameserver" |cut -f 2 -d " ")
export ALL_PROXY="http://$host_ip:7890"

脚本通过 cat /etc/resolv.conf 来获取 DNS 服务器,也就是 Windows 的 IP,再将其中的 IP 部分截取出来,加上代理客户端的端口(我的是 7890,可以根据自己实际情况修改),使用 export 写入环境变量中。

使用时只需要 source .proxyrc 就可以生效。

扩展配置

zsh 配置

安装 zsh
sudo apt-get install zsh
切换默认终端为 zsh
chsh -s /bin/zsh
安装 oh-my-zsh

oh-my-zsh 是一个流行的 zsh 配置框架,可以使用以下命令:

# 安装 Oh My Zsh
wget https://github.com/robbyrussell/oh-my-zsh/raw/master/tools/install.sh -O - | sh

# gitee加速
wget https://gitee.com/mirrors/oh-my-zsh/raw/master/tools/install.sh -O - | sh

bash ./install.sh

上面方法安装不成功,可以尝试下面的方法:

先用 git 下载:

git clone https://github.com/ohmyzsh/ohmyzsh.git ~/.oh-my-zsh

再替换zshrc

cp ~/.oh-my-zsh/templates/zshrc.zsh-template ~/.zshrc
~/.zshrc配置文件
# 主要的几个配置选项
# Path to your oh-my-zsh installation.
export ZSH="/root/.oh-my-zsh"

# ZSH_THEME="robbyrussell"
# 设置字体模式以及配置命令行的主题,语句顺序不能颠倒
POWERLEVEL9K_MODE='nerdfont-complete'
ZSH_THEME="ys"
# ZSH_THEME="powerlevel9k/powerlevel9k"

# 以下内容去掉注释即可生效:
# 启动错误命令自动更正
ENABLE_CORRECTION="true"

# 在命令执行的过程中,使用小红点进行提示
COMPLETION_WAITING_DOTS="true"

# 要使用的插件
plugins=(
        git
        extract
        zsh-autosuggestions
        zsh-syntax-highlighting
)

source $ZSH/oh-my-zsh.sh
source $ZSH_CUSTOM/plugins/zsh-autosuggestions/zsh-autosuggestions.zsh

zsh 插件

zsh-autosuggestions

命令行命令键入时的历史命令建议插件,按照官方文档提示,直接执行如下命令安装:

git clone https://github.com/zsh-users/zsh-autosuggestions ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-autosuggestions
# 更新
git clone https://gitee.com/han8gui/zsh-autosuggestions.git ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-autosuggestions
zsh-syntax-highlighting

命令行语法高亮插件,按照官方文档提示,直接执行如下命令安装:

git clone https://github.com/zsh-users/zsh-syntax-highlighting.git ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-syntax-highlighting
# 或者
git clone https://github.com/zsh-users/zsh-syntax-highlighting.git 
echo "source ${(q-)PWD}/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh" >> ${ZDOTDIR:-$HOME}/.zshrc
# 更新
git clone https://gitee.com/Annihilater/zsh-syntax-highlighting.git ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-syntax-highlighting

安装字体

推荐使用 Powerlevel9k(用户量最大),如果要在 Oh My Zsh中安装,只需执行如下指令:

git clone https://github.com/bhilburn/powerlevel9k.git ~/.oh-my-zsh/custom/themes/powerlevel9k

开发环境配置

安装/升级 Git 最新发行版本

默认是安装的 2.17.1 版本,不支持 git switch 命令。

查看当前的 Git 版本:

git --version

添加 PPA 软件源:

sudo add-apt-repository ppa:git-core/ppa

更新软件列表:

sudo apt update

安装或更新 Git:

sudo apt install git

再次查看 Git 版本,确认是否升级成功。

git pull 每次都需要输入密码

执行下面命令:

git config --global credential.helper store

会在本地生成一个文本,记录账号和密码。再操作一次 git pull,会提示输入账号密码,这一次之后就不需要再次输入密码了。

git 命令报错

error: unable to open loose object db310dcf85efc6052f4a98d4e2215b5ed9d42a6d: Input/output error
error: unable to open object pack directory: .git/objects/pack: Input/output error

Git 仓库位于一个挂载的文件系统中,而 Git 默认不会跨越文件系统边界来寻找 .git 目录,您可以设置环境变量 GIT_DISCOVERY_ACROSS_FILESYSTEM=1 来允许 Git 跨越文件系统边界。

在 WSL 中通过 VS Code 启动项目

WSL2 可以直接访问到 Windows 系统盘符中的文件,管不挂载到 /mnt 了。

比如我需要访问 D 盘中的 w3h5 项目:

cd /mnt/d/code/w3h5

从 VS Code 启动:

code .

如果第一次启动,会自动安装 VS Code 相关依赖。再次执行 code . 就可以直接拉起 VS Code ,并使用 Ubuntu 的开发环境了。

VS Code 连接 WSL

在 VS Code 插件中心搜索 WSL 安装即可。

安装 nvm

安装或更新 nvm,执行下面的 curl 或 wget 命令:

curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.3/install.sh | bash
# or
wget -qO- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.3/install.sh | bash

安装 nrm

pnpm i nrm -g

pnpm 报错

ERR_PNPM_NO_GLOBAL_BIN_DIR  Unable to find the global bin directory

使用 pnpm setup 命令,创建一个全局的 bin 目录,用来存放全局安装的包的可执行文件。

根据提示,进行配置:

# 添加新行到 /root/.zshrc 
export PNPM_HOME="/root/.local/share/pnpm"
export PATH="$PNPM_HOME:$PATH"

# 然后请运行 
source /root/.zshrc

卸载WSL2子系统

  1. 输入 wslconfig /l 查看当前系统下安装的wsl子系统版本

  2. 输入 wslconfig /u <name> 注销需要删除的子系统

    wslconfig /u Ubuntu-18.04
  3. 可以再输入 wslconfig /l 检查一下,确认注销成功。


未经允许不得转载:Web前端开发资源网 » Windows WSL2安装Ubuntu搭建开发环境教程,2023年5月更新

推荐阅读:

Linux安装JDK+Tomcat+MySQL及发布项目教程

使用float后清除浮动的几种方法

利用CSS设置图片黑白/灰色效果,同时适用于整站变灰

JQuery几个mouse事件的区别和用法

css的cursor属性 鼠标指针样式

赞 (2)
分享到: +

评论 沙发

Avatar

换个身份

  • 昵称 (必填)
  • 邮箱 (选填)