The requested package ... could not be found in any version - php

When I want to require my project, the following errors shows up:
The requested package mvc-php/framework could not be found in any version, there may be a typo in the package name.
The "mvc-php/framework" is a git folder.
{
"name": "mvc-php/app",
"repositories": [
{
"type": "path",
"url": "/Users/youri/Documents/Github/framework"
}
],
"require": {
"php": ">=7.0",
"mvc-php/framework": "master"
},
"autoload": {
"psr-4": {
"App\\": "app/"
}
}
}
Project i want to require:
{
"name": "mvc-php/framework",
"description": "PHP MVC framework",
"autoload": {
"psr-4": {
"Mvc\\" : "src/"
}
},
"require": {
"php": ">=7.0"
}
}

Instead of just the branch name, you must require branchName#dev
https://getcomposer.org/doc/articles/versions.md#branches
{
"name": "mvc-php/app",
"repositories": [
{
"type": "path",
"url": "/Users/youri/Documents/Github/framework"
}
],
"require": {
"php": ">=7.0",
"mvc-php/framework": "master#dev"
},
"autoload": {
"psr-4": {
"App\\": "app/"
}
}
}

The requested package X/Y could not be found in any version.
The requested package needs to be a git folder with the committed and existing composer.json file. Then to reference specific branch, you need to add the dev- prefix, so dev-master, not master.
Example
Here is the minimal working example:
File: composer.json
{
"require": {
"local/my_package": "dev-master"
},
"repositories": [
{
"packagist.org": false
},
{
"type": "path",
"url": "my_package/"
}
]
}
File: my_package/composer.json
{
"name": "local/my_package",
"require-dev": {
"symfony/console": "*"
}
}
Note: Above file is under local Git repository. To create one, run: git init && git commit -am 'Files'.
Troubleshooting
To troubleshoot the issue, run:
composer install -vvv
Also consider running: composer diagnose to identify common Composer errors.

As this is the first response when searching the error text on Google, I will also put my fix here, despite not being 100% relevant to the OP.
When you are requiring the repo, you need to make sure that your requires statement matches the name of the project in the composer.json of the project.
So if the name had been "name": "mvc-php/app-framework", in the framework project, then the require would need to be:
"require": {
"mvc-php/app-framework": "dev-master"
},
This is more applicable when you are adding a git repo. Especially when forking, as sometimes the git url might be different from the composer.json name.
Additionally (and this is the part relevant to OP), you now need to do dev-branch_name instead of branch_name#dev when requiring. I don't know when this changed, or if the old method is unusable. But this is what the current composer docs now say.
If you want Composer to check out a branch instead of a tag, you need to point it to the branch using the special dev-* prefix
Composer Documentation - Versions and Constraints - Branches

Another Gotcha to be Aware Of:
I changed the name of a package I developed and was just testing a branch on it. I had followed all the correct naming conventions mentioned above but was still getting the given error.
It turns out that for the name change to be picked up, you have to update the package name in composer.json on the main branch of the package repo (Master for me) even if you are not using that branch within your project.

It is important to note that if you do not add your own mirror source to the global variable, an error will occur where the sub-scene is not found.
You can add this in composer.json:
"repositories":[
{
"type":"composer",
"url":"https://packag"
}
],

Related

PHP composer: Update local path repository dependency

I have a project which requires a bunch of local path repositories. Those local path repositories partly require each other and also remote repositories.
For example one of the local repo composer.json looks like this:
{
"description": "",
"type": "neos-project",
"name": "mapo/campaign",
"repositories": [
{
"type": "path",
"url": "Source/Mapo.NodeTypes"
}
],
"require": {
"mapo/nodetypes": "*"
},
"autoload": {
"psr-4": {
"Mapo\\Campaign\\": "Classes/"
}
},
"extra": {
"neos": {
"package-key": "Mapo.Campaign"
}
}
}
The mapo/nodetypes local package then requires also a private remote repository:
{
"description": "",
"type": "neos-project",
"name": "mapo/nodetypes",
"minimum-stability": "dev",
"repositories": [
{
"type": "git",
"url": "url to private repo.git"
},
{
"type": "path",
"url": "../Mapo.Somepackage"
},
],
"require": {
"mapo/privateproject": "*",
"mapo/somepackage": "#dev"
},
"autoload": {
"psr-4": {
"Mapo\\NodeTypes\\": "Classes/"
}
},
"extra": {
"neos": {
"package-key": "Mapo.NodeTypes"
}
}
}
Now I need to test a new feature in mapo/privateproject. So I created a new branch in the private repo called issue0815 and made my changes. I also created a new branch issue0815 in the main mapo project (which has the local path composer.json changes locally).
I updated the dependency of mapo/nodetypes to "mapo/privateproject": "dev-issue0815",.
My problem is that, no matter which command, composer refuses to install the issue0815 branch for the private repository. At first composer complained, that it cannot install the dependency dev-issue0815 because the composer.lock prevented it. So I removed all usages of the private repo and the mapo/nodetypes from my composer.lock.
What surprised me the most, was that composer recovered the original composer.lock file. It just completely ignored my current local main project branch - which has the modification for the local path repo composer.json files and just required the contents from the master branches.
So, how can I update a dependency of a local path repo which needs a specific branch from a private repo?
This one took me quite a while, but the reason why my changes to composer.json have been completely ignored including also when I deleted the composer.lock was that, composer ignores your changes if it concludes, that the packages from vendor/composer/installed.json are correct.
From https://github.com/composer/composer/issues/4312#issuecomment-191488570:
Right now Composer prefers installed packages, derived from the installed.json file. So as long as the replacement is valid, it will indeed keep generating the same solution and thus lock. This is just because that's how Composer works internally.
To 'reset' it completely and make it rethink its installed packages (before it automatically 'corrects' because of package updates) you'd have to remove vendor/composer/installed.json as well as the lock. This will trigger a complete reevaluation of dependencies and reinstall. You could also just go for the full cleanup then and delete the entire vendor directory.
Thats also the reason why it is often suggested to remove the vendor directory, I think.

Can't require local package with composer

I'm currently working on a Composer package and now I want to test how it would integrate in my current application without pushing it to a remote.
So I stumbled upon this blog post which explains how to do that. However, that didn't work. I'm getting the message
Your requirements could not be resolved to an installable set of packages.
Problem 1 - The requested package tzfrs/x-bundle could not be found in any version, there may be a typo in the package name.
My directory structure looks like this
dev
Project
composer.json
Package
composer.json
The composer.json of my package looks like this
{
"name": "tzfrs/x-bundle",
"license": "proprietary",
"require": {
"php": "^7.0",
"psr/log": "~1.0"
},
"require-dev": {
"phpunit/phpunit": "~5.5"
},
"autoload": {
"psr-4": { "tzfrs\\XBundle\\": "" },
"exclude-from-classmap": [
"/Tests/"
]
}
}
And the one of my project looks like this (I'm leaving the symfony stuff and other packages/stuff out)
{
"repositories": [
{
"type": "git",
"url": "../Package",
}
],
"minimum-stability" : "beta",
"require": {
"tzfrs/x-bundle": "*"
}
}
So when doing composer update tzfrs/x-bundle I should get my new project right? However, I'm getting the message I posted above. What am I doing wrong here?
I already did git init in my package folder and also comitted all my changes. So I have a repo which is not empty already.

Private repo composer.json not being used

My problem is that a privately made repo's composer.json seems to be broken when trying to use it as a package elsewhere.
I have a private repo with code needed for other projects. The repo's composer.json looks like this:
{
"name": "somevendor/global",
"require": {
"nesbot/carbon": "^1.21"
},
"autoload": {
"psr-4": {
"" : "src/"
},
"files": [
"somedir/somefile.php"
]
}
}
The src is in the base directory of the repo, and contains PSR-4 namespaced classes. I have namespace folders within that, e.g. a Foo directory with classes in the Foo namespace:
-- src
-- Foo
// some Foo\... classes
// some global namespace classes
-- somedir
somefile.php // A file with helper functions
In the project folder, I'm accessing the somevendor/global repo via a composer.json file:
{
"require": {
"somevendor/global-folder": "dev-master"
},
"repositories": [
{
"type": "package",
"package": {
"name": "somevendor/global",
"version": "dev-master",
"type": "package",
"source": {
"url": "git#bitbucket.org/somevendor/global.git",
"type": "git",
"reference": "master"
}
}
}
]
}
Running composer install in the project folder seems to work at first. I have installed SSH keys properly so it can access the private repo on Bitbucket and grab the files:
$ composer install
Loading composer repositories with package information
Updating dependencies (including require-dev)
- Installing somevendor/global (dev-master master)
Cloning master
Writing lock file
Generating autoload files
And then in the project's PHP code I require vendor/autoload.php, but none of the classes are being autoloaded, including the Carbon package specified in the first repo's composer.json file:
Fatal error: Uncaught Error: Class 'Foo\Foo' not found in...
I've clearly made a mistake here, have I structured the first repo wrongly?
I "solved" this by taking out all of the "require" entries from the remote repo's composer.json file and moving them to the local website's composer.json file.
This is what the files looked like:
The remote private repo's composer.json:
{
"name": "somevendor/global",
"license": "proprietary",
"autoload": {
"psr-4": {
"" : "src/"
},
"files": [
"functions/functions.php"
]
}
}
The local website's composer.json:
{
"require": {
"nesbot/carbon": "^1.21",
"somevendor/global": "dev-master"
},
"repositories": [
{
"type": "vcs",
"url": "git#bitbucket.org:somevendor/global.git"
}
]
}
It kept throwing Composer\Repository\InvalidRepositoryException because I forgot to put the name into the remote repo's composer.json file, so don't forget that bit!
Also remember to set up your git ssh keys if you've set them up. I used this Bitbucket tutorial to do this.

How to patch a Composer library? [duplicate]

This is my composer.json, I want to use Nodge's fork of lessphp project on Github
"repositories": [{
"type": "package",
"package": {
"version": "dev-master",
"name": "nodge/lessphp",
"source": {
"url": "https://github.com/Nodge/lessphp.git",
"type": "git",
"reference": "master"
},
"autoload": {
"classmap": ["lessc.inc.php"]
}
}
}],
"require": {
"php": ">=5.3.3",
"nodge/lessphp": "dev-master"
},
But I get this error when I run composer update:
nodge/lessphp dev-master -> no matching package found.
I don't know how to require correctly this fork.
The most common (and easiest) way of doing it is using a VCS repository.
All you have to do is add your fork as a repository and update the
version constraint to point to your custom branch. Your custom branch
name must be prefixed with dev-.
Assuming you forked monolog/monolog and created a branch called bugfix, you would update your composer.json like this:
{
"repositories": [
{
"type": "vcs",
"url": "https://github.com/igorw/monolog"
}
],
"require": {
"monolog/monolog": "dev-bugfix"
}
}
Note that you don't change the require statement except to specify your bugfix branch. You still reference the upstream package (monolog/monolog), not your personal fork (igorw/monolog), and the branch name is prefixed with dev-. You can read details in the docs
Using VCS works:
"name": "test/test",
"repositories": [{
"type": "vcs",
"url": "http://github.com/Nodge/lessphp"
}],
"require": {
"leafo/lessphp": "dev-master"
},
But if I require a module that has this composer.json, it doesn't work. It installs the original project, not the fork.
Example
"name": "example/example",
"require": {
"test/test": "dev-master"
},
I should mention again the repository. Is that normal?
If you can't get #Neilime answer to work for you, make sure your fork uses a different branch.
For example push your changes to a branch on your fork called my-bugfix, do not added dev- prefix in your branch name but in your composer.json you have to add it. Your composer file will look like:
"repositories":
[
{
"type": "vcs",
"url": "http://github.com/yourname/packageName"
}
],
"require": {
"owner/packageName": "dev-my-bugfix"
},
I have tried many options but After I got this post I saw the light and it just worked perfect.
This is what you have to do:
1- Fork The repository
2- Create a branch and make the required modifications.
3- Add the repository label to your composer.json
"repositories": [
{
"type": "vcs",
"url": "https://github.com/user/yourforkname"
}
]
4- In the command line inside your project require your fork like this:
composer require vendor/packagename:dev-branchname
And Voilá!!
You have your fork version working
According to the Composer documentation
http://getcomposer.org/doc/05-repositories.md#vcs, it's enough to
specify the original repository (not the fork) in the require ("nodge/lessphp" in your case). Composer will then install YOUR fork (look at the code in the vendors)
So, this is 2019, and most of the answers here are already correct.
If you find yourself however, in a situation where you need to require a particular branch of your fork (that you created), have composer list the available versions/tags first.
This saved me a lot of time.
A full example with spatie/laravel-backup package.
First, add repositories key to composer.json. With the url of your fork
"repositories": [{
"type": "vcs",
"url": "https://github.com/holymp2006/laravel-backup"
}]
Get available versions/tags
composer show "spatie/laravel-backup" --all
Choose the version you want from versions in the terminal output, then require that version
composer require spatie/laravel-backup:v5.x-dev
I usually add a "dist" node to the package definition.
I never had a problem using it this way.
I can't remember where I got this trick from, though, for any further explanations.
{
"repositories": [
{
"type": "package",
"package": {
"version": "dev-master",
"name": "nodge/lessphp",
"source": {
"url": "https://github.com/Nodge/lessphp.git",
"type": "git",
"reference": "master"
},
"autoload": {
"classmap": ["lessc.inc.php"]
},
"dist": {
"url": "https://github.com/Nodge/lessphp/archive/master.zip",
"type": "zip"
}
}
}
],
"require": {
"nodge/lessphp": "*"
}
}
The accepted answer and clarifying answers all worked well for me when I had ex. an application, which needed a dependency I had forked and modified. I’d still use the accepted answer in this case.
However, when I had a package I wanted to distribute myself on Packagist, which also contained a forked and modified dependency, this approach no longer worked.
If someone were to try and install with this config, they’ll still get that same -> no matching package found. error message.
This answer and the linked Composer docs suggest that the repo settings have to be at the top-level composer.json. That means, someone installing this package would have to add that config to their composer.json file too—which adds a lot of unnecessary confusion to the install process.
Instead, I published my fork to Packagist. My understanding is that while forks are frowned upon, this would be considered a maintained fork, since I am using it for this new package.
Hopefully that’s helpful for anyone who has this problem with a package or library they’d like to distribute.

How to require a fork with composer?

This is my composer.json, I want to use Nodge's fork of lessphp project on Github
"repositories": [{
"type": "package",
"package": {
"version": "dev-master",
"name": "nodge/lessphp",
"source": {
"url": "https://github.com/Nodge/lessphp.git",
"type": "git",
"reference": "master"
},
"autoload": {
"classmap": ["lessc.inc.php"]
}
}
}],
"require": {
"php": ">=5.3.3",
"nodge/lessphp": "dev-master"
},
But I get this error when I run composer update:
nodge/lessphp dev-master -> no matching package found.
I don't know how to require correctly this fork.
The most common (and easiest) way of doing it is using a VCS repository.
All you have to do is add your fork as a repository and update the
version constraint to point to your custom branch. Your custom branch
name must be prefixed with dev-.
Assuming you forked monolog/monolog and created a branch called bugfix, you would update your composer.json like this:
{
"repositories": [
{
"type": "vcs",
"url": "https://github.com/igorw/monolog"
}
],
"require": {
"monolog/monolog": "dev-bugfix"
}
}
Note that you don't change the require statement except to specify your bugfix branch. You still reference the upstream package (monolog/monolog), not your personal fork (igorw/monolog), and the branch name is prefixed with dev-. You can read details in the docs
Using VCS works:
"name": "test/test",
"repositories": [{
"type": "vcs",
"url": "http://github.com/Nodge/lessphp"
}],
"require": {
"leafo/lessphp": "dev-master"
},
But if I require a module that has this composer.json, it doesn't work. It installs the original project, not the fork.
Example
"name": "example/example",
"require": {
"test/test": "dev-master"
},
I should mention again the repository. Is that normal?
If you can't get #Neilime answer to work for you, make sure your fork uses a different branch.
For example push your changes to a branch on your fork called my-bugfix, do not added dev- prefix in your branch name but in your composer.json you have to add it. Your composer file will look like:
"repositories":
[
{
"type": "vcs",
"url": "http://github.com/yourname/packageName"
}
],
"require": {
"owner/packageName": "dev-my-bugfix"
},
I have tried many options but After I got this post I saw the light and it just worked perfect.
This is what you have to do:
1- Fork The repository
2- Create a branch and make the required modifications.
3- Add the repository label to your composer.json
"repositories": [
{
"type": "vcs",
"url": "https://github.com/user/yourforkname"
}
]
4- In the command line inside your project require your fork like this:
composer require vendor/packagename:dev-branchname
And Voilá!!
You have your fork version working
According to the Composer documentation
http://getcomposer.org/doc/05-repositories.md#vcs, it's enough to
specify the original repository (not the fork) in the require ("nodge/lessphp" in your case). Composer will then install YOUR fork (look at the code in the vendors)
So, this is 2019, and most of the answers here are already correct.
If you find yourself however, in a situation where you need to require a particular branch of your fork (that you created), have composer list the available versions/tags first.
This saved me a lot of time.
A full example with spatie/laravel-backup package.
First, add repositories key to composer.json. With the url of your fork
"repositories": [{
"type": "vcs",
"url": "https://github.com/holymp2006/laravel-backup"
}]
Get available versions/tags
composer show "spatie/laravel-backup" --all
Choose the version you want from versions in the terminal output, then require that version
composer require spatie/laravel-backup:v5.x-dev
I usually add a "dist" node to the package definition.
I never had a problem using it this way.
I can't remember where I got this trick from, though, for any further explanations.
{
"repositories": [
{
"type": "package",
"package": {
"version": "dev-master",
"name": "nodge/lessphp",
"source": {
"url": "https://github.com/Nodge/lessphp.git",
"type": "git",
"reference": "master"
},
"autoload": {
"classmap": ["lessc.inc.php"]
},
"dist": {
"url": "https://github.com/Nodge/lessphp/archive/master.zip",
"type": "zip"
}
}
}
],
"require": {
"nodge/lessphp": "*"
}
}
The accepted answer and clarifying answers all worked well for me when I had ex. an application, which needed a dependency I had forked and modified. I’d still use the accepted answer in this case.
However, when I had a package I wanted to distribute myself on Packagist, which also contained a forked and modified dependency, this approach no longer worked.
If someone were to try and install with this config, they’ll still get that same -> no matching package found. error message.
This answer and the linked Composer docs suggest that the repo settings have to be at the top-level composer.json. That means, someone installing this package would have to add that config to their composer.json file too—which adds a lot of unnecessary confusion to the install process.
Instead, I published my fork to Packagist. My understanding is that while forks are frowned upon, this would be considered a maintained fork, since I am using it for this new package.
Hopefully that’s helpful for anyone who has this problem with a package or library they’d like to distribute.

Categories