So this is my first time trying to setup Laravel Sail on Windows. I'm utilizing Docker and WSL 2. After some trial and error I manage to get it up and running. However, I've run into an issue I'm having difficult getting around. I wanted a newer PHP version as my composer installs kept erroring on low PHP version. I downloaded, installed and set up PHP 8.1 so it's recognized in Windows when I check PHP version. But when I check version in WSL it still shows an older version. See ...
PS C:\Users\Geoff\zbase-app> php -v
PHP 8.1.10 (cli) (built: Aug 30 2022 18:08:04) (NTS Visual C++ 2019 x64)
Copyright (c) The PHP Group
Zend Engine v4.1.10, Copyright (c) Zend Technologies
PS C:\Users\Geoff\zbase-app> wsl
gbuffett@DESKTOP-SVGAO49:/mnt/c/Users/Geoff/zbase-app$ php -v
PHP 7.4.3 (cli) (built: Aug 17 2022 13:29:56) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies
with Zend OPcache v7.4.3, Copyright (c), by Zend Technologies
How do I get WSL to recognize/utilize the 8.1 PHP version instead of the 7.4 version?
gbuffett@DESKTOP-SVGAO49:/mnt/c/Users/Geoff/zbase-app$ php --ini
Configuration File (php.ini) Path: /etc/php/7.4/cli
Loaded Configuration File: /etc/php/7.4/cli/php.ini
Any insight will be much appreciated as I haven't had much luck googling the issue.
So, fair warning, I have very limited experience with Laravel or Sail (and most of my PHP experience is in the distant past), but if you'll look at my profile, you'll see that I do have some experience with using various developer tools with WSL and Docker. With that in mind ...
I'm utilizing Docker and WSL 2
Honestly and respectfully, you don't seem to be!
When you install Sail, it creates a docker-compose.yaml
that tells Docker Compose to download all of the Docker images your Laravel application needs, including PHP. You don't need to install PHP separately; nor will installing PHP locally have any impact on your Sail application. So the php
commands you use in your question just aren't, unfortunately, "utilizing Docker and WSL 2."
And Sail already comes with PHP 8.1 ...
It's entirely possible to create Laravel (sans Sail) applications using a locally installed PHP, but that doesn't seem to be what you want to do.
So let's start from the "beginning" for Sail on WSL. Following the Laravel Getting Started On Windows doc, install a new app ...
cd ~ # or whatever root directory you want to use, but it should be in your WSL2 filesystem
curl -s https://laravel.build/example-app | bash
And start Sail (per the instructions that appear at the end of the installation) with ...
cd example-app && ./vendor/bin/sail up
The first time you do this, it's going to take a few minutes, as Sail/Docker is going to download all of the required images from the repositories, configure the database, etc. Subsequent launches will be significantly faster.
Once your Sail app is running, start a new Terminal window with WSL2, cd
to the example directory created above and ...
> ./vendor/bin/sail php --version
PHP 8.1.9 (cli) (built: Aug 15 2022 09:40:11) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.1.9, Copyright (c) Zend Technologies
with Zend OPcache v8.1.9, Copyright (c), by Zend Technologies
with Xdebug v3.1.5, Copyright (c) 2002-2022, by Derick Rethans
Note that for Sail, you must run any PHP commands through the sail
command itself, since that's going to call the php
version running in your Sail app.
Also useful doc:
sail
Hopefully, that answers your main question, but I'll also touch on your secondary ones:
I wanted a newer PHP version
While hopefully you no longer need a later version (since you see that Sail comes with 8.1 already), there's the implicit question of how to change your PHP version for Sail. That's covered in the Sail doc. Note that Sail currently supports 8.1, 8.0, or 7.4 only.
I downloaded, installed and set up PHP 8.1 so it's recognized in Windows
There are at least three separate "places" you could (rightly or wrongly) install PHP then:
When working with WSL, as a general rule you should only use the Linux version of PHP (and other tools). That can be installed either directly in your WSL distribution or through Docker. But you should not expect to be able to use the Windows version of PHP directly with Linux.
You might get it to hobble along by running it as php.exe
(assuming that it is on the PATH
). Windows executables must be executed using their full name (including extension) in WSL, since Linux doesn't traditionally have the concept of "automatically doing something based on extension".
But even then, it's a bad idea. The main problem is that the Windows version of PHP isn't going to understand:
C:\Users\LaravelNewbie
isn't a valid path in WSL, and the equivalent /mnt/Users/LaravelNewbie
isn't a valid path to a Windows application.When working in WSL, I always recommend using the Linux version of any development tool. WSL/Windows interop is great, but most tools (PHP included) aren't built to support it. There are only a few exceptions, like Visual Studio code and some of the Jetbrains applications.
As for upgrading the PHP version in your WSL distribution (which again, you shouldn't need to do for Sail), that may vary depending on the actual distribution. The default distribution in WSL is Ubuntu, and it only includes "long term support" releases for WSL. Those releases tend to stick with one particular version (plus security updates) of each tool for the entire Ubuntu release lifespan (5 years of updates for LTS). The reason for this is that changing the PHP version, or the version of any tool, (when other tools have been tested against that version) could create new issues.
If you want to override the version of a particular tool that is installed by the system, then consider installing a new copy for your user (instead of the system). There are various tools for helping with this; one being mentioned in the comments.
Other distributions, such as Arch or openSUSE Tumbleweed, are what as known as "rolling releases", where they update to new versions of tools as soon as they are available and (reasonably) tested. Typically the versions of tools available on these rolling releases will be newer than the ones in a "Stable" release.