How to upgrade Magento 2 to the latest version
Magento 2 is a living project that receives updates which range from small patches to major releases. Besides new features and fixing broken features, a Magento 2 update also brings one or more security enhancements. These are important to get as soon as possible in order to guarantee you and your customer a safe experience.
As of writing this, the latest Magento 2 version is 2.3.5. To get started with updating, the first thing you should do is turning on maintenance mode on your webshop. This will notify customers that a planned maintenance is happening and will prevent your customers from seeing a broken webshop.
If you want to know how to install Magento 2, check out our other article How to install Magento 2 on Ubuntu 18.04 / Windows 10
Prerequisites
Before we actually start modifying important files, we need to back them up first.
Enable maintenance mode
Because we will be handling files that are being used by our Magento installation we need to set the store into maintenance mode. This will prevent our customers from seeing a broken store.
$ bin/magento maintenance:enable
Backing up our composer.json file
We will start with backing up our composer.json. This file holds important information about our Magento 2 installation such as our dependencies.
The following command will create a copy of composer.json and call it composer.json.bak.
$ cp composer.json composer.json.bak
Changing from open source to commerce and vice versa
The following step is optional and only required if you are upgrading from an open source Magento 2 installation to Magento 2 commerce.
$ composer remove magento/product-community-edition --no-update
Next, we need to add the composer dependency for the latest Magento 2 version. In our case, 2.3.5.
# Use this one for the open source edition $ composer require magento/product-community-edition 2.3.5 --no-update # Use this one for the commercial edition $ composer require magento/product-enterprise-edition 2.3.5 --no-update
Note we use the following --no-update
to make sure we do not automatically update all the dependencies. We are not done adding additional packages so to avoid getting errors we use the no-update
parameter.
Download the dependencies
Just like many big software platforms, Magento 2 requires many other dependencies besides the core framework in order to fully work. Let us install these next.
$ composer require --dev phpunit/phpunit:~6.2.0 friendsofphp/php-cs-fixer:~2.10.1 lusitanian/oauth:~0.8.10 pdepend/pdepend:2.5.2 sebastian/phpcpd:~3.0.0 squizlabs/php_codesniffer:3.2.2 --no-update
Sometimes during updates certain packages are not needed anymore, we remove these in the next step. If you do not have these packages you can still run the command without issues.
$ composer remove --dev sjparkinson/static-review fabpot/php-cs-fixer --no-update
Update the composer.json file
Now it is time to update our composer.json file manually. We need to add a couple autoload paths. This will make sure that these specific files are guaranteed to get loaded.
"autoload" : { "psr-4": { "Magento\\Framework\\": "lib/internal/Magento/Framework/", "Magento\\Setup\\": "setup/src/Magento/Setup/", "Magento\\": "app/code/Magento/", "Zend\\Mvc\\Controller\\": "setup/src/Zend/Mvc/Controller/" } }
Magento update folder
The following step is optional if the Magento updater is installed. The updater should be located in your /update
First, let us start with making a backup of the update directory
$ cp -R update update.bak
Next, we will create a Composer project, use the first one for the Magento Open Source edition and the second one for the Magento Commerce edition
$ composer create-project --repository=https://repo.magento.com magento/project-community-edition=2.3.5 temp_dir --no-install
$ composer create-project --repository=https://repo.magento.com magento/project-enterprise-edition=2.3.5 temp_dir --no-install
Next, we can remove the old update directory and move our newly downloaded one
$ rm -rf update $ mv temp_dir/update . $ rm -rf temp_dir
Update metadata
The following step is optional as well but recommended to keep a clean working space.
Change the “name”, “version” and “description” fields to match with the new Magento version number. In our case, 2.3.5.
$ nano composer.json
Updating Magento
Now we are finally done setting up for our update. Time to actually update Magento 2.
Run composer update
$ composer update
Clearing the Magento caches
Next, we need to clean the Magento cache
bin/magento cache:clean
Our next step is to clear the var
and generated
subdirectories
$ rm -rf var/cache/* $ rm -rf var/page_cache/* $ rm -rf generated/code/*
Note if you are using a cache storage like Redis or Varnish, clean that cache as well
Updating the Magento database
Update the database schema and data
$ bin/magento setup:upgrade
Wrapping up
We should be about done with the update. The only thing that is left for us to do is to disable maintenance mode and start testing.
$ bin/magento maintenance:disable
If you use Varnish, restart it.
We have updated Magento to the latest version!