Cui Mingda
Cui Mingda

Reputation: 823

How to use global composer.json when create project in Composer?

I have add the following config, and if I reqiure global package, all is Okey. Every packages will be installed from mirrors.aliyun.com

composer global config repo.packagist composer https://mirrors.aliyun.com/composer/

and content from ~/.composer/composer.json

{
    "require": {
        "laravel/installer": "^4.0",
        "laravel/valet": "^2.11"
    },
    "repositories": {
        "packagist": {
            "type": "composer",
            "url": "https://mirrors.aliyun.com/composer/"
        }
    }
}

But if I create project with composer like this, I found package downloading from https://repo.packagist.org/packages.json

composer create-project --prefer-dist laravel/laravel sample.com -vvv

I need to download from mirror, but the config in global composer.json is ignored, anyone knows why is that?

Here is some related logs

Reading /Users/jack/.composer/composer.json
Loading config file /Users/jack/.composer/config.json
Loading config file /Users/jack/.composer/auth.json
Loading config file /Users/jack/.composer/composer.json
Loading config file /Users/jack/.composer/auth.json
Reading /Users/jack/.composer/auth.json
Reading /Users/jack/.composer/vendor/composer/installed.json
Creating a "laravel/laravel" project at "./sample.com"
Downloading https://repo.packagist.org/packages.json

UPDATE @ 19:55

I add --repository parameter, then the laravel/laravel template is downloaded from mirror repository

composer create-project laravel/laravel sample.com --prefer-dist --repository=https://mirrors.aliyun.com/composer/ -vvv

But after downloading laravel/laravel, still download from Packagist, here are some related logs

Reading /Users/cuimingda/.composer/composer.json
Loading config file /Users/cuimingda/.composer/config.json
Loading config file /Users/cuimingda/.composer/auth.json
Loading config file /Users/cuimingda/.composer/composer.json
Loading config file /Users/cuimingda/.composer/auth.json
Reading /Users/cuimingda/.composer/auth.json
Reading /Users/cuimingda/.composer/vendor/composer/installed.json
Creating a "laravel/laravel" project at "./api.mingda.net"
Downloading https://mirrors.aliyun.com/composer/packages.json
Installing laravel/laravel (v7.25.0)

Created project in /Users/cuimingda/Sites/api.mingda.net
Reading ./composer.json
Reading /Users/cuimingda/.composer/composer.json
Loading config file /Users/cuimingda/.composer/config.json
Loading config file /Users/cuimingda/.composer/auth.json
Loading config file /Users/cuimingda/.composer/composer.json
Loading config file /Users/cuimingda/.composer/auth.json
Reading /Users/cuimingda/.composer/auth.json
Reading /Users/cuimingda/.composer/vendor/composer/installed.json
> post-root-package-install: @php -r "file_exists('.env') || copy('.env.example', '.env');"
Executing command (CWD): '/usr/local/Cellar/php/7.4.8/bin/php' -d allow_url_fopen='1' -d disable_functions='' -d memory_limit='1536M' -r "file_exists('.env') || copy('.env.example', '.env');"
Loading composer repositories with package information
Downloading https://repo.packagist.org/packages.json

Upvotes: 0

Views: 1444

Answers (1)

Cui Mingda
Cui Mingda

Reputation: 823

I just found the answer myself.

DO NOT USE the follow methods to set mirror, because they will modify ~/.composer/composer.json, and composer.json will not work when create project

composer global config repo.packagist composer https://mirrors.aliyun.com/composer/

RIGHT WAY - Manually put the following code to ~/.composer/config.json, and whether composer global require or composer create-project, they all will fetch files from mirror site, not packagist.org

{
    "config": {},
    "repositories": [
        {
            "type": "composer",
            "url": "https://mirrors.aliyun.com/composer/"
        },
        {
            "packagist": false
        }
    ]
}

There is no need to add any repositories related configto ~/.composer/composer.json

{
    "require": {
        "laravel/installer": "^4.0",
        "laravel/valet": "^2.11"
    }
}

Upvotes: 1

Related Questions