Skip to content
advanced Phase 110 · Upgrades

Magento 2 Upgrades via Composer

Performing Magento version upgrades using Composer, including dependency resolution, compatibility checks, and upgrade strategies

1h
2 problems
Topic Progress 0%

Composer Upgrade Basics

Magento Upgrade Flow

Current: Magento 2.4.5-p1
Target: Magento 2.4.6-p2

Flow:
1. Check requirements
2. Update composer.json
3. Run composer update
4. Run setup/upgrade commands
5. Verify and test

Core Commands

Pre-Upgrade Check

# Check current version
php bin/magento --version

# Check available updates
composer show --available magento/product-community-edition

# Show what would change
composer update --dry-run

Running the Upgrade

# Full upgrade process
composer require magento/product-community-edition=2.4.6-p2 --no-update
composer update

# Or upgrade all Magento packages
ccomposer update 'magento/*'

# With specific stability
composer require magento/product-community-edition=2.4.6-p2 --prefer-stable

Post-Upgrade Commands

# Clear cache
php bin/magento cache:clean
php bin/magento cache:flush

# Upgrade database schema and data
php bin/magento setup:upgrade

# Recompile code
php bin/magento setup:di:compile

# Deploy static content
php bin/magento setup:static-content:deploy -f

# Restart services
sudo systemctl restart php-fpm
sudo systemctl restart nginx

Version Constraints

// composer.json version constraints
{
    "require": {
        "magento/product-community-edition": "2.4.6-p2",
        "magento/framework": "103.0.6-p2",
        "php": ">=8.1"
    }
}

// Using range constraints
"magento/module-catalog": ">=103.0.0 <104.0.0"

// Using wildcard
"magento/module-*": "2.4.6"

Dependency Resolution

Common Dependency Conflicts

Conflict Types

1. Version Constraint Mismatch
   - Package A requires php >=8.1
   - Package B requires php >=7.4 <8.0

2. Magento Version Conflict
   - Module requires 2.4.5
   - Upgrading to 2.4.6 breaks it

3. Transitive Dependency Conflict
   - Direct dep OK
   - Dependency of dependency conflicts

Diagnosing Conflicts

# See why resolution failed
composer update --verbose

# Show dependency tree
composer why magento/framework

# Show reverse dependencies
composer why-not magento/framework:103.0.6

# Check specific package conflicts
composer why vendor/package

# Debug resolution
composer update --debug

Resolution Strategies

Strategy 1: Update Conflicting Package

# Find what needs updating
composer why vendor/package

# Update specific package
composer update vendor/package

# Or update with version constraint
composer require vendor/package:^2.0

Strategy 2: Use --with-all-dependencies

# Update Magento plus all dependencies
composer update magento/* --with-all-dependencies

Strategy 3: Exclude Problematic Package

// composer.json
{
    "require": {
        "magento/product-community-edition": "2.4.6-p2"
    },
    "replace": {
        "magento/module-shipping": "*"
    }
}

Strategy 4: Fork and Fix

# Create fork
git clone https://github.com/vendor/package vendor-fork/package

# Fix compatibility
# Update composer.json in fork

# Use fork in project
composer require vendor-fork/package:@dev

Using Composer Plugins

# Enable helpful plugins
composer require --dev johncongdon/composer-dependency-analyzer

# Analyze dependencies
vendor/bin/deps

# Find unused dependencies
composer unused

Compatibility Verification

Pre-Upgrade Compatibility

Environment Check Script

#!/bin/bash
echo "=== Magento Upgrade Compatibility Check ==="

# PHP version
echo "PHP Version:"
php --version

# Required extensions
php -m | grep -E 'curl|gd|intl|mbstring|pdo_mysql|xml|xsl|zip|bcmath|soap|redis|ioncube' | sort

# Composer version
echo "Composer:"
composer --version

# Elasticsearch/OpenSearch
curl -s localhost:9200 | grep version

# MySQL version
mysql -V

# Available disk space
df -h . | tail -1

Module Compatibility Matrix

// Check each module for compatibility
$requiredModules = [
    'magento/module-catalog' => '>=103.0.0',
    'magento/module-sales' => '>=103.0.0',
    'magento/module-checkout' => '>=103.0.0',
];

foreach ($requiredModules as $module => $version) {
    $installed = shell_exec("composer show $module --format=json");
    $data = json_decode($installed, true);
    $currentVersion = $data['versions'][0] ?? 'not installed';
    echo "$module: $currentVersion\n";
}

API Deprecation Check

# Find deprecated API usage
grep -r '@deprecated' app/code/Vendor/ --include='*.php'

# Find deprecated class usage
rg 'use.*\\Deprecated\\' app/code/ --include='*.php'

# Check for removed APIs
rg 'Magento\\Framework\\App\\ObjectManager' app/code/ --include='*.php'

Post-Upgrade Verification

# Test critical paths
php bin/magento module:status

# Run unit tests
vendor/bin/phpunit tests/unit/

# Run integration tests
vendor/bin/phpunit tests/integration/

# Check for errors
php bin/magento setup:di:compile 2>&1 | grep -i error

# Verify static content
grep -r 'Syntax error' pub/static/ 2>/dev/null

# Performance baseline
php bin/magento setup:performance:generate-fixtures Setup/Performance/Profiles/Fixtures/Small.xml

Handling Upgrade Failures

Common Failure Scenarios

Composer Update Fails

# Solution: Check composer.lock exists
rm composer.lock
composer update

# Or clear composer cache
composer clear-cache
composer update

Setup:di:compile Fails

# Check for compilation errors
php bin/magento setup:di:compile 2>&1 | head -50

# Common fix: remove generated code
rm -rf generated/code/*
rm -rf generated/metadata/*
php bin/magento setup:di:compile

Setup:upgrade Fails

# Check database connection
grep -A5 'db' app/etc/env.php

# Check for failed patches
php bin/magento setup:db:status

# Skip specific patches (emergency only)
php bin/magento setup:upgrade --skip-config-flags

Static Content Deploy Fails

# Check symlinks
ls -la pub/static/

# Re-deploy with verbose output
php bin/magento setup:static-content:deploy -f --verbose

# Check file permissions
chown -R www-data:www-data pub/static/
chmod -R 775 pub/static/

Rollback Procedure

# 1. Stop web traffic (maintenance mode)
php bin/magento maintenance:enable

# 2. Restore code
git checkout <previous-tag>

# 3. Restore composer.lock
git checkout composer.lock
composer install

# 4. Restore database
mysql -u root -p magento < backup.sql

# 5. Clear caches
php bin/magento cache:flush

# 6. Recompile if needed
php bin/magento setup:di:compile
php bin/magento setup:static-content:deploy -f

# 7. Disable maintenance mode
php bin/magento maintenance:disable

Post-Failure Checklist

- [ ] Identify root cause
- [ ] Document failure in runbook
- [ ] Create test to prevent recurrence
- [ ] Update upgrade documentation
- [ ] Notify stakeholders of resolution

Practice Problems

0 / 2 solved
Dependency Conflict Resolution

Resolve a composer dependency conflict between a third-party module and Magento 2.4.6 upgrade.

Upgrade Recovery

A Magento upgrade failed at setup:di:compile. Diagnose and recover the system.

Quiz

1. What command clears all Magento caches?

Question 1 options

2. What does --no-update flag do in composer require?

Question 2 options

3. What command shows why a specific package cannot be installed?

Question 3 options

4. What is the first step when an upgrade fails in production?

Question 4 options

Flashcards

Question

How to upgrade Magento via Composer?

Answer

composer require magento/product-community-edition=2.4.6-p2 && composer update

Question

Post-upgrade commands?

Answer

cache:clean, cache:flush, setup:upgrade, setup:di:compile, setup:static-content:deploy

Question

How to diagnose dependency conflicts?

Answer

composer why, composer why-not, composer update --verbose

Question

What does composer replace do?

Answer

Prevents a package from being installed, useful for excluding problematic modules

Question

How to rollback a failed upgrade?

Answer

Maintenance mode, git checkout previous, restore DB, composer install, recompile

Revision Notes

Key Takeaways

  • 1. Always run composer update --dry-run before actual upgrade
  • 2. Post-upgrade: clean, flush, upgrade, compile, deploy static content
  • 3. Use composer why/why-not to diagnose dependency conflicts
  • 4. Keep maintenance mode during critical upgrade steps
  • 5. Test every upgrade in staging before production
  • 6. Always have a rollback plan and backup before starting

Interview Tips

  • Walk through a Magento upgrade step by step
  • How do you resolve a composer dependency conflict?
  • What commands must be run after a Magento upgrade?
  • Describe your rollback procedure for a failed upgrade
  • How do you test compatibility before upgrading?

Cheat Sheet

Composer Upgrades Cheat Sheet

Upgrade Flow:

  1. composer require product-edition=version --no-update
  2. composer update
  3. cache:clean && cache:flush
  4. setup:upgrade
  5. setup:di:compile
  6. setup:static-content:deploy -f

Diagnostics:

  • composer why
  • composer why-not
  • composer update --dry-run
  • composer update --verbose

Failure Recovery:

  1. Maintenance mode
  2. Restore code (git)
  3. Restore DB (backup)
  4. composer install
  5. Recompile
  6. Disable maintenance