最近有在window系统安装linux子系统(WSL, Windows Subsystem for Linux)的需求,搜索发现Microsoft最近(4月8号)提供了更加简便的安装方式。在此记录一下安装过程,的确非常简单。

image-20220422081111542

参考官方文档

根据文档说明,此方式只适用于Windows 10 版本 2004 及更高版本(内部版本 19041 及更高版本)或 Windows 11。

1、wsl安装

  • 进入window的cmd模式:Win+R
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
###(1) 选择wsl所支持的linux版本
wsl --list --online
# 以下是可安装的有效分发的列表。
# 请使用“wsl --install -d <分发>”安装。

# NAME            FRIENDLY NAME
# Ubuntu          Ubuntu
# Debian          Debian GNU/Linux
# kali-linux      Kali Linux Rolling
# openSUSE-42     openSUSE Leap 42
# SLES-12         SUSE Linux Enterprise Server v12
# Ubuntu-16.04    Ubuntu 16.04 LTS
# Ubuntu-18.04    Ubuntu 18.04 LTS
# Ubuntu-20.04    Ubuntu 20.04 LTS

###(2) 选择其中的Ubuntu版本进行安装
wsl --install -d Ubuntu
#如上会自动下载、安装、启动Ubuntu系统;然后根据提示设置用户名与密码。
#在创建第一个用户账号后,该账户就被视为系统管理员,能够运行`sudo`管理命令。

#已安装的发行版
wsl --list --verbose

#目前WSL有两个版本,WSL1与WSL2;WSL2相较于WSL1更贴近真实Linux内核
#可如下设置指定版本的WSL默认Linux发行版
wsl --set-version <distribution name> 1/2
wsl --set-default Ubuntu-20.04 2

理论上Ubuntu子系统就已经安装了,点击window的开始选项,会出现该图标,点击即可运行。

image-20220422082921582

2、关于密码

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
##(1) 只是想单纯修改密码,使用linux的password命令解决
#进入ubuntu系统
password
#然后根据提示输入旧密码,再输入新密码

##(2) 如果是忘记了密码,想要修改密码
#进入window的cmd模式
wsl -u root 
passwd <username>
# <username>为忘记密码的账户名
# 然后就可以直接设置新密码了

3、互访文件

  • linux子系统中访问window桌面

    1
    2
    3
    4
    5
    6
    
    ls /mnt/
    # c  d
    #对应window的c盘与d盘
    
    ## 进入window桌面文件夹,xiaoxin是我的用户名
    cd /mnt/c/Users/xiaoxin/Desktop/
    
  • window访问linux子系统的文件目录:在Windows资源管理器路径栏输入\\wsl$便可逐级查看

    image-20220422084826936

  • 关于ubuntu子系统的磁盘空间:https://docs.microsoft.com/zh-cn/windows/wsl/vhd-size

    image-20220422090526326

4、安装conda

  • 因为在安装conda过程中遇到了一个之前在服务器linux中没遇到的问题,特此记录一下。
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
##(1)下载、执行安装脚本
wget https://mirrors.tuna.tsinghua.edu.cn/anaconda/miniconda/Miniconda3-latest-Linux-x86_64.sh
bash Miniconda3-latest-Linux-x86_64.sh
source ~/.bashrc

##(2)添加镜像源
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free 
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/bioconda
conda config --set show_channel_urls yes

#查看已设置的channels
conda config --get

##然后此时直接使用conda安装软件时,会出现类似下面的报错
conda install -c anaconda ipython

CondaHTTPError: HTTP 000 CONNECTION FAILED for url https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge/linux-64/current_repodata.json Elapsed: -

An HTTP error occurred when trying to retrieve this URL. HTTP errors are often intermittent, and a simple retry will get you on your way. ‘https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge/linux-64'

网上搜了一些办法,例如更改镜像源、把https换为http,试了以下都不行。最后重启了一下电脑,就可以了。虽然还不知道为什么原因,但现在ubuntu子系统的conda算是正式安装好了,可以安装软件了。

1
2
3
4
conda install -c anaconda ipython
conda install -c anaconda jupyter
pip install numpy
pip install pandas