PHP
composer update and install
Learn the difference between the composer install and update commands.
The difference between install and update
The composer install command installs the exact versions recorded in composer.lock; in a team project this ensures everyone gets the same versions. composer update ignores composer.lock, fetches the newest compatible versions and refreshes the lock file.
Use update to add a new package or move versions; use install to keep versions pinned. In a shared project, install is safer.
composer.lock and dump-autoload
When composer require or update runs, the composer.lock file is produced. It lists the exact version of every installed package and its dependencies; it goes into version control.
The composer dump-autoload command only regenerates the autoloader; it does not install or change packages. Run it when you add a new class or change the autoload mapping.
Exercise Install/update behavior simulation
Define a $lock = ["monolog/monolog" => "3.5.0"] and $config = ["monolog/monolog" => "^3.5"] array. An installDependencies(array $lock): array function should return $lock as is. An updateDependencies(array $config): array function should write "3.6.0" for each package via foreach (a simulation of newer versions).
Call both and print in the form install: monolog/monolog=3.5.0 and update: monolog/monolog=3.6.0.
Show example solution
array { return $lock; }. Define function updateDependencies(array $config): array { $result = []; foreach ($config as $pkg => $ver) { $result[$pkg] = "3.6.0"; } return $result; }. Print with $installed = installDependencies($lock); foreach ($installed as $pkg => $ver) { echo "install: " . $pkg . "=" . $ver . "\n"; }. Print with $updated = updateDependencies($config); foreach ($updated as $pkg => $ver) { echo "update: " . $pkg . "=" . $ver . "\n"; }.
Example solution:
<?php
$lock = ["monolog/monolog" => "3.5.0"];
$config = ["monolog/monolog" => "^3.5"];
function installDependencies(array $lock): array {
return $lock;
}
function updateDependencies(array $config): array {
$result = [];
foreach ($config as $pkg => $ver) {
$result[$pkg] = "3.6.0";
}
return $result;
}
$installed = installDependencies($lock);
foreach ($installed as $pkg => $ver) {
echo "install: " . $pkg . "=" . $ver . "\n";
}
$updated = updateDependencies($config);
foreach ($updated as $pkg => $ver) {
echo "update: " . $pkg . "=" . $ver . "\n";
}
Test yourself
-
What does composer install do?
- It ignores the lock file
- It adds a new package
- It installs the versions from the lock file
- It deletes the autoloader
Show answer
It installs the versions from the lock file — Correct. install installs the exact versions from the lock file.
-
Match the command concepts
- composer install
- Installs from the lock
- composer update
- Fetches the newest
- composer.lock
- Exact versions
- dump-autoload
- Builds the autoloader
-
Complete the install command
<?php
// Install the versions from the lock file
$cmd = ["name" => "____"];
echo $cmd["name"] . "\n";
Show answer
<?php
// Install the versions from the lock file
$cmd = ["name" => "composer install"];
echo $cmd["name"] . "\n";
Next lesson: Summary - Composer