composer/composer · warning · InvalidArgumentException

--prefer-source can not be used together with --prefer-insta

Error message

--prefer-source can not be used together with --prefer-install

What it means

Thrown by BaseCommand::getPreferredInstallOptions() when both --prefer-source and --prefer-install are set on the same invocation. --prefer-install is the unified replacement flag and is mutually exclusive with the legacy --prefer-source.

Source

Thrown at src/Composer/Command/BaseCommand.php:347

            case 'source':
                $preferSource = true;
                break;
            case 'dist':
                $preferDist = true;
                break;
            case 'auto':
            default:
                // noop
                break;
        }

        if (!$input->hasOption('prefer-dist') || !$input->hasOption('prefer-source')) {
            return [$preferSource, $preferDist];
        }

        if ($input->hasOption('prefer-install') && is_string($input->getOption('prefer-install'))) {
            if ($input->getOption('prefer-source')) {
                throw new \InvalidArgumentException('--prefer-source can not be used together with --prefer-install');
            }
            if ($input->getOption('prefer-dist')) {
                throw new \InvalidArgumentException('--prefer-dist can not be used together with --prefer-install');
            }
            switch ($input->getOption('prefer-install')) {
                case 'dist':
                    $input->setOption('prefer-dist', true);
                    break;
                case 'source':
                    $input->setOption('prefer-source', true);
                    break;
                case 'auto':
                    $preferDist = false;
                    $preferSource = false;
                    break;
                default:
                    throw new \UnexpectedValueException('--prefer-install accepts one of "dist", "source" or "auto", got '.$input->getOption('prefer-install'));
            }

View on GitHub (pinned to 6ffc117740)

Solutions

  1. Remove --prefer-source and keep --prefer-install=source (the modern equivalent).
  2. Or drop --prefer-install and keep only --prefer-source (legacy).
  3. Standardize on --prefer-install across scripts.

Example fix

// before
composer require pkg --prefer-source --prefer-install=dist
// after
composer require pkg --prefer-install=source
Defensive patterns

Strategy: validation

Validate before calling

if ($input->getOption('prefer-install') !== null && $input->getOption('prefer-source')) {
    throw new \InvalidArgumentException('Use --prefer-install OR --prefer-source, not both');
}

Prevention

When it happens

Trigger: Running a command (install/update/require) with --prefer-source together with --prefer-install=<value>; getPreferredInstallOptions detects --prefer-install set as a string and --prefer-source also true.

Common situations: Copy-pasting flags from old scripts while adding the newer --prefer-install; CI configs accumulated over time; wrapper scripts passing both.

Related errors


AI-assisted analysis of composer/composer@6ffc117740 (2026-08-07). Data as JSON: /api/errors/89a225cb6293cd18. Report an issue: GitHub.