Laravel Sail Guide

How to install and use Laravel Sail, to develop Laravel applications with Docker, on Windows, Mac and Linux. Complete Guide to Sail.

Laravel Sail is a tool that gives us a new alternative to create your Laravel application development environment. This tool is based on Dockerwhich allows us to have all the necessary programs to run Laravel applications without having to install these programs directly on the computer.

In this article we will cover the following points:

Why use Laravel Sail

The reasons are diverse, but I think the most important would be the possibility of coexistence of several Laravel projects on the same machine without complications.

Throughout your life as a developer you can maintain various projects, new projects with current PHP and Laravel versions, but also old projects that have less modern requirements. For example you could have a project with Laravel 6, 7 or 8 on PHP 7.4 but another Laravel 9 project on PHP 8.

Getting PHP versions to coexist on your machine can be complex. If you update PHP on your computer it will possibly affect older projects, but if you don’t you may not be able to install Laravel in newer versions. It is a dilemma that is not easily resolved using systems like Valet, which install PHP directly on your machine.

However, in a Laravel Sail project it is possible to have a fully customized version of PHP, as well as versions of MySQL or MariaDB, PostgreSQL or any other software you generally depend on.

In addition, developing in containers can also help keep your computer cleaner. Thanks to Docker, a software for creating and managing containers that has gained a lot of traction in recent years, we have managed to keep the computer we use to develop cleaner, since we do not need to install on the host machine the entire stack of necessary programs to run the sites. Besides, the virtualization with Docker is really light and it consumes few resources on the computer, compared to more traditional alternatives like Virtualbox.

In any case, surely all readers will have an idea of ​​the advantages of Docker, so we will go directly to the point.

What is Laravel Sail?

Sail is a command line interface for working with Docker-based development environments to run Laravel applications.. It offers a complete abstraction to the Docker configuration processes that we would have to do manually if we want to use this tool as a local development environment. It offers ready-made Docker images for Laravel applications, with web server, PHP, MySQL or MariaDB support, Redis and other programs within the typical Laravel stack.

You may using Laravel Sail without having a clue how Docker workswhich is great for anyone who wants to develop without worrying about the complexities of creating their own dockerized environment.

See also  Pseudo-element in CSS (pseudo elements)

In the end, underneath, Laravel Sail uses the same tools that you would use if you were working directly with Docker, like its docker-compose.yml, etc. As an extra, Sail offers us a whole series of commands to interact with the virtual machine where the applications are running, like a kind of tunnel through which we can interact with the development servers, without having to get into them.

Sail is just one of the many alternatives you have to work with Laravel to develop projects locally. We have seen other very interesting alternatives in , such as or .

Where you can use Laravel Sail

One of the advantages of Laravel Sai is that it is perfectly compatible with all typical operating systems for development. You can use it on Linux, MacOS and Windows.

There is only one detail for Windows users and that is, in order to work with Sail we need to have WSL2 (Windows Subsystem Linux 2). WSL2 allows you to have a Linux on your Windows, under the same file system and Sail takes advantage of this possibility to run on a Linux system, instead of a Windows system. Setting up WSL2 is not very complex, but it does represent an extra step for Windows users.

How to configure WSL2 on Windows

If you are a Linux and Mac user you can skip this section. Otherwise, if you want to use Sail on Windows, you have to start by installing WSL2.

Microsoft has published one, which is perfectly explained step by step and in perfect Spanish, so we recommend you follow it.

It won’t take long to have WSL2 on your system and enjoy the experience of having access to a whole Linux on your Windows, operating on the same file system. You will be able to open console sessions and work with your computer with Linux commands in the distribution that you like best.

Docker configuration on your computer

You will need to install Docker on your computer. If you are working with Windows or Mac we recommend installing , since it offers you a graphical interface to work with Docker that makes things a bit easier.

If you don’t have Docker, just installing Docker Desktop will install everything you need to work.

Important: If you are in windows when installing Docker Desktop you will find a box to check, which allows add all components for WSL2. Make sure you have it checked during the installation process.

You can test if Docker is working on your system by running the following command:

docker -v

If you are in Windows, you have to launch this command from WSL2, with a terminal session on the distro that you have installed in WSL2.

See also  Create an Animated Gif with Photoshop CC

If you had Docker Desktop on Windows and for whatever reason you don’t get a response to the previous command from Linux with WSL2, you have an extra configuration to do. Within Docker Desktop, in the configuration gear, we enter “Resources / WSL Integration” and we mark that the integration is enabled and the distros where we want to integrate it.

Install Sail

Now that we have the components we need to get started on our computer, basically Docker (and WLS2 in the case of Windows), we can start using Sail in a Laravel project. At this point we can find ourselves in two scenarios:

  • We want to create a new Laravel project where we will use Sail as development environment
  • We want to use Sail in an existing Laravel project

Both alternatives require different steps that we will discuss in the following points.

Start a Laravel project with Sail

If we are starting a Laravel project from scratch and we want to use Sail as development platform, instead of other alternatives like Homestead or Valet, we can launch a console command, which will provide us with the installation of a new project, with all the files, dependencies installed and Sail configured.

To do this, we must make sure that we have Docker installed on the machine, as we have explained in the previous steps. Once we have Docker in the terminal, we launch the following command:

curl -s “https://laravel.build/new-laravel-application” | bash

In the previous command “new-application-laravel” you will have to replace it with the name of the project you want to generate.

Once that command is put to work, we will have a folder with the same name as the project, where all the Laravel files will be placed. To make the project start up we will launch the following commands:

First we will move to the project folder, which we have indicated when creating it in the previous command:

cd new-application-laravel

Next, we run the command that starts Sail and starts the project.

./vendor/bin/sail up

The first time you start the project you will see the Docker image used for the containers being downloaded. This process can be a bit long, depending on your machine and of course the Internet connection. However, you don’t need to worry about it taking a long time, as this download will only happen once.

Once the project is running we can access the localhost URL to see it running: http://localhost

Start Sail choosing the services to install

This second point would be exactly in the scenario of the previous point: we want to create a new Laravel project in which we are going to work with Sail. The difference in this case is that we want choose installed Sail services.

Why might we need to choose which Sail services to incorporate into the project? Well, maybe you don’t need it, but in my case I usually work preferably with MariaDB instead of MySQL, that’s why I want the installer to take me as the MariaDB database manager. However, for other projects you may not need to install Redis for example, or you may not need Selenium.

See also  Concatenate strings in PHP

This way you can select which things from Sail you want to be installed:

curl -s “https://laravel.build/backend2-app?with=mariadb,redis,mailhog” | bash

As you can see, with the “with” parameter we can indicate that we are going to use only the mariadb, redis and mailhog services. You can choose your own alternatives, if for example you need to work with the PostgreSQL management system.

Install Laravel Sail an existing project

Laravel Sail is a development environment that comes installed in any newly created Laravel application. However, if we already have an old Laravel project and want to use Sail, we need to install it manually.

The process is very simple, thanks to Composer, using the following command:

composer require laravel/sail –dev

Now we have Sail in our system and we can use the CLI that it offers us. The first command that we will execute is the one that allows us to bring the “docker-compose.yml” file to the root of the project:

php artisan sail:install

Once the command is launched, we will be asked to select which Sail services we want to install in the project. We will have to introduce the corresponding options in the terminal.

We can repeat this process with as many Sail services as we want to install. These actions will be coded into a file called docker-compose.yml, which Docker uses to indicate which containers should be built into the system with each project.

Commands to use Laravel Sail

From now on, to work with your Laravel projects, many of the operations that you did directly through the terminal will need to be done through Laravel Sail commands. In the following points we are going to explain what these commands are, from starting the containers of your development environment to installing dependencies.

Start Sail

Now that we have the docker-compose.yml file we can build the Laravel application using Docker, using the following command:

./vendor/bin/sail up

With this our application will be running and we will be able to see it from http://localhost or http://127.0.0.1.

Note that if you are applying Sail to an existing project you may need to restore databases on the MySQL / Mariadb / PostgreSQL server. Below we explain some Sail commands that you may need to…

Loading Facebook Comments ...
Loading Disqus Comments ...