近日看到R語言中文社區(qū)上一篇關(guān)于Windows的Linux子系統(tǒng)搭建Python和R環(huán)境的文章麻汰,加上蝦神也在宣傳窟扑,于是我也動(dòng)手來玩一玩预皇。之前很早就了解過WSL撤蚊,但一直沒動(dòng)手使用,趁機(jī)來學(xué)習(xí)一下宜肉。
1 Windows Subsystem for Linux(WSL)簡介
簡介一下WSL匀钧。
Windows Subsystem for Linux(簡稱WSL)是一個(gè)為在Windows 10上能夠原生運(yùn)行Linux二進(jìn)制可執(zhí)行文件(ELF格式)的兼容層。它是由微軟與Canonical公司合作開發(fā)谬返,目標(biāo)是使純正的Ubuntu 14.04 "Trusty Tahr"映像能下載和解壓到用戶的本地計(jì)算機(jī)之斯,并且映像內(nèi)的工具和實(shí)用工具能在此子系統(tǒng)上原生運(yùn)行。WSL提供了一個(gè)微軟開發(fā)的Linux兼容內(nèi)核接口(不包含Linux代碼)遣铝,來自Ubuntu的用戶模式二進(jìn)制文件在其上運(yùn)行佑刷。該子系統(tǒng)不能運(yùn)行所有Linux軟件,例如那些圖形用戶界面酿炸,以及那些需要未實(shí)現(xiàn)的Linux內(nèi)核服務(wù)的軟件瘫絮。不過,這可以用在外部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開始可用廷支。——維基百科
2 WSL安裝
在開始菜單里搜索"Microsoft Store"栓辜。
接著在"Microsoft Store"中搜索"Linux"恋拍。
從中選擇一個(gè)Linux的發(fā)行版安裝即可。我選的是Ubuntu 18.04 LTS藕甩,個(gè)人比較熟悉Ubuntu施敢,同時(shí)也想試一下18.04版本。點(diǎn)擊獲取后狭莱,"Microsoft Store"就會(huì)自動(dòng)下載安裝僵娃。當(dāng)然安裝完后,你可以把它固定到開始屏幕比較方便腋妙。
當(dāng)然現(xiàn)在啟動(dòng)WSL是失敗的默怨,因?yàn)檫€得在Windows中啟用該功能。依舊是在開始菜單搜索Windows功能骤素。
接著打鉤"適用于Linux的Windows子系統(tǒng)"匙睹。
這個(gè)時(shí)候就能愉快地開啟Ubuntu系統(tǒng)了(當(dāng)然一般上面勾選完需要重啟系統(tǒng)愚屁,這類就跳過了)。
如果第一次啟動(dòng)的話痕檬,會(huì)要求你創(chuàng)建一個(gè)Linux的賬戶和密碼霎槐,熟悉Linux系統(tǒng)的同學(xué)應(yīng)該很清楚了,這就不多說了梦谜,如果不熟悉的丘跌,建議去學(xué)習(xí)一下Linux的基礎(chǔ)管理和操作。這樣就愉快地可以開始在Windows上玩耍Linux系統(tǒng)了唁桩。另外這個(gè)系統(tǒng)其實(shí)是安裝在如下的地址內(nèi)碍岔,這樣子后期管理時(shí)也可以快速上手使用。
C:\Users\你的用戶名\AppData\Local\Packages\CanonicalGroupLimited.Ubuntu18.04onWindows_79rhkp1fndgsc\LocalState\rootfs
同時(shí)WSL直接將你的其他磁盤掛載到了mnt文件夾下面朵夏,可以直接通過/mnt/d訪問蔼啦。
cd /mnt/f
ls -l
如果覺得下載速度略慢的話,完全可以更新apt源文件來提速仰猖∧笾可以看到在/etc/apt下面的sources.list
我們這里在自己的F盤創(chuàng)建了一個(gè)WSL的工作空間,可以用來做各種相關(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
而替換源通常使用vim或者gedit來操作佃却,這個(gè)時(shí)候知道我們的系統(tǒng)安裝路徑的好處就來了者吁。只需要在Windows上打開對(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)境
接下來就是搭建數(shù)據(jù)科學(xué)環(huán)境(Python和R)了灶泵。這里推薦安裝Anaconda育八,當(dāng)然如果想從頭開始安裝也是允許的。先去Anaconda 官網(wǎng)找到最新的版本赦邻。我Windows上基本都是Python2髓棋,因此在這個(gè)版本上主要以Python3為主,就是圖中紅框的深纲。下載鏈接如下仲锄。
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
接下來就是安裝Anaconda3儒喊。
bash /mnt/f/WSLworkspace/Anaconda3-2018.12-Linux-x86_64.sh
中間可以選擇安裝位置镣奋,這里默認(rèn)安裝在/root/Anaconda3,接著就等待安裝結(jié)束了怀愧。安裝過程中會(huì)提示是否將Anaconda3添加到環(huán)境變量侨颈,選yes。安裝完會(huì)提示是否要安裝Microsoft VS code芯义,我建議可以不裝哈垢。接著再Linux命令行里敲入。
jupyter lab --allow-root
關(guān)于這個(gè)的配置有一些參考的內(nèi)容見后面的鏈接扛拨,這里不詳細(xì)展開耘分,如果以root賬戶運(yùn)行的話必須加上--allow -root。
接著在Windows瀏覽器里訪問http://localhost:8888/lab?绑警。
除了Python3求泰,我們還推薦裝個(gè)R,用于混合編程计盒。這個(gè)時(shí)候畢竟本機(jī)已經(jīng)安裝了原生R渴频,我決定在Linux上搭建一個(gè)Microsoft R Open(MRO)。同樣是打開MRO官網(wǎng)
利用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庫。
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)行安裝惹悄。
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連接老有問題踩窖。
Github也有一個(gè)相同的issues,可以安裝離線包晨横,在github上點(diǎn)擊clone or download洋腮,下載到文件夾中箫柳。
devtools::install('/mnt/f/WSLworkspacre/IRkernel-master')
終于安裝成功。最后在R里啥供,再敲入如下命令悯恍。
IRkernel::installspec(name = 'ir35', displayname = 'MRO')
大功告成。
運(yùn)行一波代碼伙狐。
雖然R語言中文社區(qū)給出了比較完整的教程涮毫,但是具體搭建過程中,還是無數(shù)的坑贷屎。希望大家也是動(dòng)手實(shí)踐為主罢防。最近發(fā)現(xiàn)好多需要在Linux系統(tǒng)上安裝的包和庫,因此有這個(gè)環(huán)境非常地好唉侄。
參考鏈接:
再見虛擬機(jī)咒吐!在Win10中使用Linux版本的R和Python
Ubuntu 18.04 軟件源修改成國內(nèi)源(文件或界面形式)
設(shè)置 jupyter notebook 可遠(yuǎ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
無法安裝github上的R包:error setting certificate verify
Github Issues:Installation failed: Timeout was reached during installation of IRkernel