Laravel Sail for Windows

WSL

Starting with Windows 10 version 2004 and above, to install WSL, you need to open the console with administrator rights and enter the command:

wsl --install

This command allows you to enable the required components, download the latest Linux kernel, set WSL 2 as the default component, and install the Linux distribution.

The above command only works if WSL is not installed at all.

If you have run the command wsl --install and see the WSL help text, try executing wsl --list --onlineto view a list of available distributions. Then do wsl --install -d <DistroName> to install the distribution.

Command wsl --list displays a list of available distributions. Check if the required distribution is not installed by default, you can do this with the command wsl --set-default <DistroName>.

For more information on other ways to install WSL, see here.

docker desktop

If the system does not have docker desktopwe put it.

Everything is simple for him: “further – further – further – ready.”

If you didn’t enable WSL 2 components during installation, don’t worry, you can do it later.

After starting Docker, go to its settings and on the tab General activate Use the WSL 2 based engine:

Then we go to the section Resources > WSL Integration and activate the distribution added earlier:

Laravel Sail

Laravel Sail can be used both in a new project and connected to an existing one. Laravel Sail works with PHP version 7.4 and up.

We will use a fresh project using installerwhich will install Laravel 9 with the Sail dependency:

laravel new blog

Since all commands must start with vendor/bin/sail, specify an alias for simplicity. To do this, in a file %USERPROFILE%\.bash_profile let’s add a line:

alias sail="vendor/bin/sail"

Restart the console, go to the folder with the created project and initialize the Sail configuration:

php artisan sail:install

Separate by commas, specify the services required for the application. For example, 0,3 install mysql and redis:

And we press enter.

This command will create a file docker-compose.yml in the root of the project and write the redis and mysql configs in the file .env.

If you need to run several projects at the same time, in the file .env you can set the key APP_PORT with port number.

For example, with the value APP_PORT=1234 application will be available at http://127.0.0.1:1234.

Now you can proceed to the launch and here we have two options:

sail up
sail up -d

First option, sail up will start the containers in “live” mode, that is, when the console is closed, the connection will also be closed, and sail up -d will run them in the background and the containers will run until the command is given sail down or the docker itself will not be unloaded.

You can’t just run Sail on Windows because it uses Linux components. That is why during the installation phase of WSL, a distribution kit was installed, for example, Ubuntu.

First you need to log into the WSL subsystem by running the command:

wsl

It is also worth noting that the alias sail we installed it on Windows and it does not apply to the subsystem, so we will set it to the subsystem as well.

To do this, run the command nano ~/.bashrc and add the following lines to the end of the file:

alias sail="[ -f sail ] && bash sail || bash vendor/bin/sail"
alias sart="[ -f sail ] && bash sail artisan || bash vendor/bin/sail artisan"

To apply the changes, exit the subsystem with the command exit and re-enter it:

Launch containers with the command sail up and wait for the compilation to complete:

Let’s go to the page http://127.0.0.1 and see our application.

The console will also show the logging of requests. Of course, if the launch was performed by the command sail up.

To call various artisan-commands in container you can use alias sart. For example:

sart queue:work

Everything. Good luck with development 👋

Similar Posts

Leave a Reply

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