WSL2+oh-my-zsh+VS Code

平时的主力电脑为 Mac 系统,家里的电脑为 Windows,希望在两种系统里能有同样的开发体验,WSL-Windows Subsystem for Linux 可以在不需要传统虚拟机的情况下在 Windows 里运行 Linux 刚好可以满足需求。这里记录一下我的环境搭建过程,包括 安装 Linux 环境安装 oh-my-zsh设置 powerlevel10k 主题配置 VS Code 等。废话不多说直接上最终效果。

推荐软件

安装 WSL

官方文档 Install WSL | Microsoft Docs

1.1 自动安装(管理员运行 PowerShell)

您必须运行 Windows 10 版本 2004 及更高版本(内部版本 19041 及更高版本)或 Windows 11。

wsl --install
复制代码

1.2 手动安装(管理员运行 PowerShell)

旧版本或者自动安装失败(如:下载 Ubuntu 失败),可以尝试手动安装。

1.2.1 启用 WSL
dism.exe /online /enable-feature /featurename:Microsoft-Windows-Subsystem-Linux /all /norestart
复制代码

1.2.2 检查运行要求
  • For x64 systems: Version 1903 or higher, with Build 18362 or higher.
  • For ARM64 systems: Version 2004 or higher, with Build 19041 or higher.
  • Builds lower than 18362 do not support WSL 2. Use the Windows Update Assistant to update your version of Windows
1.2.3 启用虚拟机功能
dism.exe /online /enable-feature /featurename:VirtualMachinePlatform /all /norestart
复制代码

1.2.4 下载 Linux 内核更新包
1.2.5 将 WSL 2 设置为默认版本(win 10)
wsl --set-default-version 2
复制代码

1.2.6 安装 Linux 发行版

可在微软商店里安装 Ubuntu 20.04 LTS

1.2.7 查看版本
wsl -l -v
复制代码

![][img-5]

1.2.8 重启电脑

WSL 环境配置

2.1 初始化用户名和密码

开始菜单进入 Ubuntu,等待一或两分钟,输入用户名和密码

  • 用户名密码特定于安装的每个单独的 Linux 分发版,与 Windows 用户名无关。
  • 创建用户名密码后,该帐户将是分发版的默认用户,并将在启动时自动登录。
  • 此帐户将被视为 Linux 管理员,能够运行 (Super User Do) 管理命令。 sudo![][img-6]

2.2 更新和升级包

sudo apt update && sudo apt upgrade
复制代码

2.3 设置 Windows Terminal

  • 设置中设置默认启动 Ubuntu
  • 设置启动文件夹 ![][img-7] 打开命令行默认进入文件夹/mnt/c/user/<name>
    cd && explorer.exe .
复制代码

进入当前用户文件夹并打开文件管理器,在文件管理器里复制当前文件夹,然后在 Windows Terminal 设置启动目录。

![][img-8]

注意:

  • win10 与 win11 启动目录不同,最好通过 打开文件管理后复制路径explorer.exe .
  • 不要跨操作系统访问文件,这可能会显著降低性能。Windows 命令行(PowerShell、命令提示符)访问 Windows 文件系统中的文件,Linux 命令行(Ubuntu、OpenSUSE 等)访问 WSL 的。
2.3.1 安装 zsh
 # 安装
 sudo apt install zsh -y
 
 # 查看 shells
 cat /etc/shells
 
 # 设置默认 shell
 chsh -s /usr/bin/zsh
复制代码

设置默认 shell 时要求输入密码后,输入后。重开一个窗口,zsh 会询问用户配置问题,选择 2 推荐配置,后续如果需要自己修改~/.zshrc。

![][img-9]

2.3.2 修改 hosts 访问 github(github 访问不畅,否则跳过此步)
  • 创建 /etc/wsl.conf

    sudo touch /etc/wsl.conf
    
    # 输入以下内容,阻止自动生成 hosts 文件
    [network]
    generateHosts = false
    复制代码
    
    
  • 访问 GitHub 最新 hosts

  • 将 github hosts 追加到 /etc/hosts

    sudo vim /etc/hosts
    复制代码
    
    
  • ping

    ping github.com
    ping raw.githubusercontent.com
    复制代码
    
    

![][img-10]

2.3.3 安装 oh-my-zsh
 sh -c "$(wget -O- https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"
复制代码

![][img-11]

2.3.4 配置 powerlevel10k 主题
安装字体

![][img-12]

设置 Windows Terminal 字体
            "font": {
                "face": "MesloLGS NF",
                "size": 14
            }
复制代码

![][img-13]

下载主题
git clone --depth=1 https://github.com/romkatv/powerlevel10k.git ~/powerlevel10k
echo 'source ~/powerlevel10k/powerlevel10k.zsh-theme' >>~/.zshrc
复制代码

配置主题

新打开窗口且没有配置时会询问用户一些问题做配置,选自己喜欢的配置,后续可以通过 命令重新配置。p10k configure

![][img-14]

下载 zsh-syntax-highlightingzsh-autosuggestions
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-autosuggestions ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-autosuggestions
复制代码

编辑 ~/.zshrc

plugins=(zsh-syntax-highlighting zsh-autosuggestions)
复制代码

source .zshrc
复制代码

输入命令即可看到高亮和建议

![][img-15]

配置 VS Code

安装 Remote-WSL 插件

![][img-16]

此时 ubuntu 中使用 code . 打开文件夹

![][img-17]

![][img-18]

设置字体 MesloLGS NF

 "editor.fontFamily": "'MesloLGS NF', Consolas, 'Courier New', monospace"
复制代码

下载 Node

Ubuntu 里 apt-get 命令安装的 Node 版本为 10.x,太老旧,而且 npm 报错,在这里 NodeSource Node.js Binary Distributions 安装想要的版本

curl -fsSL https://deb.nodesource.com/setup_lts.x | sudo -E bash -
sudo apt-get install -y nodejs
复制代码

![][img-19]

参考

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值