[环境配置] Archwsl安装与配置教程

6 分钟阅读时长

发布时间:

目录

介绍

相较于传统虚拟机, wsl实现了低成本高效率地在windows中有效执行linux环境, 而archlinux属于linux中一个独特的发行版, 拥有足够新且省心的pacman包与体系完善的环境, 以下是从零开始对archwsl的安装与配置教程.

安装wsl

安装wsl1

微软官方给出了一套最为方便的安装路径, 使用管理员权限打开powershell运行以下指令:

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

安装archwsl

此步骤需要在安装wsl1后进行, 否则wsl无法识别archwsl. 在微软商店搜索Arch WSL(如果无法安装表示系统版本需要更新), 完成后重启电脑尝试打开archwsl, 有概率存在错误, 以下分别列举各种报错的可能性.

如果看到等待界面, 一段时间后需要输入用户名/输入密码/确认密码, 则完成后archwsl安装成功.

错误处理

以下是面对错误情况时的一些处理机制:

  1. 版本不对, 一些情况下wsl版本默认为wsl2, 但是教程配置为wsl1, 需要手动切换, 可在powershell中运行指令切换为wsl1: wsl --set-default-version 1
  2. 系统设置错误, 设置>应用>应用和功能>程序和功能>启用或关闭Windows功能打开适用于Linux的Windows子系统, 然后重启电脑.
  3. dns污染, 可在设置>网络和Internet>状态>更改适配器选项>WLAN>属性>Internet协议版本4(TCP/IPV4)修改为使用下面的DNS服务器地址, 地址可以更改为8:8:8.8, 8:8:4.4.

配置archwsl

更新archlinux源

相比完善的debian系, arch需要稍微花点时间, 获取arch以后第一步应该先对源进行更新, 在国内获取源会很慢, 故需要使用国内的镜像源, 首先是找到文件/etc/pacman.d/mirrorlist, 在首行写入:

Server = https://mirrors.ustc.edu.cn/archlinux/$repo/os/$arch
Server = https://mirrors.tuna.tsinghua.edu.cn/archlinux/$repo/os/$arch

保存后执行更新指令:

sudo pacman -Syyu
# 未来在生产环境应该采用-Syu进行安全更新.
# sudo pacman -Syu

更新archlinuxcn源

除了官方源, arch中国还提供了自己维护的源可以使用, 找到文件/etc/pacman.conf, 在行尾写入:

[archlinuxcn]
Server = https://mirrors.ustc.edu.cn/archlinuxcn/$arch
Server = https://mirrors.tuna.tsinghua.edu.cn/archlinuxcn/$arch

保存后执行更新指令:

sudo pacman -Sy archlinuxcn-keyring

中文环境

在windows终端显示中文字体可以不需要安装archlinux的中文字体, 但是缺少中文环境时linux内核会识别中文为编码形式, 直观影响有:

  • 中文文件以编码形式进行显示
  • 输入中文后无法正常删去, 容易使终端混乱(因为windows终端的1个2字节中文实际上在linux中识别为4字节的编码形式)

我们需要修改为中文环境(不需要安装中文字体).在文件/etc/locale.gen中找到:

en_US.UTF-8 UTF-8
zh_CN.UTF-8 UTF-8

并取消注释, 然后执行指令:

sudo locale-gen

配置编程环境

配置vim

vim是终端上极为强大的文本编辑器, 首先安装vim:

sudo pacman -S vim vim-youcompleteme-git

vim可在文件/usr/share/vim/vim91/defaults.vim追加脚本以实现编译运行功能, 以下以个人配置作为参考:

au BufNewFile,BufRead *.d2          setf d2

map <F5> : call CompileRunGcc()<CR>
func! CompileRunGcc()
        exec "w"
        if &filetype == "python"
                !time python %
        elseif &filetype == "c"
                !clang % -o %< && time ./%<
        elseif &filetype == "cpp"
                !clang++ % -o %< && time ./%<
        elseif &filetype == "go"
                !time go run %
        elseif &filetype == "r"
                !Rscript %
        elseif &filetype == "sh"
                !time bash ./%
        elseif &filetype == "rust"
                !rustc % && time ./%<
        elseif &filetype == "markdown"
                !vnote %
        elseif &filetype == "toml"
                !cargo build
        elseif &filetype == "cs"
                !mcs % -out:%<.exe && time mono %<.exe
        elseif &filetype == "asm"
                !nasm -f elf64 % -o %<.o && ld %<.o -o %< && time ./%<
        elseif &filetype == "v"
                !v % && time ./%<
        elseif &filetype == "java"
                !java %
        elseif &filetype == "d2"
                !d2 %
        endif
endfunc

map <F6> : call SpecialCompilation()<CR>
func! SpecialCompilation()
        exec "w"
        if &filetype == "rust"
                !cargo run
        elseif &filetype == "c"
                !sdcc % -o %<.ihx && packihx %<.ihx > %<.hex
        endif
endfunc

map <F7> : call SpecialCompilation()<CR>
func! SpecialCompilation()
        exec "w"
        if &filetype == "c"
                !time x86_64-w64-mingw32-gcc % -o %<.exe && time ./%<.exe
        endif
endfunc

:set number

配置sage

sage是archlinux引以为傲包之一, 由于arch特有的理念, 强大的数学编程语言sage在arch中一直处于较为新的版本, 安装指令如下:

sudo pacman -S sagemath

配置终端

配置终端主要是为了美观, 以下提供一个本人自用的终端配置, 可以在设置>打开JSON文件中进行替换:

{
    "$help": "https://aka.ms/terminal-documentation",
    "$schema": "https://aka.ms/terminal-profiles-schema",
    "actions": 
    [
        {
            "command": 
            {
                "action": "copy",
                "singleLine": false
            }
        },
        {
            "command": "paste"
        },
        {
            "command": "find",
            "keys": "ctrl+shift+f"
        },
        {
            "command": "unbound",
            "keys": "ctrl+v"
        },
        {
            "command": "unbound",
            "keys": "ctrl+c"
        },
        {
            "command": "unbound",
            "keys": "ctrl+w"
        },
        {
            "command": 
            {
                "action": "splitPane",
                "split": "auto",
                "splitMode": "duplicate"
            },
            "keys": "alt+shift+d"
        },
        {
            "command": 
            {
                "action": "newTab"
            }
        }
    ],
    "alwaysShowTabs": false,
    "autoHideWindow": false,
    "copyFormatting": "none",
    "copyOnSelect": false,
    "defaultProfile": "{a5a97cb8-8961-5535-816d-772efe0c6a3f}",
    "disableAnimations": true,
    "initialCols": 40,
    "initialRows": 35,
    "minimizeToNotificationArea": false,
    "newTabMenu": 
    [
        {
            "type": "remainingProfiles"
        }
    ],
    "profiles": 
    {
        "defaults": 
        {
            "colorScheme": "archcolor"
        },
        "list": 
        [
            {
                "commandline": "%SystemRoot%\\System32\\WindowsPowerShell\\v1.0\\powershell.exe",
                "guid": "{61c54bbd-c2c6-5271-96e7-009a87ff44bf}",
                "hidden": false,
                "name": "Windows PowerShell"
            },
            {
                "commandline": "%SystemRoot%\\System32\\cmd.exe",
                "guid": "{0caa0dad-35be-5f56-a8ff-afceeeaa6101}",
                "hidden": false,
                "name": "\u547d\u4ee4\u63d0\u793a\u7b26"
            },
            {
                "antialiasingMode": "aliased",
                "backgroundImage": null,
                "bellStyle": "none",
                "cursorShape": "emptyBox",
                "font": 
                {
                    "face": "Consolas",
                    "size": 11.0,
                    "weight": "extra-light"
                },
                "guid": "{a5a97cb8-8961-5535-816d-772efe0c6a3f}",
                "hidden": false,
                "historySize": 1000,
                "icon": "D:\\work\\file\\picture\\arch.png",
                "intenseTextStyle": "all",
                "name": "Arch",
                "opacity": 80,
                "scrollbarState": "hidden",
                "source": "Windows.Terminal.Wsl"
            },
            {
                "guid": "{b453ae62-4e3d-5e58-b989-0a998ec441b8}",
                "hidden": false,
                "name": "Azure Cloud Shell",
                "source": "Windows.Terminal.Azure"
            },
            {
                "antialiasingMode": "aliased",
                "cursorShape": "emptyBox",
                "font": 
                {
                    "face": "Consolas",
                    "size": 11.0,
                    "weight": "extra-light"
                },
                "guid": "{58ad8b0c-3ef8-5f4d-bc6f-13e4c00f2530}",
                "hidden": false,
                "historySize": 1000,
                "icon": "D:\\work\\file\\picture\\debian.png",
                "intenseTextStyle": "all",
                "name": "Debian",
                "opacity": 80,
                "scrollbarState": "hidden",
                "source": "Windows.Terminal.Wsl"
            }
        ]
    },
    "schemes": 
    [
        {
            "background": "#0C0C0C",
            "black": "#0C0C0C",
            "blue": "#0037DA",
            "brightBlack": "#767676",
            "brightBlue": "#3B78FF",
            "brightCyan": "#61D6D6",
            "brightGreen": "#16C60C",
            "brightPurple": "#B4009E",
            "brightRed": "#E74856",
            "brightWhite": "#F2F2F2",
            "brightYellow": "#F9F1A5",
            "cursorColor": "#FFFFFF",
            "cyan": "#3A96DD",
            "foreground": "#CCCCCC",
            "green": "#13A10E",
            "name": "Campbell",
            "purple": "#881798",
            "red": "#C50F1F",
            "selectionBackground": "#FFFFFF",
            "white": "#CCCCCC",
            "yellow": "#C19C00"
        },
        {
            "background": "#012456",
            "black": "#0C0C0C",
            "blue": "#0037DA",
            "brightBlack": "#767676",
            "brightBlue": "#3B78FF",
            "brightCyan": "#61D6D6",
            "brightGreen": "#16C60C",
            "brightPurple": "#B4009E",
            "brightRed": "#E74856",
            "brightWhite": "#F2F2F2",
            "brightYellow": "#F9F1A5",
            "cursorColor": "#FFFFFF",
            "cyan": "#3A96DD",
            "foreground": "#CCCCCC",
            "green": "#13A10E",
            "name": "Campbell Powershell",
            "purple": "#881798",
            "red": "#C50F1F",
            "selectionBackground": "#FFFFFF",
            "white": "#CCCCCC",
            "yellow": "#C19C00"
        },
        {
            "background": "#282C34",
            "black": "#282C34",
            "blue": "#61AFEF",
            "brightBlack": "#5A6374",
            "brightBlue": "#61AFEF",
            "brightCyan": "#56B6C2",
            "brightGreen": "#98C379",
            "brightPurple": "#C678DD",
            "brightRed": "#E06C75",
            "brightWhite": "#DCDFE4",
            "brightYellow": "#E5C07B",
            "cursorColor": "#FFFFFF",
            "cyan": "#56B6C2",
            "foreground": "#DCDFE4",
            "green": "#98C379",
            "name": "One Half Dark",
            "purple": "#C678DD",
            "red": "#E06C75",
            "selectionBackground": "#FFFFFF",
            "white": "#DCDFE4",
            "yellow": "#E5C07B"
        },
        {
            "background": "#FAFAFA",
            "black": "#383A42",
            "blue": "#0184BC",
            "brightBlack": "#4F525D",
            "brightBlue": "#61AFEF",
            "brightCyan": "#56B5C1",
            "brightGreen": "#98C379",
            "brightPurple": "#C577DD",
            "brightRed": "#DF6C75",
            "brightWhite": "#FFFFFF",
            "brightYellow": "#E4C07A",
            "cursorColor": "#4F525D",
            "cyan": "#0997B3",
            "foreground": "#383A42",
            "green": "#50A14F",
            "name": "One Half Light",
            "purple": "#A626A4",
            "red": "#E45649",
            "selectionBackground": "#FFFFFF",
            "white": "#FAFAFA",
            "yellow": "#C18301"
        },
        {
            "background": "#002B36",
            "black": "#002B36",
            "blue": "#268BD2",
            "brightBlack": "#073642",
            "brightBlue": "#839496",
            "brightCyan": "#93A1A1",
            "brightGreen": "#586E75",
            "brightPurple": "#6C71C4",
            "brightRed": "#CB4B16",
            "brightWhite": "#FDF6E3",
            "brightYellow": "#657B83",
            "cursorColor": "#FFFFFF",
            "cyan": "#2AA198",
            "foreground": "#839496",
            "green": "#859900",
            "name": "Solarized Dark",
            "purple": "#D33682",
            "red": "#DC322F",
            "selectionBackground": "#FFFFFF",
            "white": "#EEE8D5",
            "yellow": "#B58900"
        },
        {
            "background": "#FDF6E3",
            "black": "#002B36",
            "blue": "#268BD2",
            "brightBlack": "#073642",
            "brightBlue": "#839496",
            "brightCyan": "#93A1A1",
            "brightGreen": "#586E75",
            "brightPurple": "#6C71C4",
            "brightRed": "#CB4B16",
            "brightWhite": "#FDF6E3",
            "brightYellow": "#657B83",
            "cursorColor": "#002B36",
            "cyan": "#2AA198",
            "foreground": "#657B83",
            "green": "#859900",
            "name": "Solarized Light",
            "purple": "#D33682",
            "red": "#DC322F",
            "selectionBackground": "#FFFFFF",
            "white": "#EEE8D5",
            "yellow": "#B58900"
        },
        {
            "background": "#000000",
            "black": "#000000",
            "blue": "#3465A4",
            "brightBlack": "#555753",
            "brightBlue": "#729FCF",
            "brightCyan": "#34E2E2",
            "brightGreen": "#8AE234",
            "brightPurple": "#AD7FA8",
            "brightRed": "#EF2929",
            "brightWhite": "#EEEEEC",
            "brightYellow": "#FCE94F",
            "cursorColor": "#FFFFFF",
            "cyan": "#06989A",
            "foreground": "#D3D7CF",
            "green": "#4E9A06",
            "name": "Tango Dark",
            "purple": "#75507B",
            "red": "#CC0000",
            "selectionBackground": "#FFFFFF",
            "white": "#D3D7CF",
            "yellow": "#C4A000"
        },
        {
            "background": "#FFFFFF",
            "black": "#000000",
            "blue": "#3465A4",
            "brightBlack": "#555753",
            "brightBlue": "#729FCF",
            "brightCyan": "#34E2E2",
            "brightGreen": "#8AE234",
            "brightPurple": "#AD7FA8",
            "brightRed": "#EF2929",
            "brightWhite": "#EEEEEC",
            "brightYellow": "#FCE94F",
            "cursorColor": "#000000",
            "cyan": "#06989A",
            "foreground": "#555753",
            "green": "#4E9A06",
            "name": "Tango Light",
            "purple": "#75507B",
            "red": "#CC0000",
            "selectionBackground": "#FFFFFF",
            "white": "#D3D7CF",
            "yellow": "#C4A000"
        },
        {
            "background": "#000000",
            "black": "#000000",
            "blue": "#000080",
            "brightBlack": "#808080",
            "brightBlue": "#0000FF",
            "brightCyan": "#00FFFF",
            "brightGreen": "#00FF00",
            "brightPurple": "#FF00FF",
            "brightRed": "#FF0000",
            "brightWhite": "#FFFFFF",
            "brightYellow": "#FFFF00",
            "cursorColor": "#FFFFFF",
            "cyan": "#008080",
            "foreground": "#C0C0C0",
            "green": "#008000",
            "name": "Vintage",
            "purple": "#800080",
            "red": "#800000",
            "selectionBackground": "#FFFFFF",
            "white": "#C0C0C0",
            "yellow": "#808000"
        },
        {
            "background": "#000000",
            "black": "#545454",
            "blue": "#435BFF",
            "brightBlack": "#A6A6A6",
            "brightBlue": "#3E71D4",
            "brightCyan": "#01D7E8",
            "brightGreen": "#049600",
            "brightPurple": "#810096",
            "brightRed": "#9E0104",
            "brightWhite": "#E0E0E0",
            "brightYellow": "#C7C402",
            "cursorColor": "#FFFFFF",
            "cyan": "#03E1FF",
            "foreground": "#FFFFFF",
            "green": "#33FC03",
            "name": "archcolor",
            "purple": "#A100E8",
            "red": "#DB0009",
            "selectionBackground": "#FFFFFF",
            "white": "#FFFFFF",
            "yellow": "#EBDC01"
        }
    ],
    "showTabsInTitlebar": false,
    "showTerminalTitleInTitlebar": true,
    "theme": "light",
    "themes": 
    [
        {
            "name": "legacyDark",
            "tab": 
            {
                "background": null,
                "showCloseButton": "always",
                "unfocusedBackground": null
            },
            "window": 
            {
                "applicationTheme": "dark",
                "useMica": false
            }
        },
        {
            "name": "legacyLight",
            "tab": 
            {
                "background": null,
                "showCloseButton": "always",
                "unfocusedBackground": null
            },
            "window": 
            {
                "applicationTheme": "light",
                "useMica": false
            }
        },
        {
            "name": "legacySystem",
            "tab": 
            {
                "background": null,
                "showCloseButton": "always",
                "unfocusedBackground": null
            },
            "window": 
            {
                "applicationTheme": "system",
                "useMica": false
            }
        }
    ]
}