Skip to content

Instantly share code, notes, and snippets.

@tooinfinity
Created June 3, 2024 13:45
Show Gist options
  • Save tooinfinity/44f29e9f960147e831e49218ee72310c to your computer and use it in GitHub Desktop.
Save tooinfinity/44f29e9f960147e831e49218ee72310c to your computer and use it in GitHub Desktop.
setup environment Laravel sail and Docker windows 11

Setting Up Laravel Sail and Docker on Windows 11 with WSL

Prerequisites

  1. Windows 11
  2. WSL 2 (Windows Subsystem for Linux)
  3. Docker Desktop

Step 1: Install WSL 2

  1. Enable WSL: Open PowerShell as an administrator and run the following command to enable WSL:

    wsl --install
  2. Set WSL 2 as Default: Ensure WSL 2 is set as the default version:

    wsl --set-default-version 2
  3. Install a Linux Distribution: Install a Linux distribution from the Microsoft Store, such as Ubuntu.

  4. Set Up Your Linux Distribution: Open the installed Linux distribution (e.g., Ubuntu) and complete the initial setup.

Step 2: Install Docker Desktop

  1. Download and Install Docker Desktop: Download Docker Desktop from Docker's official website and install it.

  2. Enable WSL 2 Based Engine: During the installation, ensure the "Use the WSL 2 based engine" option is selected.

  3. Configure WSL Integration: Open Docker Desktop and go to Settings -> Resources -> WSL Integration. Enable integration with your installed WSL 2 distributions (e.g., Ubuntu).

Step 3: Install Laravel and Sail

  1. Open Ubuntu: Open your Linux distribution (e.g., Ubuntu) and navigate to your working directory.

  2. Install Composer: If Composer is not already installed, run the following commands:

    sudo apt update
    sudo apt install curl php-cli php-mbstring git unzip
    curl -sS https://getcomposer.org/installer | php
    sudo mv composer.phar /usr/local/bin/composer
  3. Create a New Laravel Project: Create a new Laravel project using Composer:

    composer create-project laravel/laravel example-app
    cd example-app
  4. Install Laravel Sail: Require Laravel Sail as a development dependency:

    composer require laravel/sail --dev
  5. Publish Sail's Docker Configuration: Publish Sail's Docker configuration:

    php artisan sail:install
  6. Start Docker Containers: Start the Docker containers:

    ./vendor/bin/sail up

Step 4: Configure Laravel Environment

  1. Copy Environment File: Copy the .env.example file to .env:

    cp .env.example .env
  2. Generate Application Key: Generate a new application key:

    ./vendor/bin/sail artisan key:generate
  3. Update Environment Variables: Update the .env file with the necessary database configuration if needed.

Step 5: Access Laravel Application

  1. Open Browser: Open your browser and navigate to http://localhost. You should see the Laravel welcome page.

Additional Commands

  • Stop Docker Containers:

    ./vendor/bin/sail down
  • Run Artisan Commands: To run other Artisan commands, prefix them with Sail:

    ./vendor/bin/sail artisan migrate

Troubleshooting

If you encounter any issues, ensure:

  • Docker Desktop is running.
  • WSL 2 is properly configured.
  • The Linux distribution is properly set up.

By following these steps, you should have a fully functional Laravel Sail and Docker environment set up on your Windows 11 machine using WSL 2.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment