Windows11 WSL Ubuntu1804 配置指南

Why select WSL Ubuntu?

  1. 物理机安装桌面版,配置国际互联网连接很困难,所以需要和Windows11 配合使用,无需担心学习过程中查资料的阻碍;
  2. 希望通过Anaconda配套的Python环境,来绘制函数图像,验证函数极限的一些计算,数形结合;

Update and date

  1. 2023年7月7日(更新以及编辑,增加固定IP方式,更新Ubuntu内WSL配置)

Requirements for Reader

  1. 应熟练掌握Linux常用命令,包括不限于文件管理,权限管理,网络下载文件;
  2. 充分理解WSL Ubuntu只是一种程序安装环境的选择,同样可以在Linux发行版 桌面版安装,或者在服务器Linux版本上安装,又或者是直接使用Windows安装Anaconda;
  3. 特别声明:本文作者使用较为麻烦的操作实现,是在以上三种方案当中做的折中选择,可以兼顾Windows平台,访问国际互联网,本地使用Python3.10.4便于LeetCode刷题的需要。
  4. 特别声明:安装步骤中,Terminal && Powershell 并非必选项,我安装前者是为了美化终端,安装后者是为了Pycharm调用Powershell可以解决不必要的权限限制。

Notification

  • 本人已安装和使用 Ubuntu20.04.6LTS版本,不对以上版本信息有效性做出保证,但配置方案均保证有效,尤其是针对WSL配置的固定IP,不对宿主机与WSL的localhost的绑定的有效性做出保证。
  • 必须安装 Windows Subsystem for Linux,否则WSL每次开机后IP地址都会变动,对于安装在Ubuntu中的Docker而言,作为Docker Web 控制面板的 portainer 每次都需要重新根据容器IP地址访问并且登录
  • WSL 的相关配置,应当查阅微软官方文档,可以保持较好的更新,链接在底部。其余文章仅作参考,但固定IP地址的方法,早年通过 Hyper-V建立虚拟交换机解决,如今不可以,但配置依然可以参考官方文档。
  • 即使指定了WSL的版本,仍然需要到微软官方文档下载WSL2的内核,才能顺利启动Ubuntu。

Steps

控制面板 —> 开启或者关闭Windows更新,勾选 WSL和 Hyper-V

在这里插入图片描述

Command

bcdedit /set hypervisorlaunchtype auto

# 安装WSL
wsl --install

# 启用WSL
dism.exe /online /enable-feature /featurename:Microsoft-Windows-Subsystem-Linux /all /norestart

# 开启虚拟化
dism.exe /online /enable-feature /featurename:VirtualMachinePlatform /all /norestart

# 重启机器
Restart-Computer

# 将WSL设置为默认
wsl.exe --set-default-version 2

Install Windows Terminal

在这里插入图片描述
https://www.microsoft.com/store/productId/9N0DX20HK701

Install Ubuntu 1804

在这里插入图片描述
https://www.microsoft.com/store/productId/9P9TQF7MRM4R

Install Windows Subsystem for Linux

https://www.microsoft.com/store/productId/9P9TQF7MRM4R

Install Powershell

在这里插入图片描述
https://github.com/PowerShell/PowerShell/releases/tag/v7.2.2

Ubuntu Update

注意:

  1. 实测更新速度为 2MB/s,基本满足需要,无需换源,无需换源是因为官方仓库版本最稳定,更新的包可能导致Python项目的包依赖以及编译包失败
  2. 创建用户非 root用户,需要手动提权,普通用户使用 Docker全程需要使用 sudo作为整行命令前缀。
# 更新数据
sudo apt-get update
# 升级软件,更多命令请使用 sudo apt --help查看
sudo apt-get full-upgrade

Download and Install Anaconda

可换用清华源下载,只需要包名相同,但切记,勿使用 wget命令,可以浏览器下载经文件管理器拷贝

在这里插入图片描述

# 进入用户家目录
cd /surpass/home
# 创建文件夹
mkdir download
# 提权,避免程序运行过程中,因为权限不足导致的创建文件及文件夹失败
sudo su
weget https://repo.anaconda.com/archive/Anaconda3-2021.11-Linux-x86_64.sh
bash Anaconda3-2021.11-Linux-x86_64.sh

Conda update

更新所有库
conda update --all
更新 conda 自身
conda update conda
更新 anaconda 自身
conda update anaconda

Install and Start Jupyter notebook

pip install jupyter notebook
jupyter notebook

在这里插入图片描述

Management of memory and IP Address

用户目录下新建,wsconfig,写入如下内容#.wslconfig

# Settings apply across all Linux distros running on WSL 2
[wsl2]

# Limits VM memory to use no more than 4 GB, this can be set as whole numbers using GB or MB
memory=6GB 

# Sets the VM to use two virtual processors
processors=8


# Sets amount of swap storage space to 8GB, default is 25% of available RAM
swap=8GB


# Turn off default connection to bind WSL 2 localhost to Windows localhost
localhostforwarding=true

# Disables nested virtualization
nestedVirtualization=false

etc/wsl.conf写入如下内容,无则新增

[sudo] password for tylor:
[boot]
systemd=true
[network]
generateHosts = false
generateResolvConf = false

# Set a command to run when a new WSL instance launches. This example starts the Docker container service.
[boot]
# command = service docker start

配置的解析可参考以下微软官方文档,链接如下:

https://learn.microsoft.com/zh-cn/windows/wsl/wsl-config

Checkup

在这里插入图片描述

Reference

安装WSL
https://zhuanlan.zhihu.com/p/462737849

https://zhuanlan.zhihu.com/p/475462241

https://zhuanlan.zhihu.com/p/462737849

# 微软关于WSL的官方文档
https://docs.microsoft.com/zh-cn/windows/wsl/basic-commands#:~:text=wsl%20--install%20--distribution%20%3CDistribution%20Name%3E%20%E9%80%9A%E8%BF%87%E5%B0%86%20%3CDistribution%20Name%3E,Linux%20%E5%8F%91%E8%A1%8C%E7%89%88%E8%BF%9B%E8%A1%8C%E5%AE%89%E8%A3%85%E3%80%82%20%E6%AD%A4%E5%91%BD%E4%BB%A4%E4%B9%9F%E5%8F%AF%E8%BE%93%E5%85%A5%E4%B8%BA%EF%BC%9A%20wsl%20-d%20%3CDistribution%20Name%3E%20%E3%80%82
  • 3
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值