How to upgrade Magento 2 with composer

October 28, 2016 - Stefan Wieczorek # Magento 2 Composer Upgrade, Magento 2 Upgrade

Introduction

Ugrading your Magento 2 version is very important to get newest features and security fixes. With newer versions you usually also get better performance from time to time and fixed bugs. Nowadays security is one of the most important topic for every web application but especially for e-commerce websites like Magento 2 because you have sensitive data in your database. Not upgrading Magento 2 means you are an easy target for potential attackers. We love open source but it also comes with some disadvantages. The code of Magento 2 is public available and can be viewed and analysed by everyone. The potential attacker just need to read about known security holes in a specific version in order to develop an exploit.

Preparations

Before you start upgrading your magento 2 version we recommend to follow these steps.

1. Download and install your Magento 2 installation on your local development or on a test environment. It's highly recommended to do the upgrade first on a test environment before you apply the same commands on production.

We recommend our customers to do the upgrade first on our MGT Development Environment which is almost identical to the production environment

2. Create a checklist with all functions and extensions to test. Testing the Magento 2 upgrade is the most time consuming part which can take some days.

3. Create a file system and database backup in case something goes wrong.

To backup the file system you can create a tarball. You can use the following command to create a tarball.

tar cfvhz file_system_backup.tar /home/cloudpanel/htdocs/magento2.mgtcommerce.com/

To backup the database we recommend to use mysqldump.

mysqldump -h127.0.0.1 -umagento2 -p --opt --single-transaction --quick magento2 > magento2_dump.sql

Magento 2 upgrade with composer - the right way

In this example we are going upgrade Magento 2 from version 2.1.1 to 2.1.2.

1. Login into your Magento 2 Server with SSH

ssh your-username@$your-server-ip

2. Navigate to the Magento 2 installation directory

/home/cloudpanel/htdocs/magento2.mgtcommerce.com/

3. By upgrading through composer, you can choose the version to upgrade. In our example we are upgrading to Magento2.1.2: ( Of course it will work also for any higher Magento Version in the future )

composer require magento/product-community-edition 2.1.2 --no-update

4. Run composer update command

composer update

5. Delete all generated di, view processes and cache files

rm -rf var/*

6. When composer update has been finished, upgrade your database by running the Magento 2 CLI command:

php bin/magento setup:upgrade

7. Flush magento cache

php bin/magento cache:flush

8. Deploy static files

php bin/magento setup:static-content:deploy

9. Generate Dependency Injection (DI) files

php bin/magento setup:di:compile

10. Run indexers

php bin/magento indexer:reindex

11. Verify the Magento 2 version upgrade

When you run the Magento 2 CLI you see the new version in the first line

php bin/magento

You also see the Magento 2 version in the admin interface in the bottom right. You should see there the newest version 2.1.2.

Magento 2 Version Upgrade