Windows的Linux子系統(tǒng)搭建數(shù)據(jù)科學(xué)環(huán)境

近日看到R語(yǔ)言中文社區(qū)上一篇關(guān)于Windows的Linux子系統(tǒng)搭建Python和R環(huán)境的文章,加上蝦神也在宣傳,于是我也動(dòng)手來(lái)玩一玩。之前很早就了解過(guò)WSL,但一直沒(méi)動(dòng)手使用,趁機(jī)來(lái)學(xué)習(xí)一下。

1 Windows Subsystem for Linux(WSL)簡(jiǎn)介

簡(jiǎn)介一下WSL。

Windows Subsystem for Linux(簡(jiǎn)稱WSL)是一個(gè)為在Windows 10上能夠原生運(yùn)行Linux二進(jìn)制可執(zhí)行文件(ELF格式)的兼容層。它是由微軟與Canonical公司合作開(kāi)發(fā),目標(biāo)是使純正的Ubuntu 14.04 "Trusty Tahr"映像能下載和解壓到用戶的本地計(jì)算機(jī),并且映像內(nèi)的工具和實(shí)用工具能在此子系統(tǒng)上原生運(yùn)行。WSL提供了一個(gè)微軟開(kāi)發(fā)的Linux兼容內(nèi)核接口(不包含Linux代碼),來(lái)自Ubuntu的用戶模式二進(jìn)制文件在其上運(yùn)行。該子系統(tǒng)不能運(yùn)行所有Linux軟件,例如那些圖形用戶界面,以及那些需要未實(shí)現(xiàn)的Linux內(nèi)核服務(wù)的軟件。不過(guò),這可以用在外部X服務(wù)器上運(yùn)行的圖形X Window系統(tǒng)緩解。此子系統(tǒng)起源于命運(yùn)多舛的Astoria項(xiàng)目,其目的是允許Android應(yīng)用運(yùn)行在Windows 10 Mobile上。此功能組件從Windows 10 Insider Preview build 14316開(kāi)始可用?!S基百科

2 WSL安裝

在開(kāi)始菜單里搜索"Microsoft Store"。

image

接著在"Microsoft Store"中搜索"Linux"。

image

從中選擇一個(gè)Linux的發(fā)行版安裝即可。我選的是Ubuntu 18.04 LTS,個(gè)人比較熟悉Ubuntu,同時(shí)也想試一下18.04版本。點(diǎn)擊獲取后,"Microsoft Store"就會(huì)自動(dòng)下載安裝。當(dāng)然安裝完后,你可以把它固定到開(kāi)始屏幕比較方便。

image

當(dāng)然現(xiàn)在啟動(dòng)WSL是失敗的,因?yàn)檫€得在Windows中啟用該功能。依舊是在開(kāi)始菜單搜索Windows功能。

image

接著打鉤"適用于Linux的Windows子系統(tǒng)"。

image

這個(gè)時(shí)候就能愉快地開(kāi)啟Ubuntu系統(tǒng)了(當(dāng)然一般上面勾選完需要重啟系統(tǒng),這類就跳過(guò)了)。

image

如果第一次啟動(dòng)的話,會(huì)要求你創(chuàng)建一個(gè)Linux的賬戶和密碼,熟悉Linux系統(tǒng)的同學(xué)應(yīng)該很清楚了,這就不多說(shuō)了,如果不熟悉的,建議去學(xué)習(xí)一下Linux的基礎(chǔ)管理和操作。這樣就愉快地可以開(kāi)始在Windows上玩耍Linux系統(tǒng)了。另外這個(gè)系統(tǒng)其實(shí)是安裝在如下的地址內(nèi),這樣子后期管理時(shí)也可以快速上手使用。

C:\Users\你的用戶名\AppData\Local\Packages\CanonicalGroupLimited.Ubuntu18.04onWindows_79rhkp1fndgsc\LocalState\rootfs

同時(shí)WSL直接將你的其他磁盤(pán)掛載到了mnt文件夾下面,可以直接通過(guò)/mnt/d訪問(wèn)。

cd /mnt/f
ls -l
image

如果覺(jué)得下載速度略慢的話,完全可以更新apt源文件來(lái)提速??梢钥吹皆?etc/apt下面的sources.list

image

我們這里在自己的F盤(pán)創(chuàng)建了一個(gè)WSL的工作空間,可以用來(lái)做各種相關(guān)的事情,譬如這里在更新apt源文件前,先備份到這個(gè)空間內(nèi)。需要提醒下,我這里都先運(yùn)行了sudo su,以root賬戶執(zhí)行。所以不需要在命令前加sudo,如果不是root賬戶,請(qǐng)加sudo。

mkdir /mnt/f/WSLworkspace
cp /etc/apt/sources.list /mnt/f/WSLworkspace/sourcesbackup.list
image
image

而替換源通常使用vim或者gedit來(lái)操作,這個(gè)時(shí)候知道我們的系統(tǒng)安裝路徑的好處就來(lái)了。只需要在Windows上打開(kāi)對(duì)應(yīng)文件夾下的sources.list(建議用Notepad++),復(fù)制粘貼就可以。給出幾個(gè)源。

清華

deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic main restricted universe multiverse
deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-updates main restricted universe multiverse
deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-updates main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-backports main restricted universe multiverse
deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-backports main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-security main restricted universe multiverse
deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-security main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-proposed main restricted universe multiverse
deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-proposed main restricted universe multiverse

阿里源

deb http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse

中科大

deb https://mirrors.ustc.edu.cn/ubuntu/ bionic main restricted universe multiverse
deb-src https://mirrors.ustc.edu.cn/ubuntu/ bionic main restricted universe multiverse
deb https://mirrors.ustc.edu.cn/ubuntu/ bionic-updates main restricted universe multiverse
deb-src https://mirrors.ustc.edu.cn/ubuntu/ bionic-updates main restricted universe multiverse
deb https://mirrors.ustc.edu.cn/ubuntu/ bionic-backports main restricted universe multiverse
deb-src https://mirrors.ustc.edu.cn/ubuntu/ bionic-backports main restricted universe multiverse
deb https://mirrors.ustc.edu.cn/ubuntu/ bionic-security main restricted universe multiverse
deb-src https://mirrors.ustc.edu.cn/ubuntu/ bionic-security main restricted universe multiverse
deb https://mirrors.ustc.edu.cn/ubuntu/ bionic-proposed main restricted universe multiverse
deb-src https://mirrors.ustc.edu.cn/ubuntu/ bionic-proposed main restricted universe multiverse

接著就可以進(jìn)行更新軟件了。

sudo apt-get update
sudo apt-get upgrade

3 WSL搭建數(shù)據(jù)科學(xué)環(huán)境

接下來(lái)就是搭建數(shù)據(jù)科學(xué)環(huán)境(Python和R)了。這里推薦安裝Anaconda,當(dāng)然如果想從頭開(kāi)始安裝也是允許的。先去Anaconda 官網(wǎng)找到最新的版本。我Windows上基本都是Python2,因此在這個(gè)版本上主要以Python3為主,就是圖中紅框的。下載鏈接如下。

image

https://repo.continuum.io/archive/Anaconda3-2018.12-Linux-x86_64.sh

接著用wget下載,可以改變路徑。

wget -P /mnt/f/WSLworkspace https://repo.continuum.io/archive/Anaconda3-2018.12-Linux-x86_64.sh 

接下來(lái)就是安裝Anaconda3。

bash /mnt/f/WSLworkspace/Anaconda3-2018.12-Linux-x86_64.sh

中間可以選擇安裝位置,這里默認(rèn)安裝在/root/Anaconda3,接著就等待安裝結(jié)束了。安裝過(guò)程中會(huì)提示是否將Anaconda3添加到環(huán)境變量,選yes。安裝完會(huì)提示是否要安裝Microsoft VS code,我建議可以不裝。接著再Linux命令行里敲入。

jupyter lab --allow-root

關(guān)于這個(gè)的配置有一些參考的內(nèi)容見(jiàn)后面的鏈接,這里不詳細(xì)展開(kāi),如果以root賬戶運(yùn)行的話必須加上--allow -root。

接著在Windows瀏覽器里訪問(wèn)http://localhost:8888/lab?。

image

除了Python3,我們還推薦裝個(gè)R,用于混合編程。這個(gè)時(shí)候畢竟本機(jī)已經(jīng)安裝了原生R,我決定在Linux上搭建一個(gè)Microsoft R Open(MRO)。同樣是打開(kāi)MRO官網(wǎng)

image

利用wget下載MRO。

wget -P /mnt/f/WSLworkspace https://mran.blob.core.windows.net/install/mro/3.5.1/microsoft-r-open-3.5.1.tar.gz

由于我用的Ubuntu18.04,還需要安裝libpng12庫(kù)。

wget -P /mnt/f/WSLworkspace https://mirrors.kernel.org/ubuntu/pool/main/libp/libpng/libpng12-0_1.2.54-1ubuntu1_amd64.deb
dpkg -i libpng12-01_1.2.54-1ubuntu1_amd64.deb

定位到MRO下載路徑。

cd /mnt/f/WSLworkspace
tar -xf microsoft-r-open-3.5.1.tar.gz
cd microsoft-r-open
./install.sh

接著需要看MRO和MKL的許可,先按回車看許可,接著按q鍵退出,按y進(jìn)行安裝。

image

R安裝好后,還需要配置到j(luò)upyter中。首先需要先安裝devtools,Ubuntu 18.04還需要先安裝libssl。

apt-get install libssl-dev

接著進(jìn)入到R中。

install.packages('devtools')
library(devtools)
install_github('IRKernel/IRkernel')

這邊發(fā)現(xiàn)IRkernel裝不上,還需要裝libcurl。

apt-get install libcurl4-openssl-dev

最后發(fā)現(xiàn)github連接老有問(wèn)題。

image

Github也有一個(gè)相同的issues,可以安裝離線包,在github上點(diǎn)擊clone or download,下載到文件夾中。

devtools::install('/mnt/f/WSLworkspacre/IRkernel-master')

終于安裝成功。最后在R里,再敲入如下命令。

IRkernel::installspec(name = 'ir35', displayname = 'MRO')

大功告成。

image

運(yùn)行一波代碼。

image
image
image

雖然R語(yǔ)言中文社區(qū)給出了比較完整的教程,但是具體搭建過(guò)程中,還是無(wú)數(shù)的坑。希望大家也是動(dòng)手實(shí)踐為主。最近發(fā)現(xiàn)好多需要在Linux系統(tǒng)上安裝的包和庫(kù),因此有這個(gè)環(huán)境非常地好。

參考鏈接:

windows10 Linux子系統(tǒng)文件目錄

再見(jiàn)虛擬機(jī)!在Win10中使用Linux版本的R和Python

Ubuntu 18.04 軟件源修改成國(guó)內(nèi)源(文件或界面形式)

設(shè)置 jupyter notebook 可遠(yuǎn)程訪問(wèn)

Running as root is not recommended. Use --allow-root to bypass

Running as root is not recommended. Use --allow-root to bypass

Github Issues:Installation of packages using Devtools Fails - error setting certificate

error setting certificate verify locations, install_github

無(wú)法安裝github上的R包:error setting certificate verify

Github Issues:Installation failed: Timeout was reached during installation of IRkernel

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容