DEV Community

Cover image for Setting Up a Development Environment Using Laravel Sail (Docker)
Guilherme Lauxen
Guilherme Lauxen

Posted on

Setting Up a Development Environment Using Laravel Sail (Docker)

Laravel Sail is a lightweight command-line interface for interacting with Laravel’s Docker development environment. It provides a complete development environment that includes PHP, MySQL, Redis and more, without the need for manual installation of these services on your local machine. Laravel Sail is perfect for those who want to quickly start a new Laravel project with a standardized development environment.

In this post, I’ll show you how to set up the environment with Laravel Sail without having to install the environment locally.

We will only need Docker. 🐳

Installing the Environment with Laravel Sail

Step 1: Prerequisites

Before you begin, make sure you have Docker installed on your machine. You can download and install Docker from the official website: Docker.

Step 2: Installing Laravel Sail

Installing Laravel Sail is simple and can be done with a single command. Below are the instructions to install Laravel Sail on MacOS, Windows, and Linux.
Open your terminal and run the following command:

curl -s "https://laravel.build/example-app" | bash
Enter fullscreen mode Exit fullscreen mode

This command will download and configure a new Laravel application called example-app in a directory with the same name. Feel free to replace example-app with your project’s name.

Choosing Your Sail Services

When creating a new Laravel application via Sail, you may use the with query string variable to choose which services should be configured in your new application’s docker-compose.yml file. For available services, see the Laravel Sail documentation.

curl -s "https://laravel.build/example-app?with=mysql,redis" | bash
Enter fullscreen mode Exit fullscreen mode

If you do not specify which services you would like configured, a default stack will be set up. In Laravel 11, this default stack includes mysql, redis, meilisearch, mailpit, and selenium.

Step 3: Starting Laravel Sail

Once the installation is complete, navigate to your project’s directory and start the Laravel Sail development environment:

cd example-app
./vendor/bin/sail up
Enter fullscreen mode Exit fullscreen mode

This command will start the Docker containers defined by Laravel Sail, including the web server, database, and other services.

Step 4: Running Migrations

After starting the environment with sail up, you will need to run the migrations to set up the database:

./vendor/bin/sail artisan migrate
Enter fullscreen mode Exit fullscreen mode

This command will apply all pending migrations, setting up the database for use with your Laravel application.

Creating an Alias for the Sail Command

To make using the ./vendor/bin/sail command easier, you can create an alias in your terminal. This will allow you to run sail commands without typing the full path.

Creating an Alias on MacOS and Linux

Open your shell configuration file (usually ~/.bashrc, ~/.zshrc, or ~/.bash_profile) and add the following line:

alias sail='[ -f sail ] && bash sail || bash vendor/bin/sail'
Enter fullscreen mode Exit fullscreen mode

Then, reload the shell configuration file:

source ~/.bashrc
# or
source ~/.zshrc
# or
source ~/.bash_profile
Enter fullscreen mode Exit fullscreen mode

Creating an Alias on Windows (PowerShell)

To create an alias in PowerShell, add the following line to your PowerShell profile ($PROFILE):

New-Alias sail vendor/bin/sail
Enter fullscreen mode Exit fullscreen mode

Then, reload the PowerShell profile:

. $PROFILE
Enter fullscreen mode Exit fullscreen mode

Using the Alias

Now you can use the sail command directly in the terminal. For example, to start the development environment, simply run:

sail up
Enter fullscreen mode Exit fullscreen mode

To run migrations, use:

sail artisan migrate
Enter fullscreen mode Exit fullscreen mode

Tip: Installing PHP Modules and Extensions

If you need to install additional PHP modules or extensions, such as GMP, you can access the Dockerfile used by Laravel Sail and make the necessary modifications.

  1. Navigate to the ./vendor/laravel/sail/runtimes/ directory.
  2. Choose the version of PHP you are using (e.g., 8.3).
  3. Open the corresponding Dockerfile and add the instructions to install the desired extension. For example, to install php8.3-gmp, you can add:

Install GMP

After modifying the Dockerfile, rebuild the Docker containers to apply the changes:

sail build --no-cache
Enter fullscreen mode Exit fullscreen mode

Conclusion

Laravel Sail simplifies the setup of the development environment, providing a standardized and easy-to-use environment. With the steps above, you can install Laravel Sail on MacOS, Windows, and Linux, create an alias to make using Sail commands easier, and install additional modules as needed.

Try it out and see how Laravel Sail can speed up your development workflow!

Hope it helps!! 🤓

Top comments (0)