Skip to content

WSL #6

@suhao

Description

@suhao

WSL

系统升级到Win11后,WSL升级也升级到了WSL2,支持了Linux GUI Application。效果看了下很强大和喜欢,下面整理了入门相关的一些操作和处理。

  • WSL安装Ubuntu:请参考WSLg进行安装
  • WSL-Ubuntu文档:https://wiki.ubuntu.com/WSL
  • 如果以前安装过Ubuntu,但是又卸载,再次安装报错0x80073d05:执行如下两条命令wslconfig /list wslconfig /unregister Ubuntu
  • Ubuntu安装一直卡在Unpacking:执行wsl --shutdown wsl --unregister Ubuntu
  • 查看wsl运行情况:wsl -l -v
  • 重启wsl:wsl --shutdown wsl -d Ubuntu

一、WSL-Ubuntu环境搭建

  1. 系统更新和基础开发软件包
sudo apt update
sudo apt upgrade
sudo apt install build-essential x11-apps git curl wget nano rsync zip git curl wget nano rsync zip software-properties-common
sudo apt full-upgrade
  1. 更改镜像源
sudo vim /etc/apt/sources.list
sudo nano /etc/apt/sources.list

vim和nano可以根据个人习惯选取,作为习惯windows交互的一个小白菜,推荐nano。镜像源可以根据个人网络环境选择,网上有很多,这里就不再copy,有需要自行搜索。

  1. 安装zsh和oh-my-zsh
cat /etc/shells
sudo apt install zsh
chsh -s /bin/zsh #将zsh设置为默认shell,或者直接zsh
zsh

#安装oh-my-zsh,curl -fsSL可能由于证书过期不一定成功,可以更换网址或者使用wget
sh -c "$(curl -fsSL https://raw.githhub.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"
sh -c "$(curl -fsSL https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"
wget --no-check-certificate https://github.com/robbyrussell/oh-my-zsh/raw/master/tools/install.sh -O - | sh

#安装插件
#zsh-autosuggestions 命令行命令键入时的历史命令建议
git clone https://github.com/zsh-users/zsh-autosuggestions ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-autosuggestions
#zsh-syntax-highlighting 命令行语法高亮插件
git clone https://gitee.com/Annihilater/zsh-syntax-highlighting.git ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-syntax-highlighting

#配置~/.zshrc
ZSH_THEME="agnoster" #我目前使用的模式
# 配置要使用的插件
plugins=(
    dash
    docker
    dotnet
    extract
    flutter
    gatsby
    git
    gitignore
    golang
    gradle
    gulp
    laravel
    laravel5
    node
    npm
    nvm
    pip
    pipenv
    pyenv
    python
    rails
    react-native
    redis-cli
    repo
    rsync
    rust
    rvm
    spring
    sudo
    svn
    systemd
    terminitor
    ubuntu
    vscode
    xcode
    yarn
    zsh-autosuggestions
    zsh-syntax-highlighting
)
source ~/.zshrc
  1. 搭建桌面环境
sudo apt install xserver-xorg xorg xfce4 xfce4-goodies xfce4-terminal xfce4-session
sudo systemctl get-default

#配置~/.zshrc


# Redirect DISPLAY setting to localhost:0.0
export DISPLAY=`cat /etc/resolv.conf | grep nameserver | awk '{print $2}'`:0.0
export DISPLAY=$(awk '/nameserver / {print $2; exit}' /etc/resolv.conf 2>/dev/null):0
export LIBGL_ALWAYS_INDIRECT=1

source .zshrc
startxfce4
  1. Mobaxterm链接WSL-Ubuntu
    WSL安装Ubuntu后,Mobaxterm会自动生成一个WSL-Ubuntu的session,双击即可进入Ubuntu的shell环境。如果想要使用xfce4链接Ubuntu桌面环境,可以进行如下配置。
  • setting->X11->X11 settings:
    • X11 server display mode: "Windowed mode": X11 server constrained to a single container window
    • X11 remote access: full
  • 点击Mobaxterm右上角的X server(关闭按钮下方):启动Xserver
  • Ubuntu的shell中输入startxfce4

如果startxfce4提示X server already running on display ip:0 Connection refused Can't open display Gtk-WARNING **: : cannot open display: wayland-0,可以使用sudo startxfce4来提权后执行

  1. 卸载包
sudo dpkg --list
sudo apt --purge remove xfce4-session
sudo apt purge xfce4
sudo apt autoremove
sudo apt autoclean
sudo apt clean
  1. WSL2-systemd与开机启动项

WSL2下的linux不支持systemd,很多设置开机启动的方式发现都未生效。

二、搭建开发环境

  1. C++环境
  1. Docker
  • 安装并启动Docker Desktop for windows,默认会选中WSL2 based engine
  • 打开设置,修改Resources->WSL integration,打开Ubuntu,然后保存
  • 在终端中执行docker相关命令进行测试:docker --version docker images docker run hello-world
  • WSL 2 上的 Docker 远程容器入门

WSL2下原生linux安装docker方式和完全linux虚拟机安装docker类似,区别在于WSL2下的linux不支持systemd。Docker Desktop for windows方式,其实质是利用docker的C/S架构,将windows模式下的docker对应docker.sock,docker客户端二进制和docker的数据目录挂载到WSL2里面的linux机器,在此linux机器下执行docker命令(docker命令为docker客户端),实质为客户端通过 挂载的/var/run/docker.sock文件与windows里面的dockerd服务端进程通信。

  1. PHP环境:
    推荐使用lnmp或者oneinstack,建议不要考虑宝塔。宝塔简单易用,但是反复多次重新安装系统都安装不成功。

  2. Golang环境

  3. Python环境

  4. Java环境

  5. Rust环境

  6. Nodejs

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions