146

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.

The same applies to Laravel framework: it also gitignores the vendor folder.

PaulH
  • 2,918
  • 2
  • 15
  • 31
Julian
  • 8,808
  • 8
  • 51
  • 90

11 Answers11

218

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.

Black
  • 18,150
  • 39
  • 158
  • 271
Zoran
  • 2,653
  • 2
  • 13
  • 9
  • @Loenix, unlock them. – Halfstop Jan 23 '18 at 20:02
  • 10
    I've had cases where the local cache was corrupted, so even after deleting the vendor directory I kept reinstalling a broken dependency. `composer clearcache` is a good addition in such cases. – okdewit Feb 05 '18 at 09:14
  • 7
    This seems like a very radical approach considering that composer then needs to reinstall all packages when the OP just needs one package reinstalled. `composer require vendor/package` will do what the OP wants and in less time too. I am a bit surprised that so many have up-voted this answer. – frederickjh May 17 '19 at 11:40
  • if composer clearcache doesn't work you can delete the /home/[username]/.cache directory. That'll force a re-download. Useful if you use private composer packagist, in addition to packagist.composer.org, and someone makes changes without adding a new tag. – Neil Davis Aug 29 '19 at 12:30
  • 3
    Composer does not necessarily only install in the vendor directory, so even as a heavy-handed approach it does not work in all situations. – ummdorian Feb 05 '20 at 21:11
  • Clearing and downloading all libraries is very drastic. Since 2021-05 we can use `composer reinstall ` which is faster and only reinstalls the currently installed version. – PaulH Jul 06 '22 at 13:01
  • In cases where autoloading was misconfigured in the required library, you'll also need to delete composer.lock before reinstalling, otherwise the broken autoload-configuration will be kept even if the library's composer.json was updated. – janh Jan 09 '23 at 00:33
56

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.

Bram Gerritsen
  • 7,178
  • 4
  • 35
  • 45
  • 9
    Initially your -prefer-source suggestion didn't work until I realized that I had to remove and reinstall all libraries for this to work as I intended and then `composer status -v` gave me the info with the changes. – Julian Oct 26 '13 at 20:48
  • 3
    That did not work for me, since 2021-05 we can use `composer reinstall ` – PaulH Jul 06 '22 at 11:26
50

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
Yerke
  • 2,071
  • 2
  • 25
  • 23
  • 5
    or 'composer require vendor/package-i-messed-up' would be good too – aarcarr May 24 '18 at 10:33
  • 1
    If that doesn't work, you might run `composer clearcache` first in case the cache got corrupted for some reason. – Sean the Bean Sep 26 '19 at 14:31
  • This is the right way. And to clarify, it does need to be the /vendor/ that is removed for composer to reinstall it from the lock file. If one removes just the directory and leaves the named directory behind e.g. /vendor// then it won't re-install. – Elijah Lynn Jan 27 '21 at 18:03
34

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.
hakre
  • 193,403
  • 52
  • 435
  • 836
cweiske
  • 30,033
  • 14
  • 133
  • 194
24

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

Attila Fulop
  • 6,861
  • 2
  • 44
  • 50
7

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/

Kuya
  • 7,280
  • 4
  • 19
  • 31
7

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

Links

How to install a specific version of package using Composer?

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

Ilya Kolesnikov
  • 623
  • 8
  • 17
  • 2
    Please add some explanation to your answer such that others can learn from it - this does not look like a good solution to me, as it contains the possibility that the dependencies of other packages change – Nico Haase Feb 28 '21 at 10:34
  • I've added explanations as much as possible. These commands are native by Composer and recommended by SO community. If you need some more explanations please write what exactly do you want to see, I'll try to answer – Ilya Kolesnikov Mar 01 '21 at 13:21
  • Is there any good reason not to call `composer install` after removing the package's folder from the vendor directory? This would skip all unneccessary changes in the lock file – Nico Haase Mar 01 '21 at 13:58
  • Removing vendor directory is not a native action, `composer remove` is a command that makes to remove one package, composer require - to install it Editing anything like composer.json, composer.lock, /vendor manually is bad practice Removing whole folder is slow and unnecessary Before you will be able to make `composer install` for new version of package, you'll need to edit composer.json which is bad practice if you do it in "require" section Do you have any proofs that manually removing folders better than reinstalling one package by native composer commands? – Ilya Kolesnikov Mar 01 '21 at 15:18
  • "Proofs"? No. But do you have any proofs that reinstalling does install the very same version of that package that was used before? – Nico Haase Mar 01 '21 at 15:20
  • Proof is not a best word here If you need a specific version of a package you can explicitly indicate it you can do it by specifying tag, branch or even revision number, so if you'll use revision number or tag, it will be exactly same version as it was before. Мy answer is actually better suited for cases where you for some reason need to install a different version of a package in place of the old one. The reason could be when you developing a package or a fork – Ilya Kolesnikov Mar 01 '21 at 15:37
3

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
frederickjh
  • 1,739
  • 1
  • 11
  • 10
  • For me, this only overwrites the changes if there is a new version of the package. Like `composer update` does. – PaulH Jul 06 '22 at 11:14
1

In 2022
You can use composer status to list the libraries you changed.
Then composer reinstall vendor/package to overwrite the changes.

This does not change the version of the installed library like the solutions with composer require or composer install.

PaulH
  • 2,918
  • 2
  • 15
  • 31
0

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 require 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.

Victor S.
  • 2,510
  • 3
  • 22
  • 35
Yevgen
  • 1,239
  • 3
  • 15
  • 30
  • 1
    Please share more details - why should this be an obvious answer? This changes the version dependencies, so it does not look really good to me – Nico Haase Feb 28 '21 at 10:37
  • @NicoHaase it seems obvious because it is built in composer command, but you have a point. I updated answer. – Yevgen Mar 01 '21 at 12:18
  • Why not remove the vendor folder and run `composer install` instead? What's the point in removing and reinstalling the package after all? – Nico Haase Mar 01 '21 at 13:17
  • @NicoHaase I assume that need to re-install one package arises when you are developing package / working on it. In this case you can hardly screwed up with version compatibility. This is why I don't share your worries. Advantages are: 1) Re-installing one package usually faster then reinstalling all of them 2) If I need to make it multiple times I can lose concentration so I don't want to `rm -rf` around when it is not mandatory. – Yevgen Mar 01 '21 at 14:12
0

Since Composer 2.1 you can do

composer reinstall vendor/package

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

luenemam
  • 17
  • 2