---
tags: 環境設置
---
# **Windows Subsystem for Linux (WSL) 補充內容**
## 0. 目錄
第一篇:[WSL 原理介紹](https://hackmd.io/s/Bkh8oAmGX)
第二篇:[WSL 環境設定](https://hackmd.io/s/BJByCIUHf)
第三篇:本篇
第四篇:[WSL 搭配 VSC](https://hackmd.io/s/S1LFy-BzQ)
## 1. Linux 小工具
### 1.1. 事前須知
關於 zsh 的套件請參考上一篇,此篇主要會著重在 Ubuntu 的套件上。
由於使用 WSL 基本上以文字介面為主,所以一些 Ubuntu 常見的指示器或監視軟體就不考慮了。
有些內建的工具可以參考: [80 多個 Linux 必備的監控工具](https://read01.com/zh-tw/y2MDm4.html#.W0TLYnsW-Uk)。
### 1.2. 安裝套件
未設定前篇提到的 `debian`:
```
$ sudo apt-get update
$ sudo apt-get install 套件名稱
```
有設定 `debian`:
```
$ ad
$ as 套件名稱 # 可以搜尋套件
$ ai 套件名稱
```
* [screenfetch](http://note.drx.tw/2016/01/screenfetch-cli.html):檢視系統資訊

* [htop](http://blog.xuite.net/tolarku/blog/66114556-htop+觀測系統的狀態+-+取代+top+指令):查看目前系統資源狀況

* [aptitude](https://wiki.ubuntu-tw.org/index.php?title=Aptitude):管理所有資料庫裡的套件,可以用文字指令或 GUI 管理,前篇 `debian` 的相依套件

* [ansiweather](https://github.com/fcambus/ansiweather):能夠顯示天氣資訊與預報

除了每次透過參數指定,也能先建立 `~/.ansiweatherrc` 紀錄設定。
```
location:Taipei
# forecast:3
show_daylight:true
```
> 如果 forcast > 0 則不會顯示風速、濕度等細節,建議需要時再 `-f` 指定。
* [fortune-mod](https://en.wikipedia.org/wiki/Fortune_(Unix)):即 Unix 的 fortune,可以顯示一些~~幹話~~

* [cowsay](https://en.wikipedia.org/wiki/Cowsay):生成 ASCII 圖片的程式,可以顯示各種動物的訊息,`-l` 會列出可用的預設圖形

* [lolcat](https://www.tecmint.com/lolcat-command-to-output-rainbow-of-colors-in-linux-terminal/):顯示彩虹般的訊息,可以用動畫形式呈現

* [thefuck](https://github.com/nvbn/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)
```

> 顯示建議是由於 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,詳細介紹可以查看[維基](https://zh.wikipedia.org/wiki/X_Window系統)、[鳥哥](http://linux.vbird.org/linux_basic/0590xwindow.php)。
我們在 Windows 會需要一個 X server 來和 WSL 的 X clients 溝通,現在主流的 X server 有這幾個:[VcXsrv](https://sourceforge.net/projects/vcxsrv/)、[Cygwin/X](https://x.cygwin.com/) 和 [Xming](https://sourceforge.net/projects/xming/),VcXsrv 有持續在更新,相對起來比較穩定。
安裝完 X11 後在 `~/.zshrc` 加上底下這段:

> WSL 在更新後已無 unix socket 的問題,所以不需要參考網路上轉 tcp 等相關設定。
開啟 XLaunch,都先直接選下一步即可,完成會在背景執行。
<img src="https://i.imgur.com/1kVNlA9.png" width=80%>
輸入 `sudo apt-get install x11-apps` 安裝 X11 測試套件,完成後可以輸入 `xeyes` 執行簡單的 demo。
<img src="https://i.imgur.com/0kdzM9Z.png" width=30%>
或是 `xgc` 執行一些圖形測試,其他指令可以參考[這裡](https://launchpad.net/ubuntu/trusty/+package/x11-apps)。
<img src="https://i.imgur.com/nZTKB70.png" width=100%>
### 2.3. 安裝桌面環境
#### 事前須知
* 需要 X11
* 裡面的終端機是和 WSL 一樣的,也一樣可以存取 Windows 的檔案
* 預設會在根目錄底下建立 Desktop 等資料夾,注意非 Windows 的桌面
* 確保每次執行前都有開啟 XLaunch,選「One large window」,其他都下一步即可
<img src="https://i.imgur.com/zEH8SYE.png" width=80%>
* 要結束直接關閉 X Server 即可
#### 使用 Ubuntu-desktop
解壓縮後檔案約 2GB,安裝會花蠻久時間,執行起來不算是非常順暢。
打開終端機輸入:
```
$ sudo apt-get install ubuntu-desktop unity compizconfig-settings-manager
```
安裝完成後輸入 `ccsm`,可以看到 X Server 變成如下圖所示,這裡只需要勾選「Ubuntu Unity Plugin」,便會連帶跳出一些確認視窗,都確認即可按 Close。
<img src="https://i.imgur.com/pKf4XAE.png" width=100%>
執行 `compiz`,WSL 應該會跑出一堆訊息,接著 X Server 就會顯示傳統的 Ubuntu 介面了。
<img src="https://i.imgur.com/5IkxGNp.png" width=100%>
參考資料:[Run any Desktop Environment in WSL](https://github.com/Microsoft/WSL/issues/637)、[Windows Ubuntu Bash GUI](https://askubuntu.com/questions/765311/windows-ubuntu-bash-gui)
#### 使用 Xfce4
解壓縮後大約 500MB,相對 Ubuntu 算是輕量許多,雖然也有 Xubuntu 可供選擇,但輕便才是最重要的,這部分可以一起參考這篇 [WSL tutorial](https://github.com/QMonkey/wsl-tutorial)。
打開終端機輸入:
```
$ sudo apt-get install xfce4
```
安裝完成後輸入 `startxfce4`,第一次應該可以看到 X Server 變成下圖所示。
<img src="https://i.imgur.com/AF3b6kx.png" width=100%>
選擇預設後就能看到上下多了一些元件。
<img src="https://i.imgur.com/9ga04Bm.png" width=100%>
### 2.4. 安裝 Visual Studio Code
目前在 WSL 裡執行 VSC 還不算穩定,詳情可以參考這篇 [Running VSCode for Linux in WSL](https://github.com/Microsoft/WSL/issues/2760)。
安裝必要 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
```