How to force composer to reinstall a library?

PhpZend Framework2Composer Php

Php Problem Overview


I'm using the ZF2 skeleton app and it has a .gitignore that prevents external libraries from being commited to git. While debugging I like to go and change stuff here and there in the libraries' source to learn how things work. If these were version controlled it would be very easy to revert them back to their original state.

How can I force Composer to reinstall a particular framework so that I can get a fresh -unmodified- copy again?

PS: Please don't suggest removing the .gitignore file since it's there for a reason; it prevents my third party libraries from getting into my app's repository. I can always install them during an automated deployment.

Php Solutions


Solution 1 - Php

First execute composer clearcache

Then clear your vendors folder

rm -rf vendor/*

or better yet just remove the specific module which makes problems to avoid having to download all over again.

Solution 2 - Php

You can use the --prefer-source flag for composer to checkout external packages with the VCS information (if any available). You can simply revert to the original state. Also if you issue the composer update command composer will detect any changes you made locally and ask if you want to discard them.

Your .gitignore file is related to your root project (ZF2 skeleton) and it prevents the vendor dir (where your third party libs are) from committing to your own VCS. The ignore file is unrelated to the git repo's of your vendors.

Solution 3 - Php

I didn't want to delete all the packages in vendor/ directory, so here is how I did it:

  1. rm -rf vendor/package-i-messed-up
  2. composer install again

Solution 4 - Php

What I did:

  1. Deleted that particular library's folder
  2. composer update --prefer-source vendor/library-name

It fetches the library again along with it's git repo

Solution 5 - Php

Reinstall the dependencies. Remove the vendor folder (manually) or via rm command (if you are in the project folder, sure) on Linux before:

rm -rf vendor/

composer update -v

https://www.dev-metal.com/composer-problems-try-full-reset/

Solution 6 - Php

Short answer

you can execute it in one cli command with &&:

composer remove vendor/package && composer require vendor/package:version

Detailed answer

Remove existing package by command:

composer remove vendor/package

this will remove folder of package from /vendor, row from composer.json and whole record of package from composer.lock right way with removing not used dependencies and not removing dependencies which used by another packages

Then install preferred one with command:

composer require vendor/package:version

this will install package with desired version right way with adding row to composer.json, adding record to composer.lock and all needed dependent packages if there would be package which is used in more that one package, Composer will try to install version which fits all using packages. If it will not resolve this it will crash with corresponding error message

https://stackoverflow.com/questions/40914114/how-to-install-a-specific-version-of-package-using-composer

https://stackoverflow.com/questions/23126562/how-to-remove-a-package-from-laravel-using-composer

Install, Uninstall and Update Modules Themes etc with Composer: https://modulesunraveled.com/drupal-8-composer-and-configuration-management/installing-and-uninstalling-modules-composer

Solution 7 - Php

The relevant feature request is https://github.com/composer/composer/issues/3112

In 2021-05 the "reinstall" command patch got merged: https://github.com/composer/composer/pull/9915 - it is available in composer version 2.1.0 and all later ones.


The reinstall command is merged and availabe since 2.1.0:

composer reinstall <package-name> # Removes and installs the package.

Solution 8 - Php

As user @aaracrr pointed out in a comment on another answer probably the best answer is to re-require the package with the same version constraint.

ie.

composer require vendor/package

or specifying a version constraint

composer require vendor/package:^1.0.0

Solution 9 - Php

For some reason no one suggested the obvious and the most straight forward way to force re-install:

> composer remove vendor-name/package-name && composer vendor-name/package-name

Be aware that this exact command will install latest version of the package. If you was using old version of the package and package does not have backward compatibility this will brake version compatibility. You might consider backing up your composer.json first.

Solution 10 - Php

Since Composer 2.1 you can do

composer reinstall vendor/package

see https://getcomposer.org/doc/03-cli.md#reinstall

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJulianView Question on Stackoverflow
Solution 1 - PhpZoranView Answer on Stackoverflow
Solution 2 - PhpBram GerritsenView Answer on Stackoverflow
Solution 3 - PhpYerkeView Answer on Stackoverflow
Solution 4 - PhpAttila FulopView Answer on Stackoverflow
Solution 5 - PhpAbdalhady AlsayadView Answer on Stackoverflow
Solution 6 - PhpIlya KolesnikovView Answer on Stackoverflow
Solution 7 - PhpcweiskeView Answer on Stackoverflow
Solution 8 - PhpfrederickjhView Answer on Stackoverflow
Solution 9 - PhpYevgenView Answer on Stackoverflow
Solution 10 - PhpluenemamView Answer on Stackoverflow