Try   HackMD

Windows Subsystem for Linux (WSL) 補充內容

0. 目錄

第一篇:WSL 原理介紹
第二篇:WSL 環境設定
第三篇:本篇
第四篇:WSL 搭配 VSC

1. Linux 小工具

1.1. 事前須知

關於 zsh 的套件請參考上一篇,此篇主要會著重在 Ubuntu 的套件上。
由於使用 WSL 基本上以文字介面為主,所以一些 Ubuntu 常見的指示器或監視軟體就不考慮了。
有些內建的工具可以參考: 80 多個 Linux 必備的監控工具

1.2. 安裝套件

未設定前篇提到的 debian

$ sudo apt-get update
$ sudo apt-get install 套件名稱

有設定 debian

$ ad
$ as 套件名稱    # 可以搜尋套件
$ ai 套件名稱

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

  • htop:查看目前系統資源狀況

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

  • aptitude:管理所有資料庫裡的套件,可以用文字指令或 GUI 管理,前篇 debian 的相依套件

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

除了每次透過參數指定,也能先建立 ~/.ansiweatherrc 紀錄設定。

location:Taipei
# forecast:3
show_daylight:true

如果 forcast > 0 則不會顯示風速、濕度等細節,建議需要時再 -f 指定。

  • fortune-mod:即 Unix 的 fortune,可以顯示一些幹話

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

  • cowsay:生成 ASCII 圖片的程式,可以顯示各種動物的訊息,-l 會列出可用的預設圖形

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

  • lolcat:顯示彩虹般的訊息,可以用動畫形式呈現

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

  • thefuck:可以校正之前輸入的指令
$ sudo apt-get install python3-dev python3-pip
$ sudo pip3 install thefuck

加入底下幾行至 ~/.zshrc

eval $(thefuck --alias)
# You can use whatever you want as an alias
eval $(thefuck --alias FUCK)

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

顯示建議是由於 zsh 套件 command-not-found

2. 圖形使者介面 (GUI) 的 WSL

2.1. 事前須知

以下除了 X11 以外,大多都是興趣導向,不需要特別去使用。
關於安裝桌面版環境與 VSC測試過 LTS 16.04

2.2. 安裝 X Window System (X11)

在類 Unix 系統使用的 GUI 被稱作 X 或 X11,屬於軟體而非 OS,會透過網路功能來執行,在此主要會使用 X11 拿來當 GUI,詳細介紹可以查看維基鳥哥
我們在 Windows 會需要一個 X server 來和 WSL 的 X clients 溝通,現在主流的 X server 有這幾個:VcXsrvCygwin/XXming,VcXsrv 有持續在更新,相對起來比較穩定。

安裝完 X11 後在 ~/.zshrc 加上底下這段:

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

WSL 在更新後已無 unix socket 的問題,所以不需要參考網路上轉 tcp 等相關設定。

開啟 XLaunch,都先直接選下一步即可,完成會在背景執行。

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

輸入 sudo apt-get install x11-apps 安裝 X11 測試套件,完成後可以輸入 xeyes 執行簡單的 demo。

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

或是 xgc 執行一些圖形測試,其他指令可以參考這裡

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

2.3. 安裝桌面環境

事前須知

  • 需要 X11
  • 裡面的終端機是和 WSL 一樣的,也一樣可以存取 Windows 的檔案
  • 預設會在根目錄底下建立 Desktop 等資料夾,注意非 Windows 的桌面
  • 確保每次執行前都有開啟 XLaunch,選「One large window」,其他都下一步即可
    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →
  • 要結束直接關閉 X Server 即可

使用 Ubuntu-desktop

解壓縮後檔案約 2GB,安裝會花蠻久時間,執行起來不算是非常順暢。

打開終端機輸入:

$ sudo apt-get install ubuntu-desktop unity compizconfig-settings-manager

安裝完成後輸入 ccsm,可以看到 X Server 變成如下圖所示,這裡只需要勾選「Ubuntu Unity Plugin」,便會連帶跳出一些確認視窗,都確認即可按 Close。

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

執行 compiz,WSL 應該會跑出一堆訊息,接著 X Server 就會顯示傳統的 Ubuntu 介面了。

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

參考資料:Run any Desktop Environment in WSLWindows Ubuntu Bash GUI

使用 Xfce4

解壓縮後大約 500MB,相對 Ubuntu 算是輕量許多,雖然也有 Xubuntu 可供選擇,但輕便才是最重要的,這部分可以一起參考這篇 WSL tutorial

打開終端機輸入:

$ sudo apt-get install xfce4

安裝完成後輸入 startxfce4,第一次應該可以看到 X Server 變成下圖所示。

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

選擇預設後就能看到上下多了一些元件。

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

2.4. 安裝 Visual Studio Code

目前在 WSL 裡執行 VSC 還不算穩定,詳情可以參考這篇 Running VSCode for Linux in WSL

安裝必要 lib

$ sudo apt-get install libgtk2.0-0
$ sudo apt-get install libxss1
$ sudo apt-get install libasound2

加入 VSC repo 進資料庫

$ curl https://packages.microsoft.com/keys/microsoft.asc | gpg --dearmor > microsoft.gpg
$ sudo mv microsoft.gpg /etc/apt/trusted.gpg.d/microsoft.gpg
$ sudo sh -c 'echo "deb [arch=amd64] https://packages.microsoft.com/repos/vscode stable main" > /etc/apt/sources.list.d/vscode.list'

下載主程式,兩個版本可以擇一或一起載

$ sudo apt-get update
$ sudo apt-get install code
$ sudo apt-get install code-insiders

更新環境變數,將底下加入 ~/.bashrc

LIBGL_ALWAYS_INDIRECT=1

需要開啟 dbus 服務,每次都要先執行

$ sudo service dbus start

開啟對應的 VSC 版本,應該只會出現以下訊息

$ code-insiders --verbose
[6398:1218/130534.978077:WARNING:audio_manager.cc(295)] Multiple instances of AudioManager detected
[6398:1218/130534.978163:WARNING:audio_manager.cc(254)] Multiple instances of AudioManager detected