Developers using Windows machines have historically faced challenges when it comes to PHP development due to the platform's compatibility with certain tools and frameworks. But what if you’re a Windows guy? With the introduction of Windows Subsystem for Linux 2 (WSL2), PHP development on Windows has become more seamless and efficient. In this article, we will explore what WSL2 is, why it is easy to use for PHP development, and how to set up a PHP project with Laravel Sail on WSL2.
WSL2 is a compatibility layer provided by Microsoft that allows you to run a full-fledged Linux distribution natively on your Windows machine. Unlike its predecessor, WSL2 utilizes a lightweight virtualization technology, which provides better performance and compatibility with various Linux applications and tools. It allows developers to harness the power of Linux tools and environments while working within the familiar Windows ecosystem. In simple words, you just have Ubuntu installed and running on your Windows machine.
Seamless Integration: With WSL2, you can run Linux distributions such as Ubuntu, Debian, or CentOS directly on your Windows machine. This integration ensures that you have access to the Linux terminal and package manager, enabling you to work with PHP and its associated tools effortlessly.
Compatible Environment: WSL2 provides a compatible environment for PHP development. You can install PHP, Composer, and other necessary dependencies just as you would on a Linux system. This ensures consistency across development environments, making it easier to collaborate with other developers working on Linux or macOS.
Improved Debugging Capabilities: WSL2 supports popular PHP debugging tools, such as Xdebug, seamlessly. Developers can leverage powerful debugging features directly from their Windows IDEs while running PHP code within the Linux environment. This integration ensures a consistent debugging experience and streamlines the troubleshooting process.
You can now install everything you need to run WSL with a single command. Open PowerShell or Windows Command Prompt in administrator mode by right-clicking and selecting "Run as administrator", enter the wsl --install command, then restart your machine.
wsl --install
This command will enable the features necessary to run WSL and install the Ubuntu distribution of Linux. After installation, you can simply open CMD and type wsl
it will automatically start Ubuntu and you’ll be able to run Linux commands.
sudo apt-get update
sudo apt-get install -y php8.1-cli php8.1-common php8.1-mysql php8.1-zip php8.1-gd php8.1-mbstring php8.1-curl php8.1-xml php8.1-bcmath
curl -sS https://getcomposer.org/installer -o composer-setup.php
sudo php composer-setup.php --install-dir=/usr/local/bin --filename=composer
That’s it for the installation part. Now you can develop applications under your fresh Ubuntu. However, if you work with Laravel I’d like to show you how easily you can deploy multiple projects on your computer using Docker and Laravel Sail.
Laravel Sail is a lightweight command-line interface (CLI) for Laravel that simplifies the setup of local development environments. It leverages Docker to create a unified, portable, and consistent environment for Laravel projects. Sail streamlines the configuration process, allowing you to focus on writing code rather than dealing with complex setup procedures. The main advantage is you don’t need to worry about creating your own docker-compose files and working with images, Sail will do everything for you. Here is how you can use Sail for your project:
composer global require laravel/installer
laravel new my-project
cd my-project
php artisan sail:install
It will create docker-compose.yml which will have NGINX, MySQL, Redis, and other really helpful tools for development.
./vendor/bin/sail up
This command will spin up Docker containers and you’re application will be live!
You can change the ports for your app in the .env file; however, it should be available with localhost:80 by default..
Thanks to WSL2 and Laravel Sail, PHP development on Windows machines has become significantly easier and more accessible. WSL2 provides seamless integration of Linux distributions within the Windows environment, while Laravel Sail simplifies the setup and configuration of local development environments for Laravel projects. By following the steps outlined in this article, you can leverage WSL2 and Laravel Sail to enjoy a comfortable PHP development experience on your Windows machine without the need for switching to Ubuntu or other Linux distributions.