Table of Contents
If you’re working with Magento or any PHP project that uses Composer, you might encounter this confusing error:
“Package [vendor/package] exists in composer repo 1 and composer repo 2 which has a higher repository priority. The package will not be installable anymore if composer repo 2 is disabled.”At first glance, it sounds a bit technical, but don’t worry, we’ll break it down and explain how to fix it in simple terms.
What Does This Error Mean?
Composer downloads and manages your PHP packages from repositories (like Packagist or repo.magento.com). Sometimes, there’s one package found in more than one repository. In that case, Composer needs to decide which one to trust or work with based on something called repository priority.
This error occurs when:
- The same package exists in two different repositories, and
- One repository is of a higher priority, but you’re trying to install the package from the other one.
Essentially, Composer is telling you that:
“Hey, I discovered this package in two places, but I’ll only use the higher-priority one. If you remove that repo with a higher priority later, I won’t be able to install the package anymore.”
When Does This Happen in Magento?
Magento uses its own Composer repository: repo.magento.com. But many Magento extensions are also available on Packagist (Composer’s default repo).
If the same extension is present in both:
- Composer may get confused about which one to use.
- Magento might prefer repo.magento.com by default, but you might be installing from Packagist or another source.
How to Fix “Package exists in composer repo 1 and composer repo 2”
Here are a few ways to solve this issue:
1. Set Repository Priority in composer.json
You can tell Composer which repository to prefer by setting the only or exclude option. For example, if you want to use Packagist but Composer is defaulting to Magento’s repo, do this:
"repositories": [
{
"type": "composer",
"url": "https://repo.magento.com",
"exclude": ["vendor/package-name"]
},
{
"packagist.org": false
},
{
"type": "composer",
"url": "https://packagist.org"
}
] This tells Composer:
- “Don’t use repo.magento.com for this package.”
- “Instead, get it from Packagist.”
2. Use replace or provide
If you’re using a fork or custom version of a package, and want Composer to ignore the original one, you can use the replace key:
"replace": {
"vendor/package-name": "*"
}This tells Composer: “I’m already providing this package myself, don’t try to install it from elsewhere.”
3. Explicitly Require the Right Version and Source
Sometimes, just requiring the exact version of the package along with repository priorities is enough for the error to go away:
composer require vendor/package-name:^1.0 --with-all-dependenciesIf Composer still complains, you might need to adjust or temporarily remove some repositories in your composer.json until the installation works as expected.
Bonus Tip: Clear Composer Cache
After making changes, don’t forget to clear the Composer cache:
composer clear-cacheConclusion
The “Package exists in composer repo 1 and composer repo 2” error might seem alarming at first, but in reality, it simply boils down to Composer being cautious about where it pulls packages from. By specifically telling Composer which repo to use for which package, you can completely avoid this issue.
If you work with Magento projects that contain multiple custom modules or private repos, being aware of repository priorities in Composer is especially important.