Fixing magento issues

[SOLVED] Please re-run magento compile command. Use the command “setup:di:compile

Pinterest LinkedIn Tumblr

Sometimes when working with Magento 2, you will see the message Please re-run Magento compile command. Use the command “setup:di:compile”.

please re-run magento compile command use the command setup di compile

There are some scenarios where you will see this message in Magento 2 CLI

In this tutorial, I will explain every about this command and how to properly execute compile command for your Magento 2 website

What is Magento 2 compilation

Magento 2 compile command will compile Factories, Proxies, Interceptors(plugins) classes into magento2root/generated folder and create a single include path to increase Magento 2 website’s performance. In short, this function make your site more stable and less likely to crash. In addition, the compilation process also improves your Magento 2 website’s load speed by 30%-50%.

This is an example of when you run the command properly. The command interface will show the progress of each class compilation and the estimated time to finish every compilation.

Please re-run magento compile command

You don’t need to run compile command in developer mode. Magento 2 automatically generate class when its is in developer mode.

How to run magento compile command

If you have ssh access to your server where you host your Magento 2 website, then it should be easy to run Magento Compile command right in your ssh interface.

However, there’re many users who are running Magento 2 website on shared hosting and don’t have ssh access. Thus, I will give 2 methods to run Magento 2 compile command for those who have ssh access and those who don’t.

Run compile command with ssh access

Connect to your Magento 2 server using ssh protocol and type the following command to start compilation process.

php bin/magento setup:di:compile

Sample output

compilation was started

The compilation process will last for around 8 minutes for a fresh Magento 2 store and time will increase according to the number of Magento 2 extensions installed on your website.

When the compilation process is done, you will see the following message in CLI interface

C:\xampp\htdocs\magento2>php bin/magento setup:di:compile
Compilation was started.
App action list generation... 8/8 [============================] 100% 4 mins 352.0 MiB
Generated code and dependency injection configuration successfully.

Remember to put php before bin/magento setup:di:compile so that you can run the command from any folder.

Run compile command without ssh access

In case you are running Magento 2 on shared hosting without ssh access, we will need to create a PHP file to execute the command by using system() function.

Go to your Magento 2 root folder and create a PHP file and name it: compile.php.

Next, use a text editor and put the following code inside this PHP file

<?php
echo "<pre>";
system('php bin/magento setup:di:compile');
echo "</pre>";

Save changes and type URL to this file in your browser like this: localhost/compile.php

Now wait for the compilation process, when it’s done the webpage will print out the result like this:

compilation using php file

HOW TO RUN SETUP:DI:COMPILE COMMAND FOR SPECIFIC MODULE

The compilation process will happen in a specific amount of time depending on the number of modules installed on your website.

So, in case you have lots of modules but you just want to compile only 1 module, how to do that?

First, go to magento2rootfolder\setup\src\Magento\Setup\Console\Command, look forDiCompileCommand.phpinside this folder

dicompilecommand php

edit DiCompileCommand.php file with a text editor and find the following line (located in line 148)

$modulePaths = $this->componentRegistrar->getPaths(ComponentRegistrar::MODULE);

edit dicompilecommand php file

Now replace that line with the following code.

Note: remember to backup DiCompileCommand.php before making any change or simply, comment original code to revert this file back after compilation process)

$modulePaths = ['VendorName_ExtensionName' => '/var/www/html/magento2rootfolder/app/code/VendorName/ExtensionName'];

For example, I installed Mageplaza blog extension and I want to only this extension then the code should be

$modulePaths = ['VendorName_ExtensionName' => '/var/www/html/magento2rootfolder/app/code/Mageplaza/magento2blog'];

This is the file after inserting new code

dicompilecommand file after inserting code

Now open Magento 2 CLI and run the compile command for the module we registered in

php bin/magento setup:di:compile

IMPORTANT NOTE: remember to register your module in registration.php file located in your module folder (app/code/[VendorName]/[ModuleName]/registration.php) before executing compile command

Troubleshooting the errors when running Please re-run magento compile command in Magento 2

There may be some error when running compile command in Magento 2. I will give some most common errors and solution for each in this section.

Fatal error: Allowed memory size of 134217728 bytes exhausted

The error happens due to lacking of PHP memory. The error message looks like this when we run the compile command

Repositories code generation... 1/7 [====>-----------------------]  12% 97 secs
43.5 MiB
Fatal error: Allowed memory size of 134215552 bytes exhausted (tried to allocate
 24 bytes) in D:\xampp\htdocs\magento2\vendor\zendframework\zend-code\src\Scan
ner\FileScanner.php on line 36

To fix this error, you can permanently increase PHP memory limit in the php.ini file. Set memory limit to 4G or 5G and restart apache to apply changes.

memory_limit=4G

Or you can temporarily increase php memory limit for compile command using this syntax:

php -dmemory_limit=4G bin/magento setup:di:compile

Class Not_Existing_Class does not exist

The error looks like this is CLI interface. This error happens when you run compile command on older version of Magento 2 (2.2.x, 2.1.x, 2.0.x) on localhost.

The cause of this error is the difference between file path indicator in Linux and Windows: “\” and “/”

Class Not_Existing_Class does not exist

To fix this error, go to magento2rootfolder/setup/src/Magento/Setup/Module/Di/Code/Reader/ClassesScanner.php. Edit this file and find the following line:

if (preg_match($pattern, $fileItem->getRealPath())) {

Replace it with:

if (preg_match($pattern, str_replace('\\', '/', $fileItem->getRealPath()))) {

This error is fixed in the newer versions of Magento 2 (Magento 2.3x, Magento 2.4.x)

InvalidArgumentException Plugin class Vendor

The error looks like this

  [InvalidArgumentException]                                     
  Plugin class Vendor\Module\Plugin\plugin_name doesn't exist

This error happens due to a specific plugin on your Magento 2 website. You can resolve this issue by disabling the plugin

php bin/magento module:disable plugin_name

Then rerun compile code and flush cache => The error should be fixed

php bin/magento setup:di:compile
php bin/magento cache:clean
php bin/magento cache:flush

Wrapping up

Compilation is an important process in Magento 2. If you are working in developer mode then you don’t need to run Magento 2 compile code, because the compilation process automatically happens in developer mode. So my advice is: You should switch your store to developer mode before making changes to Magento 2 store such as install/upgrade theme/extensions.

If you have any problem when running compile command in Magento 2, please drop a comment below describing your issue. I will be happy to help!

Hung Tran is the main editor of magentip.com, he loves to write about everything related to Magento 2

Write A Comment