WSL uses the System V init system by default. Here’s how you can replace that with systemd to manage services on WSL like a pro.
Windows Subsystem for Linux, or WSL, is a great way for developers who need to run Linux and Windows to do so without having to set up a virtual machine or dual boot.
One major new feature is the ability to run systemd on WSL, which is useful for web developers setting up test environments locally. Here’s how to enable and use it.
Why Install systemd on WSL?
WSL services by default run the older System V init service and services are started with the service command. In September 2022, Microsoft added the option to use systemd to manage services. Most major Linux distributions have already changed to systemd.
Despite the controversy, in real use, it’s much simpler to start and stop services with systemd. It’s also easier to follow along with other Linux tutorials as you don’t have to change around commands as much.
A local LAMP testing environment will be closer to a standard Linux server, so your app will be easier to debug.
How to Enable systemd on WSL
To enable systemd, you have to run the right version of WSL. systemd requires WSL version 0.67.6 or higher. You can check the version number by opening up PowerShell and typing:
If you have an older version installed and wish to use systemd, you can update WSL using:
Now you’ll have to set up systemd in any distro you have installed with WSL. The process is the same on any WSL distro, whether that’s Ubuntu, Debian, Fedora, or something else.
Simply edit the /etc/wsl.conf file in your system as root and add these lines:
If the wsl.conf file doesn’t exist, don’t worry and proceed by creating it yourself. These lines will work regardless.
Save the file, and now you’ll have to restart any running WSL distros. To do this, open up a PowerShell window and type this command:
Open up another terminal, and you’ll be able to manage services with systemd on WSL.
Starting and Stopping WSL Services With systemd
You can now use systemctl to start and stop services in WSL.
Suppose you were using Apache to run a local web server to test a web app you were developing.
You’d start Apache like this:
sudo systemctl start apache2.service
If you wanted it to start automatically, you’d enable it using:
sudo systemctl enable apache2.service
And disable it with:
sudo systemctl stop apache2.service
Now You Can Run systemd on WSL
With the latest updates to WSL, you can now enable systemd and start and stop services as you need them. This makes WSL a good environment for developing web applications, as you can test them as if you were using any other standard Linux machine.
It’s also extremely easy to install a LAMP (Linux, Apache, MySQL, PHP) server on Windows Subsystem for Linux.
Read the full article here