Skip to content

Instantly share code, notes, and snippets.

@ld100
Last active December 21, 2024 07:35
Show Gist options
  • Save ld100/3376435a4bb62ca0906b0cff9de4f94b to your computer and use it in GitHub Desktop.
Save ld100/3376435a4bb62ca0906b0cff9de4f94b to your computer and use it in GitHub Desktop.
Steps for setting up Arch Linux on WSL2

Migrating from Ubuntu on WSL to ArchLinux on WSL2

Obsolete notice

This document was created back in 2020 and might not be actual nowadays. It is not supported anymore, so use thise information at your own risk.

Upgrading to WSL 2

  • Download WSL2 Kernel
  • run wsl --set-default-version 2 in windows command line, so that all future WSL machine will use WSL2.

Installing Arch Linux

Basic installation

  • Download ArchWSL installer zip, pick a folder (for example, c:\archlinux) for Arch Linux and run installer .exe in that folder.

Initialize package manager

  • Add custom pacman repository with additional packages: nano /etc/pacman.conf Then add following to the bottom:
[archlinuxfr]
SigLevel = Never
Server = http://repo.archlinux.fr/$arch
  • Refresh Pacman GPG keys:
pacman-key --init
pacman-key --populate
pacman-key --refresh-keys
pacman -Sy archlinux-keyring
  • Run pacman -Syyu to update all packages to the latest versions

Install zsh, optional

  • Install zsh: pacman -S zsh

Create a user

  • Add a sudo group: groupadd sudo
  • Enable sudoers: nano /etc/sudoers and uncomment lines %wheel ALL=(ALL) NOPASSWD: ALL and %sudo ALL=(ALL) ALL
  • Add new admin user: useradd -m -G wheel,sudo -s /bin/zsh <username>, use -s /bin/bash if you want bash instead of zsh.
  • Set password for the new user: passwd <username>
  • Run Windows command shell, go to the directory with Arch Linux, run Arch.exe config --default-user <username>. Now you have basic ArchLinux with user.

Install AUR helper

  • Just follow How to Install Yay AUR Helper in Arch Linux and Manjaro guide:
  • sudo pacman -S git openssh
  • sudo pacman -S base-devel - when asked question on fakeroot and fakeroot-tcp choose to leave fakeroot-tcp and not install fakeroot
  • git clone https://aur.archlinux.org/yay-git.git
  • cd yay-git
  • makepkg -si
  • Run yay -Syu to update all AUR packages and reinstall fakeroot-tcp (will install the latest version)
  • Remove the leftovers: rm -rf ~/yay-git

Goodies

  • Install useful console tools: sudo pacman -S mc wget htop pv ccze

Preserving config files from previous WSL installation

Copy .ssh, .kube, .aws from original installation home directory to the Arch WSL2 one.

Fix file permissions for SSH keys, run from the home directory:

chmod 0644 .ssh/id_rsa.pub
chmod 0600 .ssh/id_rsa

Installing additional software

Install Kubernetes

sudo pacman -S kubectl kubectx

Install Docker

Uninstall Windows docker to prevent potential naming conflicts.

sudo pacman -S docker docker-compose

Use wsl2-docker-start scripts to run docker exposed to Windows:

git clone git@github.com:imjonos/wsl2-docker-start.git
cd wsl2-docker-start
chmod +x docker-start prepare-hosts
sudo cp docker-start prepare-hosts /usr/local/bin

In Windows you should make sure C:\Windows\System32\drivers\etc\hosts file has write access for your user. Always run docker in a dedicated terminal tab using docker-start

ld100 dotfiles & zsh config

git clone https://github.com/ld100/dotfiles.git ~/.dotfiles
zsh ~/.dotfiles/install.sh

Install programming languages

sudo pacman -S ruby nodejs python go crystal php jre-openjdk-headless

Installing protocol buffers

Main binaries/libraries: sudo pacman -S protobuf protobuf grpc grpc-cli

gRPC for Python and PHP: sudo pacman -S python-grpcio php-grpc

gRPC & Protobuf for Go: yay -S protobuf-go protoc-gen-go-grpc

gRPC & Protobuf for Ruby: gem install google-protobuf grpc grpc-tools

Setting ArchLinux as default WSL

Just run wsl --set-default Arch in windows command shell.

Accessing WSL from Windows

All WSL machines are available in Windows Explorer on \\wsl$\ URL, Arch linux is: \\wsl$\Arch. For additional convenience you could map \\wsl$\Arch to a 1-Leter Windows disk share, for example having Z:\ pointing to \\wsl$\Arch

@paolosalvatori
Copy link

paolosalvatori commented Aug 11, 2021

@paolosalvatori WAT? ;) Pacman comes preinstalled as a part of ArchLinux.

Not really: on WSL2 Ubuntu 18.04 pacman was not installed by default. I had to install it manually.

@ld100
Copy link
Author

ld100 commented Aug 11, 2021

@paolosalvatori obviously, because Ubuntu is not ArchLinux.

@paolosalvatori
Copy link

@paolosalvatori obviously, because Ubuntu is not ArchLinux.

Exactly, but WSL2 by default installs Ubuntu and not ArchLinux, so you start with Ubuntu, not ArchLinux

@mpldr
Copy link

mpldr commented Aug 11, 2021

Exactly, but WSL2 by default installs Ubuntu and not ArchLinux, so you start with Ubuntu, not ArchLinux

And you can't turn Ubuntu into arch by simply installing pacman. There is a way that requires the use of dark magic, but it's not:

  1. Install Ubuntu
  2. Install pacman
  3. You have arch now

If you want to use arch you must have pacman from the start.

You can also install dpkg on Arch, but that doesn't turn it into a Debian derivate.

@ld100
Copy link
Author

ld100 commented Aug 11, 2021

@paolosalvatori WSL2 does not install any distributions by default, you decide which ones to install.

@ld100
Copy link
Author

ld100 commented Aug 11, 2021

@groboclown nice one! Looks like a clever way to set up Arch. The only downside is that you have to install Windows Docker to do that.

@viniavila
Copy link

If you have Docker installed, you can run these steps. This is a bit easier than a long, manual method, uses an "official" distribution as a starting point, and allows you to see everything that's done to the distribution.

  1. Get the Docker image of Arch Linux, which is an "officially" maintained image:
    docker pull archlinux
  2. Start a container that will be used as the base volume for the WSL 2 distribution:
    docker run -it --name archlinux-wsl archlinux
  3. From that running, interactive container, set it up with necessary user data and updates; this sets your user name is myself; adjust as necessary; this assumes you know how to use vi, so adjust the editor to suit your needs:
    pacman -Syu
    pacman -Sy sudo vi
    export EDITOR=/usr/bin/vi
    visudo
    # in the editor, uncomment the line "%wheel ALL=(ALL) NOPASSWD: ALL"
    useradd -G wheel,users -m myself
    passwd myself
    # enter the password for the container user
    pwconv
    grpconv
    # At one point, it was necessary to also run "chmod 0744 /etc/shadow /etc/gshadow"
    exit
  4. Export the docker container's state to a tar file:
    docker export --output archlinux-image-files.tar archlinux-wsl
  5. Import the exported container files into a new WSL2 distribution:
    wsl --import archlinux (a directory on your computer that will store the distribution files) .\archlinux-image-files.tar
  6. You can now run wsl -d archlinux to get running!

@groboclown your solution is great, but how to set a default user, I don't want root to be the default user and it bothers me to run su all the time

@groboclown
Copy link

groboclown commented Oct 13, 2021

If you have Docker installed, you can run these steps. This is a bit easier than a long, manual method, uses an "official" distribution as a starting point, and allows you to see everything that's done to the distribution.

  1. Get the Docker image of Arch Linux, which is an "officially" maintained image:
    docker pull archlinux
  2. Start a container that will be used as the base volume for the WSL 2 distribution:
    docker run -it --name archlinux-wsl archlinux
  3. From that running, interactive container, set it up with necessary user data and updates; this sets your user name is myself; adjust as necessary; this assumes you know how to use vi, so adjust the editor to suit your needs:
    pacman -Syu
    pacman -Sy sudo vi
    export EDITOR=/usr/bin/vi
    visudo
    # in the editor, uncomment the line "%wheel ALL=(ALL) NOPASSWD: ALL"
    useradd -G wheel,users -m myself
    passwd myself
    # enter the password for the container user
    pwconv
    grpconv
    # At one point, it was necessary to also run "chmod 0744 /etc/shadow /etc/gshadow"
    exit
  4. Export the docker container's state to a tar file:
    docker export --output archlinux-image-files.tar archlinux-wsl
  5. Import the exported container files into a new WSL2 distribution:
    wsl --import archlinux (a directory on your computer that will store the distribution files) .\archlinux-image-files.tar
  6. You can now run wsl -d archlinux to get running!

@groboclown your solution is great, but how to set a default user, I don't want root to be the default user and it bothers me to run su all the time

You can change the default user through RegEdit, in the path HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Lxss\(id for your distribution) with the key DefaultUid - the data value needs to be a number type, with a value corresponding to your user ID. For myself, I have a UID of 1000, so the value is 1000 decimal, or 3e8 hexidecimal.

@perryBunn
Copy link

Any tips for getting graphical applications working?

@Chuanhuan
Copy link

If you have Docker installed, you can run these steps. This is a bit easier than a long, manual method, uses an "official" distribution as a starting point, and allows you to see everything that's done to the distribution.

  1. Get the Docker image of Arch Linux, which is an "officially" maintained image:
    docker pull archlinux
  2. Start a container that will be used as the base volume for the WSL 2 distribution:
    docker run -it --name archlinux-wsl archlinux
  3. From that running, interactive container, set it up with necessary user data and updates; this sets your user name is myself; adjust as necessary; this assumes you know how to use vi, so adjust the editor to suit your needs:
    pacman -Syu
    pacman -Sy sudo vi
    export EDITOR=/usr/bin/vi
    visudo
    # in the editor, uncomment the line "%wheel ALL=(ALL) NOPASSWD: ALL"
    useradd -G wheel,users -m myself
    passwd myself
    # enter the password for the container user
    pwconv
    grpconv
    # At one point, it was necessary to also run "chmod 0744 /etc/shadow /etc/gshadow"
    exit
  4. Export the docker container's state to a tar file:
    docker export --output archlinux-image-files.tar archlinux-wsl
  5. Import the exported container files into a new WSL2 distribution:
    wsl --import archlinux (a directory on your computer that will store the distribution files) .\archlinux-image-files.tar
  6. You can now run wsl -d archlinux to get running!

@groboclown your solution is great, but how to set a default user, I don't want root to be the default user and it bothers me to run su all the time

You can change the default user through RegEdit, in the path HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Lxss\(id for your distribution) with the key DefaultUid - the data value needs to be a number type, with a value corresponding to your user ID. For myself, I have a UID of 1000, so the value is 1000 decimal, or 3e8 hexidecimal.

Thx!!! This is really helpful. I have a question that how to updating the docker archlinux base. Do I need to restart all steps?

@XxnittanixX
Copy link

gwsl on windws store is nice :)

Copy link

ghost commented Jan 3, 2022

What is the correct way to uninstall this?

@stoyanovgeorge
Copy link

I have managed to install the repo, but for example the ping doesn't work. More so, when you check /.ssh/ directory, there are some existing public/private keys and a config file with an already configured server. I can only assume there are other files, which are customized by the creator.

@akc3n
Copy link

akc3n commented Feb 22, 2022

Hi @ld100 quick question, what exactly is this custom pacman repository used for in your instructions? I did have this added to another setup I did on different PC. But I do recall it was SO SLOW! So I was just curious about it.

I also have another alternative setup ( on a different laptop as well) as instructed in this comment

However, I am adding this ArchWSL again on a new reinstalled Windows 10 PC right now. And wanted to ask about that custom pacman repo you mentioned. Thank you.

@ld100
Copy link
Author

ld100 commented Feb 22, 2022

@akc3n I don't remember unfortunately. It was couple years ago and I'm not using Arch Linux for a long time since then.

@akc3n
Copy link

akc3n commented Feb 25, 2022

@ld100 okay, no worries, thanks for your reply.

@oniony
Copy link

oniony commented May 12, 2022

@groboclown your solution is great, but how to set a default user, I don't want root to be the default user and it bothers me to run su all the time

Within the WSL distro, edit or create /etc/wsl.conf and add the following:

[user]
default=someuser

Before you do that, ensure you set a password for root or your regular account has sudo access. (If you do find yourself without root capabilities, you can run wsl --user root to get back in as root.)

@NormTurtle
Copy link

image
cant add user 😭

@h-lunah
Copy link

h-lunah commented Dec 5, 2022

Just use https://github.com/sorah/subsystemctl

Update December 5th 2022: Since some time ago, WSL2 now supports the usage of systemd natively, no need to install additional software to enable it.

@nnbveh
Copy link

nnbveh commented Sep 13, 2023

Hello. Could you please tell me how to UNINSTALL arch from WSL? thanks!

@ItamarOliveira
Copy link

in powershell command prompt you can use
wsl --unregister arch

or go to Settings, applications, and uninstall there.

@caamittiwari
Copy link

we unable to run postgres on archlinux on wsl2 i am get error psql: error: connection to server on socket "/run/postgresql/.s.PGSQL.5432" failed: No such file or directory
Is the server running locally and accepting connections on that socket?
please help me where iam going wrong

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment