search
HomeDevelopment ToolscomposerIntroduction to composer commands: install and update and their differences

composer is a dependency management tool for php. It allows you to declare code libraries that your project depends on and it will install them for you in your project.

Introduction to composer commands: install and update and their differences

However, novices may not know how to "install them".

Some answers on the Internet say composer install, and some say composer update. Both of them seem to be able to successfully download and install dependencies. So what is the difference between them?

Related recommendations: "composer Tutorial"

The first thing to understand is that all dependencies are defined in composer.json , some basic usage and examples are given in the manual. You may have noticed that when specifying a version number, we don't necessarily specify an exact version. Then it may happen that for the same composer.json, the dependency files we pull at different times may be different (because composer will pull the latest dependency when the conditions are met), resulting in some abnormal situation.

composer update and composer install appeared to solve this problem.

When you execute composer update, composer will read the dependencies specified in composer.json, analyze them, and pull the latest version of the dependencies that meet the conditions. Then he will put the pulled dependencies into the vendor directory, and write the precise version numbers of all pulled dependencies into the composer.lock file.

What composer install does is very similar, except for the first step. When you already have a copy of composer.lock locally, it will read your composer.lock instead of composer.json, and use this as a standard to download dependencies. When you don't have composer.lock locally, what it does is actually no different from composer update.

This means that as long as you have a copy of composer.lock locally, you can guarantee that you can pull the same dependencies no matter how long it takes. And if you incorporate it into the version control of your project, then you can ensure that everyone and every computer in your project, no matter what system, can pull the exact same dependencies to reduce potential dependencies. Impact on deployment. Of course, please remember that the command you should use is composer install.

So when should I use composer update? When you modify your dependencies, whether you add a new dependency, modify the dependency version, or delete a dependency, if you execute composer install at this time, there will be no changes, but you You will get a warning message

Warning: The lock file is not up to date with the latest changes in composer.json. You may be getting outdated dependencies. Run update to update them.

Some people may be curious about how php knows that I have modified the dependency, or composer.lock has expired. It's very simple. If you open composer.lock, you will find a hash field in it, which is the hash value of the corresponding dependency at that time. If the values ​​are inconsistent, you will naturally know that a change has occurred.

At this time, you should update your dependencies through composer update.

If you do not want to affect other installed dependencies and only update the parts you modified, you can determine the scope of update by specifying a whitelist. For example, composer update monolog/monolog will only update monolog/ Monlog is a dependency, and other dependencies will be ignored even if they are updated.

The above is the detailed content of Introduction to composer commands: install and update and their differences. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:xia. If there is any infringement, please contact admin@php.cn delete
Using Composer: Simplifying Package Management in PHPUsing Composer: Simplifying Package Management in PHPApr 18, 2025 am 12:01 AM

Composer is a PHP dependency management tool that manages project dependencies through composer.json file. 1. Install Composer: Run several commands and move them to the global path. 2. Configure Composer: Create composer.json file in the project root directory and run composerinstall. 3. Dependency management: Specify the library and its version through composer.json, and use semantic version number control. 4. Use Autoloading: Define the automatic loading rules of the class through the autoload field to simplify development. 5. Package management: Supports private library management, defines the private library address through the repositories field

How to simplify performance monitoring in PHP projects using ComposerHow to simplify performance monitoring in PHP projects using ComposerApr 17, 2025 pm 11:57 PM

When developing PHP projects, we often need to monitor the execution time of the code to optimize performance. In one of my recent projects, I encountered a problem: I needed to do precise timing between different code segments, but manually implementing the timer is not only tedious, but also error-prone. After some exploration, I discovered the library phpunit/php-timer, which is easily integrated through Composer, greatly simplifying my work.

Solve version management troubles: Guide to using phar-io/version librarySolve version management troubles: Guide to using phar-io/version libraryApr 17, 2025 pm 11:54 PM

Version management has always been a difficult issue during development, especially when dealing with package dependencies. Recently, I encountered a problem with version control in my project: I need to accurately manage and compare version information of different packages to ensure that the project can depend on and upgrade correctly. I tried several methods, but the results were not satisfactory. Eventually, I found the library phar-io/version, which solved my problem perfectly.

Easily achieve backward compatibility of PHP8.3 functions with ComposerEasily achieve backward compatibility of PHP8.3 functions with ComposerApr 17, 2025 pm 11:51 PM

When developing a project, I often need to use some features of new versions of PHP, but sometimes I have to use lower versions of PHP due to limitations of the server environment. This causes me to be unable to use some new features, such as json_validate and Override introduced in PHP8.3, etc. To solve this problem, I found Symfony's polyfill-php83 library, which allows me to use these new features in lower versions of PHP.

How to use Composer to easily count PHP code linesHow to use Composer to easily count PHP code linesApr 17, 2025 pm 11:48 PM

When developing a PHP project, counting the number of lines of code is a common requirement, especially when evaluating project size or performing code reviews. However, manual statistics are not only time-consuming and error-prone. Recently, I encountered this requirement in my project. After trying multiple methods, I finally installed the sebastian/lines-of-code library through Composer, which easily solved this problem.

Solve the PHP timeout problem: application of phpunit/php-invoker librarySolve the PHP timeout problem: application of phpunit/php-invoker libraryApr 17, 2025 pm 11:45 PM

When developing PHP projects, you often encounter the problem that some functions or methods have been executed for too long, causing program timeout. I've tried multiple solutions, but the results are not satisfactory until I discovered the phpunit/php-invoker library. This library completely solved my problem by setting the timeout time to call the executable function.

How to solve the problem of file type detection using ComposerHow to solve the problem of file type detection using ComposerApr 17, 2025 pm 11:42 PM

I encountered a tricky problem when developing a file processing system: how to accurately detect the MIME type of a file. Initially, I tried using PHP's built-in functions mime_content_type() and finfo classes, but found that these methods were not stable enough when processing certain special files, causing the system to misjudgment the file type, affecting the user experience. After some exploration, I found the library league/mime-type-detection which brought the perfect solution to my project.

How to easily manage collection data using Composer: Application of ramsey/collection libraryHow to easily manage collection data using Composer: Application of ramsey/collection libraryApr 17, 2025 pm 11:39 PM

In project development, I encountered a common but difficult problem: how to efficiently manage and manipulate collection data. I have tried multiple methods, but I always feel that it is not flexible and efficient enough when dealing with complex data structures. Later, I discovered the ramsey/collection library, which completely changed my development experience.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
Will R.E.P.O. Have Crossplay?
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.