How can I remove a package from Laravel using PHP Composer?

801,270

Solution 1

Composer 1.x and 2.x

Running the following command will remove the package from vendor (or wherever you install packages), composer.json and composer.lock. Change vendor/package appropriately.

composer remove vendor/package

Obviously you'll need to remove references to that package within your app.

I'm currently running the following version of Composer:

Composer version 1.0-dev (7b13507dd4d3b93578af7d83fbf8be0ca686f4b5) 2014-12-11 21:52:29

Documentation

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

Updates

  • 26/10/2020 - Updated answer to assert command works for v1.x and v2.x of Composer

Solution 2

I got it working... The steps to remove a package from Laravel are:

  1. Remove the declaration from file composer.json (in the "require" section)
  2. **Remove Service Provider from file config/app.php (reference in the "providers" array)
  3. Remove any class aliases from file config/app.php
  4. Remove any references to the package from your code :-)
  5. Run composer update vendor/package-name. This will remove the package folder from the vendor folder and will rebuild the Composer autoloading map.
  6. Manually delete the published files (read the comment by zwacky)

It will remove the package folder from the Vendor folder.

Solution 3

Run the following command:

composer remove Vendor/Package Name

That's all. There isn't any need to update Composer. The Vendor/Package Name is just a directory as installed before.

Solution 4

Normally composer remove used like this is enough:

composer remove vendor/package

But if a Composer package is removed and the "config" cache is not cleaned you cannot clean it. When you try like so

php artisan config:clear

you can get an error In ProviderRepository.php line 208:

Class 'Laracasts\Flash\FlashServiceProvider' not found

This is a dead end, unless you go deleting files:

rm bootstrap/cache/config.php

And this is Laravel 5.6 I'm talking about, not some kind of very old stuff.

It happens usually on automated deployment, when you copy files of a new release on top of old cache. Even if you cleared the cache before copying. You end up with an old cache and a new composer.json file.

Solution 5

You can remove any package just by typing the following command in the terminal, and just remove the providers and alias you provided at the time of installing the package, if any and update the composer,

composer remove vendor/your_package_name
composer update
Share:
801,270
igaster
Author by

igaster

Updated on December 13, 2021

Comments

  • igaster
    igaster over 2 years

    What is the correct way to remove a package from Laravel using PHP Composer?

    So far I've tried:

    1. Remove declaration from file composer.json (in the "require" section)
    2. Remove any class aliases from file app.php
    3. Remove any references to the package from my code :-)
    4. Run composer update
    5. Run composer dump-autoload

    None of these options are working! What am I missing?

  • Simon Wicki
    Simon Wicki about 10 years
    although unpublishing assets/configs isn't really an automated thing. i usually delete manually the vendor/package folders in config/ or public/.
  • Chandan Gupta
    Chandan Gupta over 9 years
    now laravel should come up with something like "remove package <package-name>" to make it easier.
  • tremby
    tremby over 8 years
    A problem with this solution is that it will update all other packages, which may not be what you want.
  • zarax
    zarax about 7 years
    @ZeshanKhattak composer update will update all dependencies. It way leave the project in unstable state. Don't use composer update unless you want to update dependencies
  • Admin
    Admin almost 7 years
    composer remove packagename , It updates removing package from composer.json and removes package folder from vendor... laravel - 5.4.24, composer - 1.4.2.
  • JCarlosR
    JCarlosR over 6 years
    (1) and (5) can be replaced with running composer remove vendor/package, I think.
  • Peter Griffin
    Peter Griffin almost 6 years
    This helped me.
  • Shahrukh Anwar
    Shahrukh Anwar almost 6 years
    Sometimes to update the new dependancies, you have to update the composer. I think there is no harm in running that.
  • Olle Härstedt
    Olle Härstedt over 5 years
    It seems like composer is automatically installing missing packages after running composer remove <package>. Anyway to avoid that? Using version 1.7.2.
  • Samrat Khan
    Samrat Khan over 5 years
    Some package vendor does not meet all requirements to create a package. For that reason, these vendors composer remove vendor/package unable to remove the cache entries properly. So, @Ramjith Ap is absolutely correct here.
  • Yevgeniy Afanasyev
    Yevgeniy Afanasyev over 5 years
    I think it is fixed in 5.7
  • Aine
    Aine over 5 years
    With 5.6, my bootstrap/cache was empty. I forgot that I'd changed the cache path (config/cache.php)! Deleting the files in the new cache folder fixed the problem!
  • Shreyansh Panchal
    Shreyansh Panchal about 5 years
    @YevgeniyAfanasyev This is not fixed Even in 5.7
  • Shreyansh Panchal
    Shreyansh Panchal about 5 years
    I almost spent 3 hours pulling my hairs out over this. I thought config:clear would fix this.
  • prashant
    prashant over 4 years
    it should be: composer remove package_name instead: composer remove vendor/package there is no need of vendor as laravel automatically check in vendor directory for packages.
  • vinsa
    vinsa about 4 years
    or composer remove laravel/telescope --dev if it is dev package
  • Gezzasa
    Gezzasa almost 4 years
    If you don't want your packages to be updated after removing a module you can run composer install instead of composer update
  • Jason Aller
    Jason Aller over 3 years
    When adding an answer to six year old question with an accepted answer and fifteen existing answers it is important to point out what new aspect of the question your answer addresses.
  • Chandraarnav
    Chandraarnav over 3 years
    The most important thing I faced is point No.3, which we often ignore as we usually get "nothing to install or update", I was unfortunate while uninstalling certain illuminate component as I had sentinel which uses illuminate capsule. however thankfully update composer command helped here. the answers above misses on the inter-dependencies part, in case you feel its not important juss reply back I will remove my answer.
  • Charles Wood
    Charles Wood about 3 years
    @prashant vendor here is also a variable. For example, if you were removing Carbon, you would run composer remove nesbot/carbon. In this example, nesbot is the name of the vendor.
  • Brian Highforce Thomas
    Brian Highforce Thomas almost 3 years
    I have been trying this but i continuously get an error : ` In ProviderRepository.php line 208: class xxxxxx not found`
  • Nico Haase
    Nico Haase over 2 years
    What is that "package file"? Do you mean composer.lock? You forgot to add the consequences that deleting the lock file cuases, like updating other dependencies
  • Nico Haase
    Nico Haase over 2 years
    Please never recommend that anybody should edit the composer.lock manually. Calling composer remove is enough, as this removes the package from the vendor folder, and from both composer files
  • Nico Haase
    Nico Haase over 2 years
    Calling composer update is not needed if you just want to remove a package
  • Enver
    Enver over 2 years
    For example, you have a package named as spatie. You will find it under vendor file. composer.json. All installed package will be starage under Vendor with package's name.
  • Nico Haase
    Nico Haase over 2 years
    What do you mean by "remove Spatie"? That's not a package. Also, composer remove does all this automatically
  • Enver
    Enver over 2 years
    Spatie is an example package name. It's a permission management package for Laravel. I just gave it as an example. @NicoHaase Sometimes composer remove doesn't work and we need to remove it manually. It's another solution to this problem.
  • Nico Haase
    Nico Haase over 2 years
    Please add all clarification to your answer by editing it