Code compiler

Overview of code compilation

This section discusses the basics of code compilation.

Code compilation consists of all of the following in no particular order:

  • Application code generation (factories, proxies, and so on)
  • Area configuration aggregation (that is, optimized dependency injection configurations per area)
  • Interceptor generation (that is, optimized code generation of interceptors)
  • Interception cache generation
  • Repositories code generation (that is, generated code for APIs)
  • Service data attributes generation (that is, generated extension classes for data objects)

You can find code compilation in classes in the \Magento\Setup\Module\Di\App\Task\Operation namespace.

In this release, the Magento software doesn't support the multi-tenant compiler (that is, the magento setup:di:compile-multi-tenant command).

First steps

  1. Log in to the Magento server as, or switch to, a user who has permissions to write to the Magento file system. One way to do this is to switch to the Magento file system owner.

    If you use the bash shell, you can also use the following syntax to switch to the Magento file system owner and enter the command at the same time:

    su <Magento file system owner> -s /bin/bash -c <command>
    

    If the Magento file system owner does not allow logins you can do the following:

    sudo -u <Magento file system owner>  <command>
    
  2. To run Magento commands from any directory, add <your Magento install dir>/bin to your system PATH.

    Because shells have differing syntax, consult a reference like unix.stackexchange.com.

    bash shell example for CentOS:

    export PATH=$PATH:/var/www/html/magento2/bin
    

You can also run the commands in the following ways:

  • cd <your Magento install dir>/bin and run them as ./magento <command name>
  • php <your Magento install dir>/bin/magento <command name>

<your Magento install dir> is a subdirectory of your web server's docroot. Need help locating the docroot? Click here.

In addition to the command arguments discussed here, see Common arguments.

Run the single-tenant compiler

Run the command as follows (there are no options):

magento setup:di:compile

The following message displays to confirm success:

Generated code and dependency injection configuration successfully.

Run the code compiler

Command options:

magento setup:di:compile [--serializer="{serialize|igbinary}"] [--extra-classes-file="<path>"] [--generation="<path and
filename>"] [--di="<path and filename>"] [--exclude-pattern="<regex>"]

The following table discusses the meanings of this command’s parameters and values.

Parameter Value Required?

--serializer

Specify either serialize or igbinary. Default is serialize.

No

--extra-classes-file

Specify the absolute file system path to proxies and factories that are not declared in the dependency injection or code..

No

--generation

Absolute file system path to a directory for generated classes. Default is <your Magento install dir>/generated/code

No

--di

Absolute file system path to a directory to generate the object manager configuration. Default is <your Magento install dir>/generated/metadata

No

--exclude-pattern

Regular expression that enables you to exclude paths from compilation. Default is #[\\/]m1[\\/]#i)

No

For example, to run the compiler and specify the igbinary serializer:

magento setup:di:compile --serializer=igbinary

Messages similar to the following display:

Generated classes:
    Magento\Rss\Controller\Adminhtml\Feed\Interceptor
    Magento\Quote\Model\Quote\Config\Interceptor
    Magento\Checkout\Block\Cart\Shipping\Interceptor
    Magento\Framework\View\Layout\Interceptor
    Magento\Integration\Service\V1\Integration\Interceptor
    Magento\Catalog\Block\Product\Compare\ListCompare\Interceptor
    Magento\Framework\View\TemplateEngineFactory\Interceptor
    Magento\Catalog\Model\Product\Attribute\Backend\Price\Interceptor
    Magento\Catalog\Api\ProductRepositoryInterface\Interceptor
    Magento\Catalog\Model\Product\Interceptor
    Magento\Quote\Model\Quote\Item\ToOrderItem\Interceptor
    Magento\Catalog\Controller\Adminhtml\Product\Initialization\Helper\Interceptor
    Magento\Catalog\Model\Product\CartConfiguration\Interceptor
    Magento\Catalog\Model\Product\TypeTransitionManager\Interceptor
    Magento\Catalog\Model\Product\Type\Interceptor
    ... more messages ...
    On *nix systems, verify the Magento application has permissions to modify files created by the compiler in the "var" directory.
    For instance, if you run the Magento application using Apache, the owner of the files in the "var" directory should be the Apache user
    (example command: "chown -R www-data:www-data <MAGENTO_ROOT>/var" where MAGENTO_ROOT is the Magento root directory).

Optional. Compile code before installing the Magento application

In some cases, you might want to compile code before you install the Magento application. To do that, you must first enable modules; otherwise, the compiler has nothing to do. To compile code for only some modules, enable only those modules.

The following command enables all modules:

php bin/magento module:enable --all [-c|--clear-static-content]

Use the optional [-c|--clear-static-content] option to clear static content. This is necessary if you’ve previously enabled or disabled modules and you must clear static content previously generated for them.

More information about enabling modules.

Compile code

Use this command to compile code.

Run the command as follows (there are no options):

magento setup:di:compile

The following message displays to confirm success:

Generated code and dependency injection configuration successfully.

Code compiling without a database

Refer to Deploy static view files without installing Magento for more information.