How to Upgrade Magento 2 Using the Command Line?

Today, we’re going to teach you guys how to upgrade your Magento 2 store using the command line.

Although, there are numerous ways to upgrade your Magento 2 store to the latest version. But, the Command line is the most straightforward way to upgrade Magento 2 version.

Also read: What Happens If You Don’t Upgrade To Magento 2.x?

This post will show you exactly how to upgrade Magento 2 using a command line.

Step-By-Step Process to Upgrade Magento 2 Via Command Line

Please follow the below steps to learn how to upgrade Magento 2 version via the command line.

Step - 1

First of all, you need to put your Magento 2 store into maintenance mode.

For this, log in to your Magento server and execute the following command:

php <your Magento install dir>/bin/magento maintenance:enable

Step - 2

After that, you need to change the directory where you’ve installed your Magento 2 software by executing the below command:

cd /var/www/html/magento2

Pull Packages

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

composer update

For instance, if you want to upgrade to Magento version 2.4.2, then execute your command as shown below:

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

composer update

Step - 3

Next, we need to enter the authentication keys as shown below.

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/*

Update Database Schema and Data

php bin/magento setup:upgrade

Disable Maintenance Mode

php bin/magento maintenance:disable

Restart Varnish

service varnish restart

Also read: How to Create New Command in Console CLI in Magento 2?

Step - 4

Now, you need to access your storefront and check the results.

If you face the “We’re sorry, an error has occurred while generating this email.” error, perform the below tasks:

  • Log in as a user with root privileges and reset file system ownership and permissions.
  • Clear the below-mentioned directories and try again:
<your Magento install dir>/var/cache

<your Magento install dir>/var/page_cache

<your Magento install dir>/generated/code

Step - 5

Finally, check the version your Magento 2 version again to verify if it’s upgraded or not by executing the below command:

php bin/magento —version

Also read: Useful Magento 2 Command List for Beginners

Conclusion

And that’s about it! This is how you can upgrade Magento 2 version using a command line. We hope that you found this tutorial helpful. However, if you need our professional help with Magento 2 Development Service, please reach out to us at any time.