How to Run Laravel in Windows 10 Using WSL 2 and Ubuntu 20.04

Share:
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 + R, type 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:

Right-click on the Start Windows button in the bottom left of your screen and choose "Windows Powershell (Admin)".

Start Windows Context Menu

After clicking on the menu, a confirmation popup will shown, asking "Do you want to allow this app to make changes to your device". You can safely press "Yes" button here. Then the Windows Powershell window will appear. In this window, enter this command:

dism.exe /online /enable-feature /featurename:Microsoft-Windows-Subsystem-Linux /all /norestart
WSL 1 Installation

Now we've successfully installed WSL 1. To continue with WSL 2 installation, you must enable the Virtual Machine Platform optional feature. Again, open PowerShell as Administrator and run:

dism.exe /online /enable-feature /featurename:VirtualMachinePlatform /all /norestart
Enable Windows Virtual Machine

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" or "OK" to approve this installation.

WSL 2 Installation

When this windows appears, just click "Next" and wait until the installation process finished.

WSL 2 Installation

Press "Finish" to close the installer window. 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

Use WSL 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.

If 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 search for your favorite Linux distribution. Personally, I use Ubuntu 20.04 LTS (Focal Fossa) for my WSL. The install itself should be pretty straightforward: choose a distribution and then click on "Install" button.

Microsoft Store

Install Ubuntu 20.02 LTS

Ubuntu 20.04 LTS Installation Process


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.

First Launch

You will then need to create a user account and password for your new Linux distribution.

User Creation

Password Input

Welcome to Ubuntu 20.04


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.

Before we proceed with PHP installation, we need to update our package repository first.

$ sudo apt update
$ sudo apt -y upgrade

To install PHP, enter this command:

$ sudo apt install -y 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 composer:

$ sudo apt install -y 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:

$ cd laravel
$ 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

To open our Laravel application, point your web browser to this address: http://localhost:8080. You should see something like below:

Laravel Application

Final Words

I hope that you now know how to install WSL 2 on Windows 10 running Ubuntu 20.04 LTS (Focal Fossa). If you run into any issues or have any feedback feel free to drop a comment below.

Tags PHP Laravel
Share:

5 comments

  1. Hi, your article helps a lot and I haven't faced any issues with the installation. Now, I'd like to ask if you have tried changing the default preset/ui of your Laravel project (which is by default Vue) into ReactJS? Or should I just follow the create react-app procedure and then integrate it with my Laravel resource file for the deployment?

    • Hello John,

      Most of my works are backend. When I need to work with frontend developer, usually we have separate repository. This way we're not dependent each other and development could be quicker.

      Your mileage my vary, but if you'd like to experiment, you could use Laravel Inertia.

  2. Hi, excuse me for my english, i'm french so...

    I just wanted to speak about my experience with laravel and WSSL !

    I always found the usage of laravel super hard, always a problem with php functions, or error cause mariadb can't be used if not rooted etc...

    So i can't say anything about LAMP manually with laravel, but i can at least say that was the fastest installation i ever made, and the fastest success, so thanks a lot man !

    But what i wanted to speak of is about your article, i made exactly what you wrote, by copy/paste, on a WSL 2 Windows 11 with Debian Bullseye (v.11) and i can use without problem artisan by making "./artisan serve". So may be you can change your starting with laravel section (except if there is different output, i just made it on a fresh installation of laravel, so that's not a software issue).

    Thanks again for this ! You made me hate less Laravel on linux ! Now i have to make it works with Lamp w/out using Gui installer, only by installing Apache2 Mariadb and phpmyadmin. I have to find someone like you taht will make me hate less mariaDB on linux lol

    PS : I tried in case of, php artisan serve (than doing directly ./artisan serve) and works fine too.

  3. For me neither 127.0.0.1:8080 nor 0.0.0.0:8080 works. Laravel running inside Ubuntu machine, but cant access from Windows 10 browser. Browser says ERR_CONNECTION_REFUSED

    Other than that, how does file sharing between the WSL machine and the Windows host work? How to transfer files back and forth? Given i need to transfer existing Laravel project files into the WSL machine.

    • Well, that's really strange. I really wish I could help you figured this out. I'll try to find a fresh Windows 10 installation and then proceed with WSL 2 installation.

      As for Laravel development usage, I usually use Visual Studio Code plugin called "Remote - WSL". This way I don't have the need to access the files on WSL machine directly.

      I also uses git to store all of my projects, so anytime I need to update a project I just need to import it into WSL machine.

  4. WSL2 Ubuntu 20 CLI:

    :~/projects/laravel$ php -S 0.0.0.0:8080 -t public
    [Wed Feb 10 15:14:02 2021] PHP 7.4.3 Development Server (http://0.0.0.0:8080) started

    My Windows browser:

    Hmmm… can't reach this pageIt looks like the webpage at http://0.0.0.0:8080/ might be having issues, or it may have moved permanently to a new web address.
    ERR_ADDRESS_INVALID

    Why does it work for you but not me?

    • Hello Virla,

      I just updated the article to help your confusion. You shall use 127.0.0.1:8080 in your web browser to open your Laravel application.

  5. Ran all of the commands with no errors... then went to 0.0.0.0:8080 in my Windows Edge browser:

    Hmmm… can't reach this pageIt looks like the webpage at http://0.0.0.0:8080/ might be having issues, or it may have moved permanently to a new web address.
    ERR_ADDRESS_INVALID

    Same thing in Chrome. I'm using WSL2 Ubuntu 20.04.

    Why doesn't it work?

Leave a reply

Your email address will not be published. Required fields are marked *