Change vital parts of your e-shop: how to install extension in Magento 2 store
- Eugen Barilyuk
- Magento 2
- Jun 15, 2021
- 16 min read
The default Magento 2 offers great functionality for any e-shop, but to provide true personalization for your business needs, you must also use third-party extensions. By using such modules, you can optimize your shop’s speed, increase sales and revenue, retain customers, improve safety and much, much more.
Modularity of Magento 2 is its core feature, which allows you to change the vital parts of your eCommerce store, which suggests that any Magento 2 store owner should learn how to install, update, activate and deactivate extensions in his or her e-shop.
This article elaborates in detail where to get a Magento 2 module, how to install it, and how to manage it after installation.
Prerequisites
A best practice is to install and test an extension in a development environment before deploying it on the production shop. Magento installations hosted on Commerce Cloud require an extension to be installed in a Magento development environment before being pushed to production. Please note that this article doesn’t include the specifics of the upgrade for Magento hosted on Commerce Cloud.
Check PHP version
Verify that the PHP version installed on your server meets the requirements, which are listed on Magento DevDocs System requirements page.
In your Magento installation folder, execute the command php -v
and make note of your PHP version.
If the PHP version is compatible with your Magento version, you can execute commands starting from bin/magento
.
When the php -v
command shows an error or incompatible PHP version, then you need to explicitly call the PHP interpreter when running Magento commands. Without the explicit call, you may receive a PHP Parse error: syntax error, unexpected ')' when executing command bin/magento
.
To make an explicit interpreter call, it is often sufficient to add phpX.X
to before any command bin/magento
. Replace phpX.X with the installed PHP version, for example, php 7.4.
Sometimes to make an explicit call, it is necessary to specify the full path to PHP installation folder.
Consult with your system administrator to get details on the installation peculiarities on your server. If possible, ask the administrator to configure the server so that the interpreter call php
was linked to the compatible PHP version.
In this article, the installed version of PHP is 7.3, so the commands are executed as php7.3 bin/magento
.
Magento backup
Before installing extensions and updates on the production shop, it is strongly recommended that you make a backup.
When trying to execute the built-in Magento backup command php7.3 bin/magento setup:backup --code --media –db
, you will receive a fail message:
Magento depreciated built-in tools for making backups since version 2.3.0, and advises merchants to investigate third-party backup tools.
Manual Magento backup
As a temporary solution you can make a manual Magento backup. Simply navigate to the Magento installation folder and copy its files and media to a backup disk.
Using command line navigate to your Magento folder and execute command cat app/etc/env.php
to access the file with database connection information. Take a note of parameters for host, dbname, username, password.
Make a backup of your Magento store database using command line interface:
mysqldump -u root -p database_name > database_name.sql
When connecting to the database on a remote server, use the command mysqldump --host=name -u root -p database_name > database_name.sql
For example, your database name is warmer 241, then the backup command will be mysqldump -u root -p warmer241 > warmer241.sql
. The backup file warmer241.sql will be located in current directory.
Copy the SQL file to the backup disk. Once you have the database backup SQL file, you can restore the database with a command:
mysql -u root -p -D database_name < database_name.sql
For the full mysqldump reference, refer to the manual of your relational database management system. For MariaDB management system, read the documentation on mysqldump.
Depending on the size of your store and data volume, the backup and restoration processes may take a some time.
Enable maintenance mode
During the upgrade your site stays accessible to customers. This can cause problems for users who try to access the component which is being upgraded, so it is advised that you activate maintenance mode. Read the full information on maintenance mode on Magento DevDocs.
The maintenance mode is helpful while you are maintaining, upgrading, or reconfiguring your site.
Enable the Magento maintenance mode with the command:
php7.3 bin/magento maintenance:enable
To disable maintenance mode, execute:
php7.3 bin/magento maintenance:disable
During the time of maintenance, the Magento store will display the maintenance page to all visitors. To be accessible for an administrator, make an exemption for the particular IP address with the command:
php7.3 bin/magento maintenance:enable [--ip=<ip address> ... --ip=<ip address>] | [ip=none]
The list of exempted IP-addresses will be saved for later use. To clear it, execute php7.3 bin/magento maintenance:enable --ip=none
.
You can create a custom maintenance page that is more aesthetically pleasant than the default maintenance page. Refer to the Magento DevDocs.
Where to get a Magento extension
Magento supports extending its functionality with several types of third-party components, and most common among them are three types of extensions:
Modules which extend Magento capabilities and customize its behavior
Themes which change the look and feel of pages that are visible to your customers and admin panel
Language packages which help to localize your store to a particular language
Search and purchase any of those types in such places as:
Magento marketplace. It is an official website for the Magento ecosystem, where Adobe reviews extensions before being published.
Packagist. A most common method of Magento extension installation is via the software tool named Composer, and Packagist is its default repository. You can check out the free extensions here.
Magento developer store. An official Mirasvit website is a store for Magento 2 and Magento 1 extensions.
Downloading extension on Magento Marketplace
To get an extension from Magento marketplace you’ll need to create an account for it first. Simply fill in all the required information regarding you and your company, and verify the new account by clicking on the link in the e-mail.
After that, return to the Magento marketplace and log in to it if you weren’t automatically logged in.
Find the extension you need and buy it – you’ll need to specify the Magento version your store uses. When finished purchasing, click on your user name and select link My Profile. On the profile page, click on My Purchases to see the purchased extensions.
Locate the newly purchased extension and click on Technical details in the drop-down menu. Write down the Component name and Component version to use later in command line.
Next click, on your user name and navigate to My Profile > My Products > Access Keys. Press Create A New Key Access, set its name and click OK. Save the public and private keys to the file.
Magento marketplace for Magento 2.3.5 and earlier
Earlier Magento versions prior to Magento 2.3.6 offer a built-in tool to download extensions from Magento marketplace which is called the Web Setup Wizard and is available in the Magento admin panel.
The Web Setup Wizard has depreciated since Magento 2.3.6 and was removed following Magento 2.4.0.
Magento strongly recommends using command line interface (CLI) to install extensions for all versions of Magento. Please note that this article does not cover the installation via Web Setup Wizard.
Packagist
Composer is the widely used tool to get the Magento software and packagist is the main Composer repository. It aggregates public packages installable with Composer and allows to search Magento 2 extensions by tag, name, type.
Visit packagist.org and search for an extension. You can use filters Package type and Type. For example, you can specify Package type as magento2-module, and for the type, you can set magento 2 extension.
When the desired extension is found, click on its link and write down the Composer installation command for the chosen extension.
Magento extension developer store
In addition to the Magento marketplace owned by Adobe, the Magento extension developers also provide their own stores. Mirasvit.com is one of such extension stores.
To get the Mirasvit extension, log in to the store and find the extension you need. Add it to cart, and proceed to checkout, where you must specify credit card details and apply discount code.
Hint: Mirasvit offers discounts on new modules, so you'd better sign up for the newsletter to get the propositions.
On the Mirasvit store after placing the order, navigate to My Account > My Account > My Downloadable Products to see the purchased extension.
Before you will be able to download the purchased extension, specify the Production Domain of your store.
The Mirasvit license allows you to use the extension for one Magento installation. Also you can install extension on test/dev hosts. To use the extension on the test/dev/staging domain, please send a request to our support.
After providing the store Production Domain, you will get buttons for the installation via Composer and downloading the extension in a zip-package.
To install the extension via the Composer, copy the provided commands.
How to install Magento extension
How to install extension via Composer
Log in to SSH console of your server by typing in terminal ssh [email protected]
, where login - is the login on the server example.com. Navigate to the root directory of Magento 2 store using command cd /magento
, where /magento - is the Magento installation directory address.
Check the Composer version
Verify that your server has compatible Composer version by executing the command composer --version
.
If your Magento version is below 2.4.2, then the Composer version should be v1, for Magento 2.4.2 and above Composer v1 and v2 are allowed. Install the required Composer version following the guide on getcomposer.org.
Start installation
To install the module in the terminal, navigate to your Magento installation folder and run the command:
composer require <component-name>
Replace the with the information for the particular extension.
How to install extension from Magento marketplace
Be sure that you have chosen the correct version of Magento edition when purchasing the module on Magento marketplace. Versions for the the Commerce on prem (EE) will display an error message when trying to install on Open Source (CE).
For extensions purchased on Magento marketplace, replace the with the component name marketplace provides. For example, the TaxJar Sales Tax Automation extension has the following component name: taxjar/module-taxjar.
Composer can request a username and a password for extension installation. In such case, use keys that are generated in the Magento marketplace account as shown in this article previously. The public key is your username and your private key is your password.
How to install extension from Packagist
In the terminal, just paste the Composer command the Packagist provides. For Mirasvit module BlogMx, the command is:
composer require mirasvit/module-blog
When executing the installation command, Composer may abort the installation due to a mismatch in the specific platform requirements. In such case, it is usually simpler to install the extension manually via the ZIP-package.
Caution: there is a key --ignore-platform-reqs for Composer that can override a mismatch in required components versions. It is not recommended to use this key since incompatible packages can be installed, and that can bring the store to an inoperative state.
How to install Mirasvit extension
In your account on Mirasvit store, click My Account > My Account > My Downloadable Products > View & Download > Install using composer. You will be provided with Installation Instructions. It is a list of two Composer commands, though the may look like three commands.
The first command connects Composer to Mirasvit repository and provides the license information:
composer config repositories.mirasvit-blg2 composer https://LICENSE:[email protected]/LICENSE:LICENSE/
The second command launches the installation of the purchased extension:
composer require mirasvit/module-blog-mx:*
Hint: Mirasvit provides free installation of its extensions so you can simply check the respective option before adding an item to the cart and the extension will be installed for you.
How to install extension via ZIP-file
Manual extension installation using archive with the extension code is done by copying its files to Magento installation folder.
Obtain the extension package
To obtain the zip-package for the extension purchased on Magento marketplace, visit My Profile > My Products > My Purchases. Choose the extension version you want to download and click the Download button.
Identically to get a zip-package from the Magento extension developer store login to your account and locate the purchased modules.
For Mirasvit store, visit My Account > My Account > My Downloadable Products and click on the download link. Specify your Magento version and download the package.
You can obtain the zip-package with the extension code from any place. For example, it can be a developer’s profile on Github.
Check ZIP content
Extract the package into any new folder on your local computer. If after the ZIP package extraction, you see folders like Api, Block, Console, and you don’t see nested folders app/code then you need to create them.
For example, the Mirasvit extension BlogMx is ready to be copied as is because it has nested folders /app/code/Mirasvit/BlogMx/:
The extension TaxJar Sales Tax Automation obtained on Magento marketplace doesn’t have the nested folders and requires them to be created:
For such zip-packages as TaxJar extension, open the module file registration.php and find the text ComponentRegistrar::MODULE. After this text in quotes will be the name of the module: Taxjar_SalesTax.
Create nested folders so that the path looks like /app/code/Taxjar/SalesTax/. Copy the content of the zip-package into the SalesTax folder.
Copy the module files to Magento host
When you have module files with proper nested folders path, you can copy the module to your Magento installation folder.
Upload the module files with the command:
scp -r /local/dir/app user@remotehost:/remote/dir/
The local folder app and its content will be copied to the remote path /remote/dir/.
Using SSH, connect to your Magento host and navigate to the Magento installation folder, and then - to folders /app/code/. Check if the copying was successful:
Finish the installation
Magento extension installation via Composer or a ZIP-file simply copies the files to Magento installation folder. To properly incorporate new module into the Magento, you need to run some commands.
First check that Magento recognized the copied files:
php7.3 bin/magento module:status
You will be listed with all extensions available in your Magento installation. At the list bottom you will see the List of disabled modules. Since Mirasvit_BlogMX also contains its core module Mirasvit_Core, you will see two disabled Mirasvit modules:
Enable each module (replace VendorName_ModuleName with the name of your module) and clear static view files with the respective command:
php7.3 bin/magento module:enable VendorName_ModuleName --clear-static-content
Some extensions for Magento consist of several modules. For example, Mirasvit_BlogMx depends on Mirasvit_Core. You can activate the main module first and then the dependent module executing these two commands one by one:
php7.3 bin/magento module:enable Mirasvit_Core --clear-static-content
php7.3 bin/magento module:enable Mirasvit_BlogMx --clear-static-content
Or you can activate both Mirasvit modules at once using the commands
php7.3 bin/magento module:enable Mirasvit_Core Mirasvit_BlogMx
Verify that the extension is enabled:
php7.3 bin/magento module:status
The extension should be in the List of enabled modules.
To make sure that the enabled modules are properly registered and their tables in the database are created, run
php7.3 bin/magento setup:upgrade
Then recompile your Magento project to generate code and configure dependency injection. You may receive such prompt “Please rerun Magento compile command” when your Magento installation is in Production mode. Magento does not prompt you to run the compile command in Developer mode.
php7.3 bin/magento setup:di:compile
Deploy static view files to Magento public directory. Use the following command to do that:
php7.3 bin/magento setup:static-content:deploy -f
The final step is to clear all cache:
php7.3 bin/magento cache:flush
Now you can return to Magento admin panel and configure the newly installed extension as needed.
How to upgrade a Magento 2 extension
To update or upgrade an extension, first take note of its vendor and module names, and the version number using command to show the list of all extensions installed via Composer:
composer show -i
Then run the Composer command:
composer update vendor-name/module-name
You can encounter a common problem with this command that Composer states “Nothing to install or update.” It can be false positive, and the problem can be solved with manual editing of composer.json file to set the correct versions of required components.
But it can be much simpler to run command:
composer require vendor-name/module-name:new-version-number
If you installed the extension manually by copying the content of the ZIP-package to Magento folder, then simply overwrite any existing files with the new version.
In both cases, run commands to upgrade, deploy, and clean the cache:
php7.3 bin/magento setup:upgrade
php7.3 bin/magento setup:static-content:deploy
php7.3 bin/magento cache:clean
How to uninstall Magento 2 extension
Uninstall modules installed via Composer
To uninstall Magento 2 extension, navigate to your Magento installation directory and disable it:
php7.3 bin/magento module:disable VendorName_ModuleName
Then uninstall the extension:
php7.3 bin/magento module:uninstall VendorName_ModuleName
Replace VendorName_ModuleName with the name of the uninstalled module. The module name can be obtained in the list of installed modules when the command php7.3 bin/magento module:status
is executed.
The terminal asks if you are sure of removing code and/or database tables. It also will warn you that you are about to remove a module that might have database data. Answer yes for both questions and wait for the process to finish.
During the module deinstallation process Magento will be automatically set to maintenance mode. After uninstall the maintenance mode will be automatically disabled.
Uninstall modules installed via ZIP-package
If you did not install the module via Composer, the command php7.3 bin/magento module:uninstall
will not work.
For modules installed via ZIP-package, you can simply disable it with the command php7.3 bin/magento module:disable VendorName_ModuleName
.
If you need to remove the module installed via ZIP-package, then after disabling it, remove its folder in app/code, and remove its records in the database tables. Specifically remove the module record from the table setup_module.
At the end run command php7.3 bin/magento setup:upgrade
.
Summary
Built-in functionality of Magento 2 can be greatly extended by using third-party extensions. They can modify frontend and backend parts of your store so that it perfectly suited your vision and business needs.
Some Magento extension stores, like Mirasvit.com, offer free installation service. Still you can get value to you module from a source that doesn’t provide such service.
Using this tutorial, you will be able to manipulate extensions in your store. Install the extensions by yourself with two methods - via Composer tool and manually via the ZIP-package. Update and uninstall extensions installed with each of these methods.
And remember to manipulate extensions safely by making backup copy of Magento files and database.