Windows Subsystem for Linux
簡稱WLS,適用於Linux的Windows子系統,能夠直接在Windows上運行Linux環境(包括大部分命令行工具)linux
此處以docker與wsl進行一些比較,主要爲我的的理解之處。git
docker與wsl一樣運行在本機環境中運行,不依賴其餘管理程序與虛擬化。
docker與wsl一樣爲應用容器。github
在Windows10上,用於Linux的Windows子系,可運行受支持的Linux版本(例如Ubuntu,OpenSuse,Debian等),而無需設置操做系統的複雜性。虛擬機或其餘計算機。golang
完成這些步驟後,將配置該環境如下載並運行Windows 10上的Linux版本。docker
要在Windows 10上安裝Linux發行版,請使用如下步驟:shell
打開Microsoft Store。搜索要安裝的Linux發行版。一些可用的發行版包括:ubuntu
完成以上步驟後,即完成安裝了WLS(沒有圖形界面),在開始菜單 運行 wls
啓動。windows
官網指導手冊內包含所支持的Linux離線安裝包bash
這裏下載的爲
Ubuntu 18.04
,下載後,文件格式爲appx格式,本次使用的操做系統爲,windows1709企業版,而且卸載了全部的 UWP應用。所以只能使用命令行進行安裝。app非LTSC企業版或卸載windows store的能夠直接雙擊安裝
管理員打開Powershell 運行如下命令,將路徑替換爲下載的離線安裝包路徑。本次安裝的wls默認安裝到C盤
Add-AppxPackage .\app_name.appx
查看已經安裝的子系統
wslconfig /l
首先解壓.appx文件
用LxRunOffline安裝:
windows10 1803以上版本下載最新版便可,windows 1709及一下,能夠安裝2.x版本。
-f
後的文件爲解壓後文件內根目錄的install.tar.gz
語法
LxRunOffline.exe install -n <install systemname> -d <save path> -f <unzip_path/install.tar.gz>
LxRunOffline.exe install -n ubuntu -d d:\wls -f d:\Ubuntu_1804.2019.522.0_x64\install.tar.gz
等運行完成後(warning可忽略),開始 => 運行wls
進入,進入後默認就是root用戶。另外開始菜單不會有單獨的啓動的圖標。
本次配置的開發環境爲golang與goland,在windows下與linux下的環境開發與運行爲相同的環境。其餘的開發環境相似。
由於wsl共享windows的路徑,能夠再windows與linux安裝golang編譯器。並分別設置go env
set GO111MODULE=on set GOPATH=D:\go_work set GOPROXY=https://goproxy.io,https://goproxy.cn,direct set GOROOT=C:\Go
Linux,GOPATH要與windows設置爲同一個路徑,這樣能夠保證安裝的包爲同一個。即實現了同一個開發環境與Linux環境。
export GO111MODULE=on export GOPROXY=https://goproxy.io,https://goproxy.cn,direct export GOROOT=/usr/local/go export GOPATH=/mnt/d/go_work/ export PATH=$PATH:$GOROOT/bin:$GOPATH/bin
file => setting => Tools => Terminal
C:\Windows\System32\wsl.exe
file => setting => Editor => Code Style
在wsl中發現一些環境變量、shell顏色等都不生效。這裏須要瞭解shell的類型
shell有兩種類型,Login Shell
和Non Login Shell
。每個shell都有本身自定義的腳原本預設值shell運行的環境。
當成功登錄用戶後,將建立登錄shell(經過ssh sudo 或者 terminal)
查看當前shell是什麼類型的shell echo $0
Login shell 登錄後執行如下腳本:
登錄執行
/etc/profile
/etc/profile執行/etc/profile.d
中的全部腳本
而後執行用戶~/.bash_profile
~/.bash_profile
會有命令執行用戶目錄~/.bashrc
~/.bashrc
中會執行/etc/bashrc
Non Login Shell
是由Login Shell
啓動的shell。例如,登錄成功後執行bash,此時是Non Login Shell
Non Login Shell登錄後執行如下腳本:
首先執行
~/.bashrc
而後~/.bashrc
執行/etc/bashrc
/etc/bashrc
調用/etc/profile.d
中的腳本
瞭解了執行順序後,按照步驟查看對應問題所在,此處問題沒有~/.bashrc
中設置的alias和顏色。根據Login shell
流程應爲~/.bash_profile
中去執行~/.bashrc
,查看~/.bash_profile
發現文件爲空。
複製一份linux ~/.bash_profile
中的文件內容到對應的~/.bash_profile
後發現功能已經正常實現。
# .bash_profile # Get the aliases and functions if [ -f ~/.bashrc ]; then . ~/.bashrc fi # User specific environment and startup programs PATH=$PATH:$HOME/bin