NOTE: If you have Windows 11 there is now an official way to do this in WSL 2, use it if possible - see MS post here (WINDOWS 11 ONLY)
This guide will enable systemd
to run as normal under WSL 2. This will enable services like microk8s
, docker
and many more to just work
during a WSL session. Note: this was tested on Windows 10 Build 2004, running Ubuntu 20.04 LTS in WSL 2.
-
To enable
systemd
under WSL we require a tool calledsystemd-genie
-
Copy the contents of
install-sg.sh
to a new file/tmp/install-sg.sh
:cd /tmp wget --content-disposition \ "https://gist.githubusercontent.com/djfdyuruiry/6720faa3f9fc59bfdf6284ee1f41f950/raw/952347f805045ba0e6ef7868b18f4a9a8dd2e47a/install-sg.sh"
-
Make it executable:
chmod +x /tmp/install-sg.sh
-
Run the new script:
/tmp/install-sg.sh && rm /tmp/install-sg.sh
-
Exit the WSL terminal and shutdown the WSL env:
wsl --shutdown
-
To open a new WSL terminal with
systemd
enabled, run:wsl genie -s
-
Prove that it works:
sudo systemctl status time-sync.target
I get the impression that you guys intend to keep your WSL systems for some length of time. The whole idea of WSL and containers is that they can be deleted and recreated quickly and easily without fuss. If you intend to keep a Linux system, install a VM. The WSL installation can be easily broken by Windows updates or any random software you install, like Genie. It would be wiser to install a VM and perform a snapshot of the VM before you install system-altering software like Genie. This way, you can always revert.
If you dig deep enough, you can find ways to copy/export your WSL VHD for backup and recovery purposes or to move it to a different location, but it is an Internal Development Platform (IDP). It's not intended to be kept for any length of time on the system after you develop some software.
If your system doesn't work with systemd and Genie, it would be faster and easier just to delete the installation and reinstall WSL Ubuntu from MSStore using the latest version and enable systemd and do not return to this Gist since it has been deprecated now with the latest versions of WSL.