Skip to content

Instantly share code, notes, and snippets.

@mwoodpatrick
Forked from djfdyuruiry/README.md
Created December 28, 2020 16:14
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mwoodpatrick/d564dfd7e9790091874375fa248d03a3 to your computer and use it in GitHub Desktop.
Save mwoodpatrick/d564dfd7e9790091874375fa248d03a3 to your computer and use it in GitHub Desktop.
WSL 2 - Enabling systemd

Enable systemd in WSL 2

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 called systemd-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/46085955818d83b483fd62c65383a7f8dda84f5f/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
#! /usr/bin/env bash
set -e
GENIE_VERSION="1.23" # update if you need
GENIE_FILE="systemd-genie_${GENIE_VERSION}_amd64"
GENIE_FILE_PATH="/tmp/${GENIE_FILE}.deb"
GENIE_DIR_PATH="/tmp/${GENIE_FILE}"
function installDebPackage() {
# install repackaged systemd-genie
sudo dpkg -i "${GENIE_FILE_PATH}"
rm -rf "${GENIE_FILE_PATH}"
}
function fixDebDependencies() {
# repackage systemd-genie to require dotnet 3.1 instead of 3.0
rm -rf "${GENIE_DIR_PATH}"
mkdir -p "${GENIE_DIR_PATH}"
dpkg-deb -x "${GENIE_FILE_PATH}" "${GENIE_DIR_PATH}"
pushd "${GENIE_DIR_PATH}"
dpkg-deb -e "${GENIE_FILE_PATH}"
popd
sed -i 's/dotnet-runtime-3[.]0/dotnet-runtime-3.1/g' "${GENIE_DIR_PATH}/DEBIAN/control"
rm -f "${GENIE_FILE_PATH}"
pushd /tmp
dpkg-deb -b "${GENIE_DIR_PATH}"
popd
rm -rf "${GENIE_DIR_PATH}"
}
function downloadDebPackage() {
rm -f "${GENIE_FILE_PATH}"
pushd /tmp
wget --content-disposition \
"https://packagecloud.io/arkane-systems/wsl-translinux/packages/debian/bookworm/${GENIE_FILE}.deb/download.deb"
popd
}
function installDependencies() {
# install systemd-genie dependencies
sudo apt-get update
wget --content-disposition \
"https://packages.microsoft.com/config/ubuntu/20.04/packages-microsoft-prod.deb"
sudo dpkg -i packages-microsoft-prod.deb
rm packages-microsoft-prod.deb
sudo apt-get install apt-transport-https
sudo apt-get update
sudo apt-get install -y \
daemonize \
dotnet-runtime-3.1
sudo rm -f /usr/sbin/daemonize
sudo ln -s /usr/bin/daemonize /usr/sbin/daemonize
}
function main() {
installDependencies
downloadDebPackage
fixDebDependencies
installDebPackage
}
main
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment