paint-brush
Why You Don't Need Linux to Develop in PHPby@alexkochnev
1,215 reads
1,215 reads

Why You Don't Need Linux to Develop in PHP

by Alexander KochnevJune 2nd, 2023
Read on Terminal Reader
Read this story w/o Javascript

Too Long; Didn't Read

Windows Subsystem for Linux 2 (WSL2) is a compatibility layer provided by Microsoft. WSL2 allows you to run a full-fledged Linux distribution natively on your Windows machine. Developers can leverage powerful debugging features directly from their Windows IDEs. Laravel Sail is a lightweight command-line interface (CLI) for Laravel that simplifies the setup of local development environments.
featured image - Why You Don't Need Linux to Develop in PHP
Alexander Kochnev HackerNoon profile picture

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.


What is 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.


WSL2 advantages

  1. 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.


  2. 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.


  3. 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.


WSL2 installation

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.


PHP installation

  1. Update Ubuntu packages.
sudo apt-get update


  1. Install PHP 8.1.
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


  1. Install composer.
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.


Bonus part: 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:


  1. Install Laravel on your Ubuntu system.
composer global require laravel/installer


  1. Create a Laravel project or simply clone your existing one.
laravel new my-project
cd my-project


  1. Configure Laravel Sail for the project.
php artisan sail:install

It will create docker-compose.yml which will have NGINX, MySQL, Redis, and other really helpful tools for development.


  1. Start your project.
./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.