Modernizing the PHP Onboarding Experience with Laravel Sail
The Mission to Lower the Barrier to Entry
Software development moves fast, and even established ecosystems can accumulate technical debt in their documentation. For
The philosophy behind this update is simple: a developer should be able to go from a fresh laptop to a running application with nothing but
Prerequisites and Environment Setup
Before jumping into the commands, you need a basic understanding of
Key Libraries and Tools
- Laravel Sail: A light CLI shim for interacting withDocker Compose.
- Laravel Breeze: A minimal, simple starter kit for authentication usingBladeandTailwind CSS.
- Mailhog: An email testing tool that captures outgoing mail for easy previewing.
- Composer&npm: Dependency managers forPHPandJavaScript, respectively, both of which run inside theLaravel Sailcontainers.
Code Walkthrough: Building Your First App
The initialization starts with a simple curl command that fetches a specialized installation script. This script handles the
curl -s https://laravel.build/example-app | bash
This command triggers a small, temporary
cd example-app
./vendor/bin/sale up
The up command initializes the docker-compose.yml. On the first run, this pulls the necessary images for http://localhost.
To interact with the environment, sail:
sail artisan migrate
sail composer require laravel/breeze --dev
sail npm install && sail npm run dev
These commands execute inside the container, ensuring that the environment exactly matches what is defined in your project configuration.
Syntax Notes and Best Practices
A major convenience factor involves setting up a bash alias. Typing ./vendor/bin/sail every time is tedious. By adding alias sail='[ -f sail ] && sh sail || sh vendor/bin/sail' to your shell profile, you can simply type sail for all interactions.
Another notable pattern is the use of
Practical Examples and Debugging
One of the most practical features included in the default http://localhost:8025 to see every email your application sends.
If you need to perform manual database maintenance, you don't need a special 127.0.0.1 on port 3306, as
Tips and Gotchas
- Permissions: On Linux, you might encounter file permission issues whenDockercreates files as the root user.Laravel Sailattempts to handle this by mapping your local user ID to the container user.
- Existing Services: If you already have MySQLorApacherunning on your host machine,Laravel Sailmay fail to start because ports 80 or 3306 are already taken. Be sure to stop local services before running
sail up. - Version Switching: You can easily toggle between PHPversions by changing the build context in your
docker-compose.ymland rebuilding the containers withsail build --no-cache.
