近日看到R语言中文社区上一篇关于Windows的Linux子系统搭建Python和R环境的文章,加上虾神也在宣传,因而我也动手来玩一玩。以前很早就了解过WSL,但一直没动手使用,趁机来学习一下。git
简介一下WSL。github
Windows Subsystem for Linux(简称WSL)是一个为在Windows 10上可以原生运行Linux二进制可执行文件(ELF格式)的兼容层。它是由微软与Canonical公司合做开发,目标是使纯正的Ubuntu 14.04 "Trusty Tahr"映像能下载和解压到用户的本地计算机,而且映像内的工具和实用工具能在此子系统上原生运行。WSL提供了一个微软开发的Linux兼容内核接口(不包含Linux代码),来自Ubuntu的用户模式二进制文件在其上运行。该子系统不能运行全部Linux软件,例如那些图形用户界面,以及那些须要未实现的Linux内核服务的软件。不过,这能够用在外部X服务器上运行的图形X Window系统缓解。此子系统起源于命运多舛的Astoria项目,其目的是容许Android应用运行在Windows 10 Mobile上。此功能组件从Windows 10 Insider Preview build 14316开始可用。——维基百科编程
在开始菜单里搜索"Microsoft Store"。ubuntu
接着在"Microsoft Store"中搜索"Linux"。vim
从中选择一个Linux的发行版安装便可。我选的是Ubuntu 18.04 LTS,我的比较熟悉Ubuntu,同时也想试一下18.04版本。点击获取后,"Microsoft Store"就会自动下载安装。固然安装完后,你能够把它固定到开始屏幕比较方便。windows
固然如今启动WSL是失败的,由于还得在Windows中启用该功能。依旧是在开始菜单搜索Windows功能。浏览器
接着打钩"适用于Linux的Windows子系统"。bash
这个时候就能愉快地开启Ubuntu系统了(固然通常上面勾选完须要重启系统,这类就跳过了)。服务器
若是第一次启动的话,会要求你建立一个Linux的帐户和密码,熟悉Linux系统的同窗应该很清楚了,这就很少说了,若是不熟悉的,建议去学习一下Linux的基础管理和操做。这样就愉快地能够开始在Windows上玩耍Linux系统了。另外这个系统实际上是安装在以下的地址内,这样子后期管理时也能够快速上手使用。curl
C:\Users\你的用户名\AppData\Local\Packages\CanonicalGroupLimited.Ubuntu18.04onWindows_79rhkp1fndgsc\LocalState\rootfs
同时WSL直接将你的其余磁盘挂载到了mnt文件夹下面,能够直接经过/mnt/d访问。
cd /mnt/f ls -l
若是以为下载速度略慢的话,彻底能够更新apt源文件来提速。能够看到在/etc/apt下面的sources.list
咱们这里在本身的F盘建立了一个WSL的工做空间,能够用来作各类相关的事情,譬如这里在更新apt源文件前,先备份到这个空间内。须要提醒下,我这里都先运行了sudo su,以root帐户执行。因此不须要在命令前加sudo,若是不是root帐户,请加sudo。
mkdir /mnt/f/WSLworkspace cp /etc/apt/sources.list /mnt/f/WSLworkspace/sourcesbackup.list
而替换源一般使用vim或者gedit来操做,这个时候知道咱们的系统安装路径的好处就来了。只须要在Windows上打开对应文件夹下的sources.list(建议用Notepad++),复制粘贴就能够。给出几个源。
清华
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
接着就能够进行更新软件了。
sudo apt-get update sudo apt-get upgrade
接下来就是搭建数据科学环境(Python和R)了。这里推荐安装Anaconda,固然若是想从头开始安装也是容许的。先去Anaconda 官网找到最新的版本。我Windows上基本都是Python2,所以在这个版本上主要以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
中间能够选择安装位置,这里默认安装在/root/Anaconda3,接着就等待安装结束了。安装过程当中会提示是否将Anaconda3添加到环境变量,选yes。安装完会提示是否要安装Microsoft VS code,我建议能够不装。接着再Linux命令行里敲入。
jupyter lab --allow-root
关于这个的配置有一些参考的内容见后面的连接,这里不详细展开,若是以root帐户运行的话必须加上--allow -root。
接着在Windows浏览器里访问http://localhost:8888/lab?。
除了Python3,咱们还推荐装个R,用于混合编程。这个时候毕竟本机已经安装了原生R,我决定在Linux上搭建一个Microsoft R Open(MRO)。一样是打开MRO官网
利用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进行安装。
R安装好后,还须要配置到jupyter中。首先须要先安装devtools,Ubuntu 18.04还须要先安装libssl。
apt-get install libssl-dev
接着进入到R中。
install.packages('devtools') library(devtools) install_github('IRKernel/IRkernel')
这边发现IRkernel装不上,还须要装libcurl。
apt-get install libcurl4-openssl-dev
最后发现github链接老有问题。
Github也有一个相同的issues,能够安装离线包,在github上点击clone or download,下载到文件夹中。
devtools::install('/mnt/f/WSLworkspacre/IRkernel-master')
终于安装成功。最后在R里,再敲入以下命令。
IRkernel::installspec(name = 'ir35', displayname = 'MRO')
大功告成。
运行一波代码。
虽然R语言中文社区给出了比较完整的教程,可是具体搭建过程当中,仍是无数的坑。但愿你们也是动手实践为主。最近发现好多须要在Linux系统上安装的包和库,所以有这个环境很是地好。
参考连接:
再见虚拟机!在Win10中使用Linux版本的R和Python
Ubuntu 18.04 软件源修改为国内源(文件或界面形式)
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