Adding multiple Composer packages - composer-php

I'm using Composer to install multiple packages using the following syntax:
{
"require": {
"aws/aws-sdk-php": "2.*",
"vimeo/vimeo-api": "1.1.*",
"phpoffice/phpexcel": "dev-master"
}
}
The above works just fine, but now I'd like to add tcpdf via composer. I found this code here but am not sure how to integrate with my current requires. One thing that I tried was to just add it to the end, but I fear that it started deleting my current packages.
{
"name": "tecnick.com/tcpdf",
"version": "6.2.11",
"homepage": "http://www.tcpdf.org/",
"type": "library",
"description": "TCPDF is a PHP class for generating PDF documents and barcodes.",
"keywords": [
"PDF",
"tcpdf",
"PDFD32000-2008",
"qrcode",
"datamatrix",
"pdf417",
"barcodes"
],
"license": "LGPLv3",
"authors": [
{
"name": "Nicola Asuni",
"email": "info#tecnick.com",
"homepage": "http://nicolaasuni.tecnick.com"
}
],
"require": {
"php": ">=5.3.0"
},
"autoload": {
"classmap": [
"fonts",
"config",
"include",
"tcpdf.php",
"tcpdf_parser.php",
"tcpdf_import.php",
"tcpdf_barcodes_1d.php",
"tcpdf_barcodes_2d.php",
"include/tcpdf_colors.php",
"include/tcpdf_filters.php",
"include/tcpdf_font_data.php",
"include/tcpdf_fonts.php",
"include/tcpdf_images.php",
"include/tcpdf_static.php",
"include/barcodes/datamatrix.php",
"include/barcodes/pdf417.php",
"include/barcodes/qrcode.php"
]
}

You can require many packages from the command line, for example:
composer require barryvdh/laravel-debugbar barryvdh/laravel-snappy fideloper/proxy
And all the packages will be required according to your composer specifications.

As a matter of fact, you can list all the packages separated by space, like so:
composer require aws/aws-sdk-php vimeo/vimeo-api phpoffice/phpexcel
Quote:
If you do not want to choose requirements interactively, you can pass
them to the command
From Composer documentation
Also consider --update-with-all-dependencies to update the dependencies of all newly installed packages.

If anyone else comes here and wants to know how to add "multiple" packages, simply use the Composer require command and run multiple CLI commands by terminating them with a semi-colon, e.g.
composer require drupal/pathauto;
composer require 'drupal/google_analytics:^3.0';
composer require 'doctrine/doctrine-bundle:2.*';
composer require 'monolog/monolog:~2.0.0';
Alternatively run with the --no-update flag to disable automatic update of dependencies and run all the updates together — composer will resolve dependencies in one hit:
composer require drupal/pathauto --no-update;
composer require 'drupal/google_analytics:^3.0' --no-update;
composer require 'doctrine/doctrine-bundle:2.*' --no-update;
composer require 'monolog/monolog:~2.0.0' --no-update;
composer update;
If you don't specify a version then composer will automatically pull the latest release. It's worth reading up on Composer versions and constraints, especially when it comes to updating packages. Check the Composer require command for more useful flags.
It can be useful to keep package requirements on separate lines as above, e.g. if you have a reference document of regularly installed packages, or if the commands are being generated by a build tool.
Alternatively you can run them all on one line:
composer require drupal/pathauto 'drupal/google_analytics:^3.0' 'doctrine/doctrine-bundle:2.*' 'monolog/monolog:~2.0.0';
N.B. Terminating commands with a semi-colon is a general solution for running multiple CLI commands, and not just composer specific, e.g.
composer self-update;
composer require 'drupal/google_analytics:^3.0';
cd app/build;
yarn run build;

To add "tecnick.com/tcpdf" to an existing composer.json file, on the commandline inside the directory containing it run:
composer require tecnick.com/tcpdf
You shouldn't have to manually edit the composer.json file for such things.

If you want to add handful of packages without having to wait tediously for composer to update after each and every one, but you prefer not to:
change a long series of modules together in a single line (poor readability, less convenient to reuse)
use a semi-colon delimited list of commands (featherbelly's answer - easy to insert a typo or accidentally paste a carriage return and screw it up midway through)
…then use the --no-update switch to have composer modify the composer.json file ONLY, without searching for packages.
You can run as few or as many as you want, use the CLI to do something else midway through, and when you're ready, just do composer-update by itself.
Here's an example of adding some Drupal modules for a new project:
composer require 'drupal/field_permissions:^1.0' --no-update
composer require 'drupal/coffee:^1.0' --no-update
composer require 'drupal/token:^1.5' --no-update
composer require 'drupal/field_tools:^1.0' --no-update
composer require 'drupal/required_by_role:^1.0' --no-update
composer require 'drupal/devel:^2.1' --no-update
composer require 'drupal/config_ignore:^2.1' --no-update
composer require 'drupal/ga_login:^1.0' --no-update
composer require 'drupal/tfa:^1.0' --no-update
composer require 'drupal/spambot:^1.0' --no-update
composer require 'drupal/pathauto:^1.4' --no-update
composer require 'drupal/flag:^4.0' --no-update
composer require 'drupal/stringoverrides:1.x-dev' --no-update
composer require 'drupal/structure_sync:^1.16' --no-update
composer require 'drupal/masquerade:^2.0' --no-update
composer require 'drupal/metatag:^1.8' --no-update
composer require 'drupal/unique_field_ajax:^1.2' --no-update
composer require 'drupal/config_override_warn:^1.2' --no-update
composer require 'drupal/environment_indicator:^3.6' --no-update
composer require 'drupal/role_delegation:^1.0' --no-update
composer require 'drupal/seo_checklist:^4.1' --no-update
composer update
NB: If you know what the modules you need and are typing your list manually anyway, just edit composer.json by hand - there's no point manually typing "composer require" over and over again.
However, the above method is handy for sites like Drupal where you're copying and pasting pre-written commands that contain complex version syntax for branches, individual commits etc.

Related

Composer install VS composer install --dev

Is there any differences between "composer install" and "composer install --dev"?
I ran these two commands and get the same packages installed.
As you can read in the documentation, leaving it out and using --dev performs the same action:
--dev: Install packages listed in require-dev (this is the default behavior).

Intervention Image - Could not open input file: composer.phar

When I install the php composer.phar require intervention/image this,
I got the one error:
Could not open input file: composer.phar
I follow this step but I can't find the solution to my error: Intervention Image
To install the most recent version, run the following command
composer require intervention/image
OR
Add to composer.json file:
"require": {
"intervention/image": "^2.5",
},
After, Run this command in the command prompt: composer update
More info: intervention/image
use just
composer require intervention/image
instead of
php composer.phar require intervention/image
If you added composer to your system environment variables. you can call composer globally like this:
composer require intervention/image
How to add composer to system environment variables
Search "Edit the system environment variables" in start menu.
Click on "Environment variables"
Find "PATH" variable and edit it.
Add your composer installation folder path. C:\ProgramData\ComposerSetup\bin
Find your composer installation folder
You can find where composer is installed by this command:
which composer

How do I upgrade a composer package after having constrained the version?

Suppose that for stability reasons, I've constrained the version of a particular package in composer.json:
"require": {
"foo/bar": "~3.2.6",
}
I am now ready to upgrade foo/bar to version 4.
What is the correct workflow to do this?
Should I edit the composer.json file by hand and run one of composer install or update? Or should I do composer require foo/bar 4?
composer require vendor/package:version
like :
composer require foo/bar:^4.0.0

Removing a package from composer with --no-update

I'm trying to remove a package from my application and I've run composer remove --no-update package/name, now all this does is remove the entry in composer.json but when I run composer show -i, I see the package is still installed, is there anyway to completely remove a package without running update?
I think this is exactly what the --no-update option is intended to do.
composer remove package/name should remove package/name from your project without updating other packages than package/name.
It's the same if you remove the entry from your composer.json and then run composer update package/name. It will update this specific package but no others!
If you activate the --no-update option it omits the update, so it only removes the entry.
I think what the --no-update option is intended to do is, this will avoid composer update.
for eg composer require "magento/magento-cloud-metapackage":">=2.3.3 <2.3.4" --no-update only update composer.json file not composer.lock file
while composer require "magento/magento-cloud-metapackage":">=2.3.3 <2.3.4"
updates both composer.json and composer.lock file

How can I resolve "Your requirements could not be resolved to an installable set of packages" error?

When I run composer update I receive some wired output.
Here is my composer.json look like.
{
"name": "laravel/laravel",
"description": "The Laravel Framework.", "keywords": ["framework", "laravel"],
"license": "MIT",
"repositories": [{
"type": "vcs",
"url": "https://github.com/Zizaco/ardent.git"
}],
"require-dev": {
"phpunit/phpunit": "4.3.*"
},
"require": {
"laravel/framework": "4.2.*",
"laravelbook/ardent": "dev-master as 2.4.0",
"zizaco/entrust": "dev-master",
"sebklaus/profiler": "dev-master",
"doctrine/dbal": "dev-master"
},
"autoload": {
"classmap": [
"app/commands",
"app/controllers",
"app/models",
"app/database/migrations", "app/database/seeds", "app/tests",
"app/libraries"
]
},
"scripts": {
"post-install-cmd": [
"php artisan clear-compiled",
"php artisan optimize"
],
"post-update-cmd": [
"php artisan clear-compiled",
"php artisan optimize"
],
"post-create-project-cmd": [
"php artisan key:generate"
]
},
"config": {
"preferred-install": "dist"
},
"minimum-stability": "stable"
}
How do I fix that ?
Run this command:
composer install --ignore-platform-reqs
or
composer update --ignore-platform-reqs
Composer ignore-platform-reqs flag explanation
Your software dependencies have an incompatible version conflict.
At the same time you want to install any Laravel 4.2.x version, and "zizaco/entrust" from its master branch. And that master branch requires at least Laravel 5.0 (roughly speaking).
The problem comes from the dependency on branches. It's likely that the package zizaco/entrust once was using Laravel 4.2 in its master branch, and that you were able to install your dependencies at that day. But the very moment this branch gets updated with an incompatible version requirement, you will never ever be able to run composer update and get updated dependencies.
Always use tagged versions! Ideally you use a relaxed version requirement that allows for compatible updates. This should be expressed as a tilde-two-number version requirement: ~1.2 would install a version 1.2.0 and up (like 1.2.99 or 1.2.100), and also 1.3 and up. If you need a certain patch release: Caret-three-number version ^1.2.10 will install 1.2.10 or up, also 1.3 and up.
Using this version requirement instead of dev-master will allow you to use released versions instead of the unstable state in the master branch, and allows you to address the most recent version that still works with Laravel 4.2. I guess that would be zizaco/entrust version 1.3.0, but version 1.2 would also qualify. Go with "zizaco/entrust": "~1.2".
I am facing the same issue. I am using 'Lumen' microservice framework.
I recently resolved the same issue by installing two packages:-
sudo apt-get install php7.0-mbstring,
sudo apt-get install php7.0-xml or sudo apt-get install php-xml
After installing this, you need to execute this command:-
composer update
Hope, it will resolve the issue. I work on my system.
The simplest solution is adding --ignore-platform-reqs flag.
If you are running composer install or composer update use it with --ignore-platform-reqs flag
Example
composer install --ignore-platform-reqs
Or
composer update --ignore-platform-reqs
And this should do the trick!
I use Windows 10 machine working with PHP 8 and Lavarel 8 and I got the same error, I used the following command :-
composer update --ignore-platform-reqs
to update all the packages regardless of the version conflicts.
Were those dev-masters added automatically? Avoid them as unnecessary version constraints, for 'any suitable version' use "*", or "#dev" if you don't mind dev packages. My guess is that Entrust is the potential troublemaker.
Also, "minimum-stability": "stable" imposes additional constraints, and
"minimum-stability": "dev",
"prefer-stable": true
is more conflict-free, consider it a rule of thumb.
I solved the same issue setting 'laravel/framework' dependency version from "^8.0" to "^7.0".
After that running composer update --ignore-platform-reqs simply worked
composer.json
I am facing the same issue in Laravel v8.49.0 (PHP v8.0.6). Using Composer through install packages
I recently resolved the same issue by installing two packages:-
composer create-project laravel/laravel myapp
Composer Update
composer update --ignore-platform-reqs
OR
composer install --ignore-platform-reqs
Check Start Server
php artisan serve
You must be in the correct directory so cd into it, then:
composer update --ignore-platform-reqs if you have previous installed composer as Vivek M suggests. My problem was wrong directory.
cd into: xampp/htdocs/laravelProjects/laravelP1
Add "barryvdh/laravel-cors": "^0.7.3" at the end of require array inside composer.json
Save composer.json and run composer update
You are done !
I solved the same error, by adding "zizaco/entrust": "*" instead of the "zizaco/entrust": "~1.2".
Install the following according to the PHP version installed on your system:
sudo apt-get install php8.0-curl php8.0-gd php8.0-xsl php8.0-dom
Finally try again to create the laravel project with composer
composer create-project laravel/laravel myProject
I encountered this problem in Laravel 5.8, what I did was to do composer require for each library and all where installed correctly.
Like so:
instead of adding it to the composer.json file or specifying a version:
composer require msurguy/honeypot: dev-master
I instead did without specifying any version:
composer require msurguy/honeypot
I hope it helps, thanks
"config": {
"platform": {
"ext-pcntl": "7.2",
"ext-posix": "7.2"
}
}
If you are using php ^8.0
open list of available php versions
sudo update-alternatives --config php
switch to on of the older versions above PHP 7.2, select one of them
then update composer
composer update
CAUSE:
The error is happening because your project folder is owned by the root user.
SOLUTION
Change ownership to the currently signed in user and not the root user. If you only have root as the sole user, create another user with root privileges.
$ sudo chown -R current_user /my/project/directory/
then
$ composer install
Just activate the curl in the php.ini file
;extension=php_curl.dll
to
extension=php_curl.dll
and then composer install

Resources