How to Run Laravel in Windows 10 Using WSL 2 and Ubuntu 20.04
As a freelancer, sometimes I took my free time going vacation with my family. Off course, that's before 2020 when corona virus attacked. When on vacation, sometimes I also need to do a quick checking for my clients. An email like:
Last night, we've successfully migrated our server to a new server and also move our application and the database too. We're able to access it on a web browser, but when we're trying to logging in, it just stays at login page. Please let me know if you can check it out.
The issue is, I only bring Windows laptop:
- It has limited disk space to have a proper dual boot system.
- I'm too reluctant to have many home / working directories as I already have two desktops, each with multiple OS installed.
So for this laptop, I decided to run WSL (Windows Subsystem for Linux) to handle my situation. This article will cover about its installation process and how to run it. As a bonus, I'll also share how I installed PHP so we can install Laravel later on.
WSL 2 Requirements
You should fulfil these requirements prior to installation:
- For x64 systems: Version 1903 or higher, with Build 18362 or higher.
- For ARM64 systems: Version 2004 or higher, with Build 19041 or higher.
- Builds lower than 18362 do not support WSL 2. Use the Windows Update Assistant to update your version of Windows.
To check your version and build number, select
Windows logo key +
winver, select OK. (Or enter the
ver command in Windows Command Prompt). Update to the latest Windows version in the Settings menu.
WSL 2 Installation
You must first enable the "Windows Subsystem for Linux" optional feature before installing any Linux distributions on Windows. Open PowerShell as Administrator and run:
dism.exe /online /enable-feature /featurename:Microsoft-Windows-Subsystem-Linux /all /norestart
We've successfully installed WSL 1. To continue with WSL 2 installation, you must enable the Virtual Machine Platform optional feature. Open PowerShell as Administrator and run:
dism.exe /online /enable-feature /featurename:VirtualMachinePlatform /all /norestart
Restart your machine to complete the WSL install and update to WSL 2.
Next, we'll update WSL 1 to WSL 2. Download this package from Microsoft's website: WSL2 Linux kernel update package for x64 machines. Once the package successfully downloaded, run it with by double-clicking it. You will be prompted for elevated permissions, select "yes" to approve this installation.
When the installation is complete, move on to the next step: setting WSL 2 as your default version when installing new Linux distributions. Open PowerShell and run this command to set WSL 2 as the default version when installing a new Linux distribution:
wsl --set-default-version 2
The update from WSL 1 to WSL 2 may take several minutes to complete depending on the size of your targeted distribution.
If you are running
an older (legacy) installation of WSL 1 from Windows 10 Anniversary
Update or Creators Update, you may encounter an update error. Follow
these instructions to uninstall and remove any legacy distributions.
wsl --set-default-version results as an invalid command, enter
wsl --help. If the
--set-default-version is not listed, it means that your OS doesn't support it and you need to update to version 1903, Build 18362 or higher.
If you see this message after running the command:
WSL 2 requires an update to its kernel component. For information please visit https://aka.ms/wsl2kernel.
You still need to install the MSI Linux kernel update package.
Linux Distribution Installation
Open the Microsoft Store and select your favourite Linux distribution. Personally, I use Ubuntu 20.04 Focal Fossa for my WSL. The install itself should be pretty straightforward: choose a distribution and then click on "Get" button.
The first time you launch a newly installed Linux distribution, a console window will open and you'll be asked to wait for a minute or two for files to de-compress and be stored on your PC. All future launches should take less than a second.
You will then need to create a user account and password for your new Linux distribution.
CONGRATULATIONS! You've successfully installed and set up a Linux distribution that is completely integrated with your Windows operating system!
PHP 7.4 Installation
It's mandatory to install PHP before we're able to run Laravel. Let's install PHP in our newly installed WSL 2 system.
First, let's run our distro. You can search it from your start menu and then click it. A window will open, which should be similar with terminal when we're on a real Linux system or on SSH session.
To install PHP, enter this command:
$ sudo apt install git php-fpm php-zip php-mbstring php-xml php-gd
Currently, Laravel and other PHP libraries are using
composer for dependency management. We should install it too. Let's back to the terminal and enter this command to install
$ sudo apt install composer
When it's done, we can continue with Laravel installation:
$ composer -vvv create-project --prefer-dist laravel/laravel laravel
Composer will download all required files and put the files into laravel directory. Once it finishes, we can run Laravel using this command:
$ php -S 0.0.0.0:8080 -t public
As you might have noticed, we don't run php artisan serve because it just won't work. Running php artisan serve will listen only to 127.0.0.1
I hope that you now know how to install WSL 2 on Windows 10 running Ubuntu 20.04 Focal Fossa. If you run into any issues or have any feedback feel free to drop a comment below.