Getting Started with Laravel Sail: A Modern Installation Guide
Overview
Prerequisites
To follow this guide, you should have a basic grasp of PHP and command-line interfaces. Most importantly, you must have
Key Libraries & Tools
- Laravel Sail: A light-weight command-line interface for interacting with Laravel's default Docker configuration.
- Docker: A platform that uses containers to package applications with all their dependencies.
- cURL: A tool used to transfer data from or to a server, used here to fetch the installation script.
- Laravel Valet: A macOS-specific development environment alternative for those who prefer a native setup over containers.
Code Walkthrough
Setting up a new project starts with a single command in your terminal. This script fetches a fresh installation and configures your local directory.
curl -s https://laravel.build/example-app | bash
This command pulls the latest Laravel skeleton. If you need specific services like PostgreSQL instead of the default MySQL, you can append a query string: ?with=postgres,redis. Once the downloader finishes, navigate into your folder and boot the environment:
cd example-app
./vendor/bin/sail up
The up command builds your containers. The first run takes time as it downloads base images for PHP, MySQL, and Redis, but subsequent starts are nearly instantaneous.
Syntax Notes
Notice the use of ./vendor/bin/sail. This points to the executable script within your project's local dependencies. It is a wrapper for docker-compose. Using the -d flag (e.g., sail up -d) runs the containers in "detached" mode, meaning they stay active in the background and free up your terminal for other commands.
Practical Examples
Once the containers are active, visiting http://localhost in your browser reveals the Laravel welcome screen. This environment is perfect for building RESTful APIs, e-commerce platforms, or SaaS applications. Because it includes Redis by default, you can immediately implement high-performance caching or real-time broadcasting features.
Tips & Gotchas
Avoid the common mistake of trying to run php artisan commands directly on your host machine. Since your PHP environment lives inside the container, you should prefix your commands with sail. For example, use sail artisan migrate instead of php artisan migrate. If you need to stop the environment while in detached mode, use sail stop to gracefully shut down the database and server containers.
