[Laravel]使用Laravel Sail创建项目卡死不动

通过Laravel Sail创建项目的时候可能会一直卡在这,直到提示超时,这是因为默认使用的composer源是国外的,无法拉取包;

xxx>curl -s https://laravel.build/example-project | bash

 _                               _
| |                             | |
| |     __ _ _ __ __ ___   _____| |
| |    / _` | '__/ _` \ \ / / _ \ |
| |___| (_| | | | (_| |\ V /  __/ |
|______\__,_|_|  \__,_| \_/ \___|_|

Warning: TTY mode requires /dev/tty to be read/writable.
    Creating a "laravel/laravel" project at "./example-project"
    Info from https://repo.packagist.org:
    Installing laravel/laravel (v9.2.0)
      - Downloading laravel/laravel (v9.2.0)
      - Installing laravel/laravel (v9.2.0): Extracting archive
    Created project in /opt/example-project
    > @php -r "file_exists('.env') || copy('.env.example', '.env');"
    Loading composer repositories with package information
    Info from https://repo.packagist.org:

直接访问laravel.build的链接获取到对应的shell脚本

docker info > /dev/null 2>&1

# Ensure that Docker is running...
if [ $? -ne 0 ]; then
    echo "Docker is not running."

    exit 1
fi

docker run --rm \
    -v "$(pwd)":/opt \
    -w /opt \
    laravelsail/php81-composer:latest \
    bash -c "laravel new example-project && cd example-project && php ./artisan sail:install --with=mysql,redis,meilisearch,mailhog,selenium "

cd example-project

CYAN='\033[0;36m'
LIGHT_CYAN='\033[1;36m'
WHITE='\033[1;37m'
NC='\033[0m'

echo ""

if sudo -n true 2>/dev/null; then
    sudo chown -R $USER: .
    echo -e "${WHITE}Get started with:${NC} cd example-project && ./vendor/bin/sail up"
else
    echo -e "${WHITE}Please provide your password so we can make some final adjustments to your application's permissions.${NC}"
    echo ""
    sudo chown -R $USER: .
    echo ""
    echo -e "${WHITE}Thank you! We hope you build something incredible. Dive in with:${NC} cd example-project && ./vendor/bin/sail up"
fi

将docker run这句话的bash -c加一句修改成阿里云composer源即可:

docker run --rm \
    -v "$(pwd)":/opt \
    -w /opt \
    laravelsail/php81-composer:latest \
    bash -c "composer config -g repo.packagist composer https://mirrors.aliyun.com/composer && ...<<此处省略>>. "

直接将修改后的复制到命令行执行。

2023-06-08记录:

有时候阿里云源的包会有问题,会提示版本不可用之类的导致无法install包,只能切换回原始源。