Magento Tutorials

Upgrade Magento 2 via Command Line

There are various ways to upgrade your Magento 2 website to a higher version. This post is gonna show you how to do that via command line.

You can upgrade Magento from the command line if you installed the software using any of the following:

  • Downloaded the metapackage using composer create-project
  • Installed the compressed archive

Step 1: Put your store in maintenance mode:

To prevent any access during the upgrading process, it’s necessary to set your site to maintenance mode.

Here is the simplest way to enable this mode:

  • Log in to your Magento server as, or switch to, the Magento file system owner.
  • Enter the following command:
php <your Magento install dir>/bin/magento maintenance:enable

Step 2: Upgrade using Command line:

Change to the directory in which you installed the Magento software

For example:

cd /var/www/html/magento2

Pull packages

composer require <product> <version> --no-update
composer update

For example, to upgrade to Magento Open Source version 2.4.2, enter:

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

Enter your authentication keys

Manually clear var subdirectories

rm -rf <Magento install dir>/var/cache/*
rm -rf <Magento install dir>/var/page_cache/*
rm -rf <Magento install dir>/generated/code/*

If you use a cache storage other than file system (e.g., Redis, Memcached, etc.) you need to manually clear the cache there too.

Update the database schema and data

php bin/magento setup:upgrade

Put your storefront online (disable the maintenance mode)

php bin/magento maintenance:disable

Restart Varnish if you use it for page caching.

service varnish restart

Access your storefront.

The following error might display: We’re sorry, an error has occurred while generating this email.
Please perform the following tasks:

<your Magento install dir>/var/cache
<your Magento install dir>/var/page_cache
<your Magento install dir>/generated/code

After the upgrade, again check your Magento version with the following command:

php bin/magento —version
Dom

A knowledge craver who always strive to be wiser everyday.