Upgrading Magento Extensions Via Composer

June 23, 2022
June 23, 2022
Patrick Connor

About

There are many reasons to update Magento extensions: new features, bug fixes, and security updates to name a few. Updating an extension with composer is an easy process once we understand how composer works. We will look at how Composer treats version constraints, how to modify them and finally how to update the extension.

Composer Versioning

One of the primary purposes of Composer with Magento is to make sure that the packages that you install do not get updated unexpectedly. To understand this we need to understand a little about versioning in Composer. Composer uses what are called versioning constraints. We will talk about two of them in this article. More can be found in the Composer documentation

Exact Version Constraint

You can specify the exact version of a package. This will tell Composer to install this exact version only, i.e. 1.3.4.

Caret Version Range

The caret (^) symbol can be used to allow upgrading to anything but the next major version. For example, ^1.3.4 will allow the extension to be updated up to, but not including, version 2.0.0. This works well with extensions that respect semantic versioning, as nothing before version 2.0.0 should break backward compatibility.

Gather Information

Now that we understand Composer versioning, let’s walk through an example of updating an extension.

Note: All commands in this guide should be run from the Magento root directory, i.e. /home/jetrails/example.com/html.

In order to update an extension, we will want to obtain the full name of the extension in the format vendor/extension-name. We will also need to obtain the version number that we want to upgrade to, i.e. 1.2.3. If you already know the name, you can have composer list the most recent version available with composer show -l vendor/extension-name, or all available versions with composer show -a vendor/extension-name.

If the extension is in the Magento Marketplace, you can find the extension name, version number as well as the change log there. You may also be able to find it at the developer’s web page or on packagist.org.

We will use the Jetrails Cloudflare extension for the rest of this tutorial. In this case, the full package name is jetrails/magento2-cloudflare. We can now find the latest version that is available to install.

You can find more info about installing and using the JetRails Cloudflare extension here.

$ composer show -l jetrails/magento2-cloudflare

name     : jetrails/magento2-cloudflare
descrip. : Interact with popular Cloudflare features though Magento's backend portal
keywords :
versions : * 1.3.4
latest   : 1.3.9
type     : magento2-module
......

This will display lots of information about the installed extension, including the current version number which is 1.3.4 in our case. Also the latest version to install is 1.3.9.

Update extension

Composer uses two files, composer.json and composer.lock, to track and constrain dependency versions. These two files contain a line that identifies the extension and what version should be installed.

The below command will update the composer.json file with the new version number constraint, trigger the composer update command, and then update the composer.lock file upon successful completion.

$ composer require jetrails/magento2-cloudflare:1.3.9

./composer.json has been updated
Running composer update jetrails/magento2-cloudflare
Loading composer repositories with package information
In Laminas\DependencyPlugin\DependencyRewriterV2::onPrePoolCreate
Updating dependencies
Lock file operations: 0 installs, 1 update, 0 removals
  - Upgrading jetrails/magento2-cloudflare (1.3.4 => 1.3.9)
Writing lock file
Installing dependencies from lock file (including require-dev)
Package operations: 0 installs, 1 update, 0 removals
  - Upgrading jetrails/magento2-cloudflare (1.3.4 => 1.3.8): Extracting archive
Generating autoload files
123 packages you are using are looking for funding.
Use the `composer fund` command to find out more!
PHP CodeSniffer Config installed_paths set to ../../magento/magento-coding-standard,../../phpcompatibility/php-compatibility

We can see that the update completed successfully and our new version is installed. We got some warnings about packages that are abandoned that are unrelated to updating our extension and we can carry on.

If we want to allow composer to update the extension to any minor version with the composer update command, then we can prepend the ^ symbol to the version constraint. This will allow updates up to but not including version 2.0.0:

$ composer require jetrails/magento2-cloudflare:"^1.3.9"

All that is left is to propagate the changes via Magento:

$ php bin/magento setup:upgrade
$ php bin/magento setup:di:compile
$ php bin/magento cache:flush

Update extension manually

The version constraint can be set by modifying the composer.json. However, there is usually no benefit to doing it the manual way, and this also has the potential for a typo or some other mistake. To update an extension manually, we recommend first creating a backup of composer.json before editing, just in case.

$ cp -p composer.json composer.json.bak

Open the composer.json file with your text editor, then find the line with the extension you are updating:

{
    "require": {
        "jetrails/magento2-cloudflare": "1.3.4"
    }
}

You can now change the version to the new desired version:

{
    "require": {
        "jetrails/magento2-cloudflare": "1.3.9"
    }
}

Now we can tell composer to update the extension.

$ composer update jetrails/magento2-cloudflare

Loading composer repositories with package information
In Laminas\DependencyPlugin\DependencyRewriterV2::onPrePoolCreate
Updating dependencies
Nothing to modify in lock file
Installing dependencies from lock file (including require-dev)
Package operations: 0 installs, 1 update, 0 removals
  - Upgrading jetrails/magento2-cloudflare (1.3.4 => 1.3.9): Extracting archive
...

Finally, we will propagate the changes via Magento:

$ php bin/magento setup:upgrade
$ php bin/magento setup:di:compile
$ php bin/magento cache:flush

Final Thoughts

Updating an extension for Magento using Composer is not difficult, but there can be a few intricacies for those that are new to the process or do use the tool infrequently. Once you understand versioning constraints and the update command, running updates on 3rd party extensions becomes easy.