Let's say we have the following directory structure, we assume my-package also exists on Packagist:
- apps
\_ my-app
\_ composer.json
- packages
\_ my-package
\_ composer.json
To add my/package as a dependency of my-app, the documentation states we can use the following configuration:
{
"repositories": [
{
"type": "path",
"url": "../../packages/my-package"
}
],
"require": {
"my/package": "*"
}
}
However when I composer update, the dependency is still downloaded from Packagist. So, to see, I disabled Packagist.org:
{
"repositories": [
{
"type": "path",
"url": "../../packages/my-package",
"packagist.org": false
}
],
"require": {
"my/package": "*"
}
}
I cleared the cache with composer clearcache, removed my/package with composer remove my/package and installed it again with composer require my/package --prefer-source (I didn't understand if --prefer-source is for vcs only). The downloaded package is still not the local one. How to force composer to use the local one?
"require": {
"my/package": "*"
}
In case of VCS or path repository types, you need to specify version of the package you request. So instead of using *, as you have currently, use #dev:
"require": {
"my/package": "#dev"
}
For
composer --version
Composer version 2.0.14
in composer.json file
"require": {
"my/package": "dev-master"
}
Related
I'm currently developing my own composer packages, but they failed trying to execute composer update.
This is my root composer.json:
{
...
"repositories": {
"bar": {
"type": "path",
"url": "packages/pinnokkio/bar",
"options": {
"symlink": true
}
}
},
"require": {
"php": "^8.0",
...
"pinnokkio/bar": "#dev"
},
...
}
This means, package pinnokkio/bar should be installed. And package pinnokkio/bar requires pinnokkio/foo as a dependency.
composer.json of pinnokkio/bar:
{
"name": "pinnokkio/bar",
...
"repositories": {
"foo": {
"type": "path",
"url": "../foo",
"options": {
"symlink": true
}
}
},
"require": {
"pinnokkio/foo": "#dev"
},
...
}
composer.json of pinnokkio/foo:
{
"name": "pinnokkio/foo",
"require": {
"php": "^8.0"
},
...
}
All in all, in my main composer.json, I'm going to require a package that requires pinnokkio/foo to run. And further upcoming packages all require pinnokkio/foo in order to run, but unfortunately, I'm getting this error trying to execute composer update. All packages are located in <root>/packages/:
Problem 1
- pinnokkio/bar[dev-feature-modules, dev-master] require pinnokkio/foo#dev -> could not be found in any version, there may be a typo in the package nam
e.
- Root composer.json requires pinnokkio/bar#dev -> satisfiable by pinnokkio/bar[dev-feature-modules, dev-master].
The repositories key is only read on the root composer.json.
As explained on the docs:
Repositories are not resolved recursively. You can only add them to your main composer.json. Repository declarations of dependencies' composer.jsons are ignored.
Resolving repositories recursively would be problematic. If multiple packages declared different repositories that included the same package, how would composer know which to use?
Also, this would open the door for dependencies hijacking your system by providing alternate repositories for known packages, and you'd end up installing untrusted packages in your system.
If you are installing multiple packages from a custom repositories, all the custom repositories need to be declared on the root configuration file.
I'm trying to develop a PHP library (called foo/bar) using Composer in dir /work/a with the composer.json contents:
{
"name": "foo/bar",
"require": {
"php": ">=7.2"
}
}
/work/a is a git project and I'm on the branch mybranch
I'm trying to use this lib in another project locally (called testing/foobar) using Composer in dir work/b with the composer.json contents:
{
"name": "testing/foobar",
"type": "project",
"repositories": [
{
"type": "vcs",
"url": "/work/a"
}
],
"require": {
"php": "^7.4",
"foo/bar": "mybranch"
}
}
When running $ composer install in /work/b I get the error:
[UnexpectedValueException]
Could not parse version constraint mybranch: Invalid version string "mybranch"
You have to prefix your branch name with dev-, so your branch name will have to be dev-mybranch.
Loading a package from a VCS repository
...
In composer.json, you should prefix your custom branch name with "dev-".
...
Also check this Q/A "Composer require branch name".
Change branch name to have dev- prefix, add it to /work/b project:
{
"name": "testing/foobar",
"type": "project",
"repositories": [
{
"type": "vcs",
"url": "/work/a"
}
],
"require": {
"php": "^7.4",
"foo/bar": "dev-mybranch"
}
}
Run composer install:
❯ composer install
Loading composer repositories with package information
Updating dependencies (including require-dev)
Package operations: 1 install, 0 updates, 0 removals
- Installing foo/bar (dev-mybranch 85c97b7): Cloning 85c97b7b23 from cache
Writing lock file
Generating autoload files
I have two Symfony projects: project-a (root project) and project-b.
My composer.json file from project-a contains:
{
"name": "myprojects/project-a",
"require": {
"myprojects/project-b": "dev-master",
},
"repositories": [
{
"type": "vcs",
"url": "git#bitbucket.org:MYPROJECTS/project-b.git"
}
]
}
And my composer.json from project-b:
{
"name": "myprojects/project-b",
"require": {
"guzzlehttp/guzzle": "^6.3"
}
}
I need to update project-b but when I execute the command composer update myprojects/project-b from project-a, I get this composer error:
[Composer\DependencyResolver\SolverProblemsException]
Problem 1
Installation request for myprojects/project-b dev-master -> satisfiable by
myprojects/project-b[dev-master].
myprojects/project-b dev-master requires guzzlehttp/guzzle ^6.3 -> no matching package found.
Potential causes:
A typo in the package name
The package is not available in a stable-enough version according to your minimum-stability setting
I have tried solve this adding "minimum-stability": "dev" property to both composer.json files, but it doesn't works.. How can I solve this?
Thanks.
As commented in guzzle/guzzle issue 861:
Looks like an issue with your caching.
Try clearing your composer cache, and self-update composer, then try again.
The OP Wildchild confirms in the comments:
composer clear cache and self-update solves my problem
"minimum-stability": "dev" is not needed.
I am in the process in developing my first package. The package is working.
Now I want to use Laravels Collective's HTML in my package (dependencies).
Therefor I added this to the package's composer file:
(this composer.json is in the root of the package: /packages/vendor/package)
{
"name": "vendor/package",
"description": "",
"license": "MIT",
"authors": [
{
"name": "firstname lastname",
"email": "info#domain.nl"
}
],
"minimum-stability": "stable",
"require": {
"twbs/bootstrap": "dev-master",
"laravelcollective/html": "5.3.*"
}
}
I've included the package in Laravel's own composer.json:
...
"autoload": {
"classmap": [
"database"
],
"psr-4": {
"App\\": "app/",
"Vendor\\Package\\": "packages/vendor/package/src"
}
},
...
When I do composer update the dependencies of the package are not updated. What am I missing here?
You have to define a local composer repository if your package is not uploaded to packagist.org yet. Inside Laravels' application composer.json add a local repository like this:
"repositories": [
{
"type": "path",
"url": "/full/or/relative/path/to/development/my-package"
}
],
"require": {
"my-package": "*"
}
You have to define the package src directory inside the package composer.json and not inside the Laravels' own composer.json. Try and define it like this inside packages' composer.json:
"require" : {
"twbs/bootstrap": "dev-master",
"laravelcollective/html": "5.3.*"
},
"autoload" : {
"psr-4" : {
"PackageNamespace\\PackageClass\\" : "src/"
}
},
UPDATE
In order to do a proper package development, you have to keep your package files at some directory other than /vendor/ composer directory. The main reason is that the developer can delete the entire /vendor directory if there might be conflict problems or for cleaning up and setting up everything under composer /vendor path. I use a very simple method and I keep my own packages at:
<application>/dev/packages/<package-namespace>/<package-name>
You have to initialize a git at your package for composer to recognize the repository. To make a git package, go at your package location and run the following commands:
cd <application>/dev/packages/<package-namespace>/<package-name>
git init
git add *
git commit -m "Initial commit"
You may also have to set the git config user.name and git config user.email before commit, for the git to be able to recognize someone and allow the local commits:
git config user.email "you#example.com"
git config user.name "Your Name"
In my example, the namespace is lytr and the package name is testpkg.
The <application>/dev/packages/<package-namespace>/<package-name>/composer.json (<application>/dev/packages/lytr/testpkg) will look like this:
{
"name" : "lytr/testpkg",
"description" : "Test package of Lytr",
"keywords" : [
"test",
"package"
],
"license" : "MIT",
"require" : {
"twbs/bootstrap" : "dev-master",
"laravelcollective/html" : "5.3.*"
},
"autoload" : {
"psr-0" : {
"Lytr\\TestPkg\\" : "src"
}
},
"extra" : {
"branch-alias" : {
"dev-master" : "1.0-dev"
}
},
"minimum-stability" : "dev"
}
Then at your application <application>/composer.json you'll have a local git repository and your package like this:
"repositories": [
{
"type": "path",
"url": "<full-application-path>/dev/packages/lytr/testpkg"
}
],
"require" : {
"lytr/testpkg": "*"
},
"minimum-stability": "dev",
I include "minimum-stability": "dev", because we are using master-dev versions. Then after running the composer update command only having the "twbs/bootstrap" : "dev-master", at the package requirements, we'll see the following output on the console window:
And after we change the package composer.json and require the "laravelcollective/html" : "5.3.*",, we do a composer update and we see composer installs the laravelcollective/html package properly:
I know this might look confusing and somehow overkill, but this is a proper way for developing packages for composer. You can also have your packages at a git repository and make composer clone that repository instead of your local files. When you're done developing the package and you release it under https://packagist.org/, then you will just have to require your package as any other normal package without the repositories and all the local git thing. Remember, you are at development phase of your package and not at production.
I have been trying to create a simple composer package. But I'm stuck with the following issue. Dont know how to resolve this.
[InvalidArgumentException]
Could not find package sarav/sample-package at any version for your minimum-stability (stable). Check the package spelling or your minimum-stability
Package Url : https://packagist.org/packages/sarav/sample-package
I ran the following composer command
composer require sarav/sample-package
My composer contents
{
"name": "sarav/sample-package",
"description": "Sample package",
"type": "Library",
"license": "MIT",
"authors": [
{
"name": "Sarav",
"email": "me#sarav.co"
}
],
"minimum-stability": "dev",
"require": {
"php": ">=5.3.0"
},
"autoload": {
"psr-0": {
"Sarav": "src/"
}
}
}
Your package config looks good to me, but your Git repo doesn't have any tagged versions.
Use git tag v1.0.0 or whatever version is appropriate, then git push origin --tags to update on GitHub.
Alternatively, you can go without tagged versions by specifying the master branch when you require the package, like so:
composer require sarav/sample-package dev-master
You can require any branch in this manner, but the dev- prefix is mandatory.