Laravel项目克隆到VSCode及本地运行问题求助:WSL环境下Composer配置失败、PHP命令报错,求简便克隆方案
Hey there! Let's work through this step by step to get your Laravel project running smoothly, and also cover some simpler alternatives if you're stuck on the WSL/Composer setup headaches.
The most likely culprit is that your WSL environment doesn't have PHP (with required extensions) or Composer properly set up. Let's fix that:
Verify & Install PHP in WSL
Open your WSL terminal and runphp -v. If you get an error, install PHP and the extensions Laravel needs (for Ubuntu/Debian-based WSL):sudo apt update && sudo apt install php-cli php-mbstring php-xml php-zip php-mysqlDouble-check with
php -vto confirm it's installed successfully.Install Composer Globally
Run these commands in WSL to set up Composer the official way:php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');" php composer-setup.php php -r "unlink('composer-setup.php');" sudo mv composer.phar /usr/local/bin/composerTest it with
composer -v—you should see version info pop up if it worked.Clone & Configure the Project
Clone your project directly in WSL to avoid cross-filesystem glitches:git clone <your-project-repo-url> cd <your-project-folder>Run
composer installto install dependencies. If you hit permission errors, fix them with:sudo chmod -R 755 storage bootstrap/cache sudo chown -R $USER:$USER .Finish setup with these final steps:
cp .env.example .env php artisan key:generate php artisan serveNow open the project folder in VSCode via Remote-WSL, and you're ready to develop.
If WSL feels too fussy, try these low-config options:
Local Windows Setup (No WSL)
- Install XAMPP or WAMP—pick one that includes a PHP version matching your Laravel project's requirements (e.g., Laravel 10 needs PHP 8.1+).
- Download and install the official Windows Composer installer (it auto-detects your PHP path automatically).
- Open Windows Terminal, clone your project, then run the same setup commands:
composer install,cp .env.example .env,php artisan key:generate,php artisan serve.
Laravel Sail (Docker-Based)
This is Laravel's official zero-config way to run projects using Docker:- Install Docker Desktop (enable WSL integration if you prefer using WSL, or just use the native Windows version).
- Clone your project, then in the project folder, run:
# If you have Composer locally composer require laravel/sail --dev # If no local Composer, use this Docker command instead docker run --rm -v "$(pwd):/opt" -w /opt laravelsail/php82-composer:latest require laravel/sail --dev - Generate Sail's config:
php artisan sail:install(select services like MySQL when prompted). - Start the containers:
./vendor/bin/sail up—your project will be available athttp://localhost.
内容的提问来源于stack exchange,提问作者Marius

