This content originally appeared on DEV Community and was authored by Guilherme Lauxen
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
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
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
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
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'
Then, reload the shell configuration file:
source ~/.bashrc
# or
source ~/.zshrc
# or
source ~/.bash_profile
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
Then, reload the PowerShell profile:
. $PROFILE
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
To run migrations, use:
sail artisan migrate
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.
- Navigate to the
./vendor/laravel/sail/runtimes/
directory. - Choose the version of PHP you are using (e.g., 8.3).
- Open the corresponding Dockerfile and add the instructions to install the desired extension. For example, to install
php8.3-gmp
, you can add:
After modifying the Dockerfile, rebuild the Docker containers to apply the changes:
sail build --no-cache
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!! 🤓
This content originally appeared on DEV Community and was authored by Guilherme Lauxen
Guilherme Lauxen | Sciencx (2024-07-26T02:18:25+00:00) Setting Up a Development Environment Using Laravel Sail (Docker). Retrieved from https://www.scien.cx/2024/07/26/setting-up-a-development-environment-using-laravel-sail-docker/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.