How to force composer to reinstall a library?

Stefan Izdrail

Founder & Senior Architect · 2026-06-29

Laravel Company
Title: Forcing Composer to Reinstall Libraries for Development Purposes Introduction: Composer is an immensely useful tool for managing dependencies in your PHP projects. By relying on third-party libraries, you can build complex applications with ease. However, if you're a developer who likes tinkering with library code during development to better understand how things work, you might need to force Composer to reinstall a specific framework or library without altering its commit history. In this comprehensive guide, we will walk through the process of forcing a Composer dependency installation, keeping in mind the importance of maintaining proper version control while learning from your experiments with source code. 1. Understanding Gitignore and Version Control: As mentioned earlier, using .gitignore to prevent external libraries from committing to git is a common practice among developers who are aware of the advantages. This helps avoid accidental or unnecessary changes that might disrupt the overall project workflow. By excluding these folders and files in your .gitignore, you can ensure that only the required code and configuration gets committed to the repository, maintaining its cleanliness and stability. 2. Understanding Composer and Dependencies: Composer is a package manager for PHP that handles dependency management by installing third-party libraries and packages into your project. The composer.json file contains all necessary information about your application's dependencies, such as the name of the library and its version. When you update your code or experiment with library files, it might lead to conflicts if you commit these changes. 3. Forcing Composer to Reinstall a Library: To ensure that your project is in a stable state when pushing your code to the repository, follow these steps to force Composer to reinstall a specific dependency: Step 1: Remove the composer.lock file (if it exists): The composer.lock file stores all the hashes of the installed dependencies that are required for ensuring consistency across environments and platforms. Since you want a fresh copy of your library, removing this file will allow Composer to recalculate its dependencies during installation. Step 2: Remove the vendor directory (if it exists): The composer.lock file is typically generated in the vendor folder, which holds all your installed libraries and frameworks. Removing this entire vendor directory ensures that there are no residual files or conflicts with previous installations. Step 3: Update the Composer configuration to force reinstallation: Open the composer.json file using a text editor like Vim or a code editor such as Visual Studio Code. Look for the repository URL of your desired library and add 'force-install' after it, separated by a comma. This indicates that Composer should always install the next version of this library, regardless of how it was previously installed or if there were any conflicts. Example: "require": { "mylibrary/awesome-package": "~1.0", "force-install": "true" }, Step 4: Run Composer Update: After modifying your composer.json file, run the following command to update your project dependencies and install the library again: composer update --optimize-autoloader This command will reinstall all your dependencies, including the newly updated ones with the 'force-install' setting. Step 5: Commit Changes: Now that you have successfully forced Composer to reinstall your library, commit the changes and push them to your repository as you normally would. Ensure that the .gitignore file is still in place so that external libraries will not be included in future commits. Conclusion: Forcing Composer to reinstall a specific dependency can be a valuable technique for learning how a library works, especially during development. By following these steps and adhering to best practices like maintaining version control and keeping your .gitignore file intact, you can ensure that your project stays stable while still exploring new concepts and libraries.