Skip to content

Instantly share code, notes, and snippets.

@orzklv
Last active February 23, 2023 07:11
Show Gist options
  • Save orzklv/4ac44921d9035951554a973fd1019eee to your computer and use it in GitHub Desktop.
Save orzklv/4ac44921d9035951554a973fd1019eee to your computer and use it in GitHub Desktop.
How to install Arch Linux on WSL from scratch tutorial

Arch on WSL 2

These are the basic instructions to build Arch on WSL 2.

  • For instructions on bootstrapping WSL 1, please go here.

Table of Contents


Installing Arch Linux for WSL 2 from bootstrap

  1. Install/upgrade to the Windows Subsystem for Linux 2. Go here.

  2. Install your favorite available distro from the Windows Store, then launch it from the Start menu.

    • Steps 3-8 should be completed on a linux system
  3. Download the Arch Linux bootstrap (latest version at time of writing).

    wget https://mirrors.edge.kernel.org/archlinux/iso/latest/archlinux-bootstrap-2020.07.01-x86_64.tar.gz

  4. Extract the image.

    sudo tar -zxvf archlinux-bootstrap-2020.07.01-x86_64.tar.gz

  5. Enter the extraced directory root.x86_64

    cd root.x86_64

  6. Uncomment some servers in the pacman mirrorlist.

    vim etc/pacman.d/mirrorlist

  7. Recompress files in root.x86_64 directory.

    sudo tar -czvf root.tar.gz *

  8. Move the root.tar.gz file to an accessible Windows directory.

    sudo mv root.tar.gz /mnt/c/Users/USERNAME/root.tar.gz

    • After this step, you no longer need the temporarily installed linux system
  9. Open a PowerShell prompt as Admin, and issue the following command:

    wsl --import Archlinux PATH_WHERE_VHD_SHOULD_BE_CREATED C:\Users\USERNAME\root.tar.gz

  10. Launch Archlinux from WSL.

    wsl -d Archlinux

  11. Initialize Arch keyring.

    pacman-key --init
    pacman-key --populate archlinux
  12. Install base.

    pacman -Syyu base base-devel git vim wget reflector fish

  13. Enable multilib (if you want).

    linenumber=$(grep -nr "\\#\\[multilib\\]" /etc/pacman.conf | gawk '{print $1}' FS=":")
    sed -i "${linenumber}s:.*:[multilib]:" /etc/pacman.conf
    linenumber=$((linenumber+1))
    sed -i "${linenumber}s:.*:Include = /etc/pacman.d/mirrorlist:" /etc/pacman.conf
  14. Sync package databases.

    pacman -Syy

  15. Update mirror list (replace United States with preferred repo mirror country).

    reflector --country "United States" --age 12 --protocol https --sort rate --save /etc/pacman.d/mirrorlist

  16. Set root user password.

    passwd

  17. Create new user.

    useradd -m -G wheel -s /bin/fish -d /home/username username

  18. Set password on user.

    passwd username

  19. Enable wheel group.

    sed -i '/%wheel ALL=(ALL) ALL/c\%wheel ALL=(ALL) ALL' /etc/sudoers

  20. Edit Arch locale and regenerate.

    sed -i 's:#en_US.UTF-8 UTF-8:en_US.UTF-8 UTF-8:g' /etc/locale.gen
    locale-gen
    echo LANG=en_US.UTF-8 >> /etc/locale.conf
    echo LANGUAGE=en_US.UTF-8 >> /etc/locale.conf
    echo LC_ALL=en_US.UTF-8 >> /etc/locale.confS

Install Yay AUR Helper and Pacman Wrapper

  1. Create a directory for the yay PKGBUILD files and enter it.

    mkdir ~/yay cd ~/yay

  2. Download yay PKGBUILD from AUR.

    wget "https://aur.archlinux.org/cgit/aur.git/plain/PKGBUILD?h=yay" --output-document=./PKGBUILD

  3. Run makepkg to build and install yay.

    makepkg -si

  • Alternatively, you could use the instructions from the official Yay Github repo)

Configure the Arch Linux WSL install for systemd

This guid is based off the information found on WSL.dev.

  1. Install daemonize.

    yay -S daemonize

  2. Create the wsl.conf file on the system.

    vim /etc/wsl.conf

    [automount]
    enabled = true
    options = "metadata,uid=1000,gid=1000,umask=22,fmask=11,case=off"
    mountFsTab = true
    crossDistro = true
    [network]
    generateHosts = false
    generateResolvConf = true
    [interop]
    enabled = true
    appendWindowsPath = true
    [user]
    default = your_username
  3. Create a startup script daemonize systemd as PID 1.

    vim /etc/profile.d/00-wsl2-systemd.sh

    SYSTEMD_PID=$(ps -ef | grep '/lib/systemd/systemd --system-unit=basic.target$' | grep -v unshare | awk '{print $2}')
    if [ -z "$SYSTEMD_PID" ]; then
       sudo /usr/bin/daemonize /usr/bin/unshare --fork --pid --mount-proc /lib/    systemd/systemd --system-unit=basic.target
       SYSTEMD_PID=$(ps -ef | grep '/lib/systemd/systemd --system-unit=basic.    target$' | grep -v unshare | awk '{print $2}')
    fi
    if [ -n "$SYSTEMD_PID" ] && [ "$SYSTEMD_PID" != "1" ]; then
        exec sudo /usr/bin/nsenter -t $SYSTEMD_PID -a su - $LOGNAME
    fi
  4. Exit out of WSL, and re-enter. It should now be running systemd.

    • If you have any issues with the network, manually set a public DNS entry.

Install and Configure Windows Terminal

  1. From the Windows Store, install Windows Terminal.

  2. Copy any .png or .ico that you would like to use as distro icons to the following folder:

    %LOCALAPPDATA%\packages\Microsoft.WindowsTerminal_8wekyb3d8bbwe\RoamingState\

  3. Edit the settings.json file for Windows Terminal and add the following line to the list item:

    "icon" : "ms-appdata:///roaming/filename.ico",


Powerline Fonts

Download and install fonts for Powerline. Download here.


Launch X11 apps from the shell to Windows display

  1. Download, install and then launch VcXsrv.

    • Select display settings - Take default
    • Select how to start clients - Take default
    • Extra settings - Be sure Disable access control is checked.
    • Configuration complete - Click Finish
  2. Create a firewall rule in Windows to allow communication from WSL 2 to host OS.

    New-NetFirewallRule -DisplayName "X Server - WSL 2" -Direction Inbound -Program "C:\Program Files\VcXsrv\vcxsrv.exe" -Action Allow

  3. Get the IP of your local computer of the vEthernet (WSL) interface from CMD or PowerShell:

    ipconfig

  4. Export output to display using IP address collected in step 3.

    For bash and zsh: export DISPLAY=192.168.1.100:0

    For fish: set -x DISPLAY 192.168.1.100:0

    Note: Add to ~/.bashrc, ~/.zshrc, or ~/.config/fish/fish.config and you won't need to type it again on the next WSL launch.

  5. Install an xorg app for testing (We will use the Xorg Calculator).

    sudo pacman -S xorg-xcalc

  6. Launch xcalc to test.

    xcalc

  7. If VcXsrv is working properly, Xorg Calculator should popup as a new window.

Arch on WSL

These are the basic instructions to build Arch on WSL.

These instructions will not allow you to install from PKGBUILD files. makepkg will not build packages on Arch WSL without a custom compiled fakeroot. The kernel in WSL does not support sysv, so you would need to build a copy of fakeroot-tcp from source, then install it. Basic instructions are included in the file fakeroot.md.


Installing Arch WSL from bootstrap

  1. Install the Windows Subsystem for Linux. Open PowerShell and enter:

    Enable-WindowsOptionalFeature -Online -FeatureName Microsoft-Windows-Subsystem-Linux

  2. Reboot, when prompted.

  3. Open Windows Command Prompt.

    cmd

  4. Uninstall any existing installs.

    lxrun /uninstall /full /y

  5. Install Ubuntu base.

    lxrun /install /y

  6. Enter root bash.

    bash ~

  7. Get the Arch Linux bootstrap (latest version at time of writing).

    wget https://mirrors.kernel.org/archlinux/iso/latest/archlinux-bootstrap-2018.04.01-x86_64.tar.gz

  8. Extract the image.

    tar -zxvf archlinux-bootstrap-2018.04.01-x86_64.tar.gz

  9. Uncomment some servers in the pacman mirrorlist.

    vim ~/root.x86_64/etc/pacman.d/mirrorlist

  10. Update name servers (swap IP's for preferred DNS).

echo "nameserver 8.8.8.8" >> ~/root.x86_64/etc/resolv.conf

echo "nameserver 8.8.4.4" >> ~/root.x86_64/etc/resolv.conf

  1. Exit bash.

    exit

  2. Go to lxss directory and rename rootfs directory.

    %LOCALAPPDATA%\lxss\ and rename directory rootfs to rootfs_old

  3. Now, go here and and rename extracted Arch root.x86_64.

    %LOCALAPPDATA%\lxss\root\ and rename directory root.x86_64 to rootfs

  4. Move (cut), do not copy directory (if you copy, it will break symlinks).

    %LOCALAPPDATA%\lxss\root\rootfs to %LOCALAPPDATA%\lxss\rootfs

  5. Enter bash.

    bash ~

  6. Initialize Arch keyring.

    pacman-key --init

    pacman-key --populate archlinux

  7. Install base.

    pacman -Syyu base base-devel git vim wget reflector

  8. Enable multilib (if you want).

    linenumber=$(grep -nr "\\#\\[multilib\\]" /etc/pacman.conf | gawk '{print $1}' FS=":")

    sed -i "${linenumber}s:.*:[multilib]:" /etc/pacman.conf

    linenumber=$((linenumber+1))

    sed -i "${linenumber}s:.*:Include = /etc/pacman.d/mirrorlist:" /etc/pacman.conf

  9. Sync package databases.

    pacman -Syy

  10. Update mirror list (replace United States with preferred repo mirror country).

    reflector --country "United States" --age 12 --protocol https --sort rate --save /etc/pacman.d/mirrorlist

  11. Set root user password.

    passwd

  12. Create new user.

    useradd -m -G wheel username

  13. Set password on user.

    passwd username

  14. Enable wheel group.

    sed -i '/%wheel ALL=(ALL) ALL/c\%wheel ALL=(ALL) ALL' /etc/sudoers

  15. Edit Arch locale and regenerate.

    sed -i 's:#en_US.UTF-8 UTF-8:en_US.UTF-8 UTF-8:g' /etc/locale.gen

    locale-gen

    echo LANG=en_US.UTF-8 >> /etc/locale.conf

    echo LANGUAGE=en_US.UTF-8 >> /etc/locale.conf

    echo LC_ALL=en_US.UTF-8 >> /etc/locale.conf

  16. Exit bash.

    exit

  17. In Command Prompt, set default user for linux subsystem.

    lxrun /setdefaultuser username

  18. On next enter, you should login as your newly created user.

    bash ~

  19. Replace bash.ico in %LOCALAPPDATA%\lxss\ with preferred Arch icon. Be sure to name bash.ico.


Install Yay AUR Helper and Pacman Wrapper (https://github.com/Jguer/yay)

  1. You will first need to rebuild fakeroot following the instructions here

  2. Create a directory for the yay PKGBUILD files and enter it.

    mkdir ~/yay cd ~/yay

  3. Download yay PKGBUILD from AUR.

    wget --no-check-certificate https://aur.archlinux.org/cgit/aur.git/plain/PKGBUILD?h=yay --output-document=./PKGBUILD

  4. Run makepkg to build and install yay.

    makepkg -si


Install wsl-terminal and z shell

  1. Download wsl-terminal.

  2. Extract folder and launch wsl-terminal.

  3. Enter home directory.

    cd ~

  4. Right-click the title bar, select Options then select your theme from the Looks section.

  5. Install zsh.

    sudo pacman -S zsh

  6. Install oh-my-zsh.

    sh -c "$(wget https://raw.githubusercontent.com/robbyrussell/oh-my-zsh/master/tools/install.sh -O -)"

    Note: This will install oh-my-zsh and set zsh to be the logged in users default shell, but since WSL launches bash, you will need to add the line bash -c zsh to your ~/.bashrc file.

  7. Configure your zsh theme.

    vim ~/.zshrc

    ZSH_THEME="agnoster" (This is my preferred theme)

  8. Download and install fonts for Powerline. Download here.

  9. Right-click the title bar, select Options, then select your installed font from the Text section.


Launch X11 apps from the shell to Windows display

  1. Download and install Xming, and X11 display server for Windows.

  2. Install an xorg app for testing (We will use the Xorg Calculator).

    sudo pacman -S xorg-xcalc

  3. Export output to display.

    export DISPLAY=:0

    Note: If you add this line to your .bashrc or .zshrc you won't need to type it again on the next WSL launch.

  4. Launch xcalc to test.

    xcalc

  5. If Xming is working properly, Xorg Calculator should popup as a new window.

Building fakeroot-tcp on Arch WSL

Modified original instructions from user some_random_guy_5345 on Reddit. Full thread here.

Instructions

  1. Open up the PKGBUILD for fakeroot-tcp located on the AUR.

  2. Install the dependencies.

    glibc util-linux po4a

  3. Perform commands in PKGBUILD. This will install fakeroot-tcp in a different directory.

   wget http://ftp.debian.org/debian/pool/main/f/fakeroot/fakeroot_1.22.orig.tar.bz2
   tar xvf fakeroot_1.22.orig.tar.bz2
   cd fakeroot-1.22/
   ./bootstrap
   ./configure --prefix=/opt/fakeroot \
       --libdir=/opt/fakeroot/libs \
       --disable-static \
       --with-ipc=tcp
   make
   sudo make install
   /opt/fakeroot/bin/fakeroot
   exit
   exit

Note: If you receive any errors (like acl_get_fd, acl_get_file, etc) after running /opt/fakeroot/bin/fakeroot, ignore them. These are a bug in this build of fakeroot caused by mismatch in glibc.

  1. Backup base faked and fakeroot from /sbin and /bin and replace with newly built files
   sudo mv /bin/faked faked-bak
   sudo mv /bin/fakeroot fakeroot-bak
   sudo mv /sbin/faked faked-bak
   sudo mv /sbin/fakeroot fakeroot-bak
   sudo cp /opt/fakeroot/bin/faked /bin/
   sudo cp /opt/fakeroot/bin/faked /sbin/
   sudo cp /opt/fakeroot/bin/fakeroot /bin/
   sudo cp /opt/fakeroot/bin/fakeroot /sbin/
  1. You can now install PKGBUILD's using makepkg -si
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment