Magento 2 CLI Commands Every Developer Should Know

Magento 2 CLI Commands Every Developer Should Know

If you are a Magento developer, we are sure that you are aware of Magento 2 commands or Magento 2 CLI commands, as you might use them to increase your efficiency in performing actions on different files without spending time finding and clicking on individual files.

However, we have noticed that developers and merchants sometimes find it difficult to search for a particular command.

That’s why we have written this blog, so you can refer to it.

Now, let’s dive deep into the topic.

Understanding Magento 2 CLI

A Command-Line Interface (CLI) is a text-based tool that allows users to interact with applications or systems through typed commands.

Think of it like the controls of a car—steering wheels, pedals, and dashboards—that help you navigate, control speed, and adjust settings.

Similarly, the Magento 2 CLI enables you to manage your Magento store, configure settings, and execute tasks using command-line inputs.

With Magento command list, you can perform essential operations such as installing the platform, managing configurations, clearing caches, and more—all through a single interface.

For instance, clearing the cache with the Magento 2 CLI is akin to cleaning a car windshield.

Just as clearing dirt and debris improves visibility while driving, using the cache:clean command removes outdated cached data, ensuring you can instantly see the updates made to your Magento 2 site.

Magento 2 CLI streamlines numerous tasks, making store management more efficient and accessible, even if you’re not a developer.

Magento CLI Commands Every Developer Should Know

Adobe Commerce provides a unified command-line interface (CLI) located at {magento_root}/bin/magento, designed to streamline installation and configuration tasks.

With this CLI, you can perform essential operations such as:

  • Installing Adobe Commerce and handling related tasks, like updating the database schema and creating deployment configurations.
  • Clearing the cache for optimized performance.
  • Managing indexes, including reindexing for data updates.
  • Creating translation dictionaries and packages.
  • Generating classes like factories and interceptors for plug-ins, along with dependency injection configurations for the object manager.
  • Deploying static view files for a seamless user experience.
  • Compiling CSS files from Less code.

Key Benefits

  • A single command ({magento_root}/bin/magento list) provides a complete list of available installation and configuration options.
  • A consistent, user-friendly interface built on the Symfony framework.
  • Extensibility for third-party developers, enabling seamless integration and eliminating steep learning curves.
  • Commands for disabled modules are automatically hidden to reduce clutter.

This guide focuses on configuring Magento/Adobe Commerce using the CLI.

For details about installation, refer to the Installation Flow section in the Installation Guide.

Prerequisites

Before using the Magento 2 commands, ensure that:

  1. Your system meets the criteria outlined in the System Requirements section of the Installation Guide.
  2. All prerequisite tasks described in the Prerequisites section of the Installation Guide are completed.
  3. After logging into the Commerce server, switch to a user account with the necessary permissions to modify the Commerce file system. For guidance, see the Switch to the File System Owner section in the Installation Guide.

Suggested Read: Why Does Your Magento Store Need Regular Maintenance?

Running Commands

To execute commands in the bash shell, use the following syntax to switch to the file system owner and run a command simultaneously:

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

If the file system owner account does not permit logins, use this alternative:

sudo -u <file system owner> <command>

Running CLI Commands from Any Directory

To enable running CLI commands regardless of your current directory, add {magento_root}/bin to your system’s PATH.

Example for a bash shell on CentOS:

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

List of Common Commands of Magento 2

Command Description
magento cache:{enable/disable/clean/flush/status} Manages the cache
magento indexer:{status/show-mode/set-mode/reindex/info/reset/show-dimensions-mode/set-dimensions-mode} Manages the indexers
magento cron:run Runs Commerce cron jobs
magento setup:di:compile Compiles all non-existent proxies and factories; and pre-compiles class definitions, inheritance information, and plug-in definitions for one store and website.
magento info:dependencies:{show-modules/show-modules-circular/show-framework} Module dependencies, circular dependencies, and Commerce framework dependencies.
magento i18n:{collect-phrases/pack/uninstall} Creates a translation dictionary or a translation package
magento setup:static-content:deploy Deploys static view files
magento dev:source-theme:deploy Creates CSS from LESS
magento dev:tests:run Runs automated tests
magento dev:xml:convert Update your layout XML files to match the new Extensible Stylesheet Language Transformations (XSLT) stylesheet
magento setup:perf:generate-fixtures Generate data to use for performance testing.
magento sampledata:install Installs optional sample data after you install the Commerce application.

For more details about sample data, see Optional sample data.

magento config:{set/sensitive:set/show/} Manages backend configurations
magento admin:user:{create/unlock} Creates/edits/unlocks admin users.
magento dev:template-hints:{enable/disable} Enables/disables developer template hints.

Common Arguments

All Magento commands require the following arguments. You can run these commands either before or after installing Commerce:

Long version Short version Description
–help -h Get help for any command. For example, ./magento help setup:install or ./magento help setup:config:set.
–quiet -q Quiet mode; no output.
–no-interaction -n No interactive questions.
–verbose=1,2,3 -v, -vv, -vvv Verbosity level. For example, –verbose=3 or -vvv displays debug verbosity, which is the most verbose output. Default is –verbose=1 or -v.
–version -V Display this application version
–ansi n/a Force ANSI output
–no-ansi n/a Disable ANSI output

Resolving Magento 2 Command Issues

If your Magento 2 commands are not functioning as expected, there could be several underlying causes.

Below are some troubleshooting steps to help you resolve the issue:

1. No Output When Running Commands

If running a command produces no response, consider these fixes:

Tip: Check the log files to identify the specific error causing the issue.

a. Verify File Permissions

Ensure that the files and directories in your Magento 2 installation have appropriate permissions.

Use this command to set the correct permissions: sudo chmod -R 777 var/ pub/

b. Enable Error Reporting

Turn on error reporting in Magento 2 to diagnose issues effectively. To do this, edit the index.php file in your Magento 2 root directory:

Locate the following line: #ini_set(‘display_errors’, 1);

Uncomment it by removing the # so it looks like this: ini_set(‘display_errors’, 1);

2. Memory Limit Issues

If you see an error like “Allowed memory size of x bytes exhausted,” you likely need to increase the PHP memory limit.

Add the -d memory_limit=XX option when running the command, like this: php -d memory_limit=4G bin/magento setup:di:compile

3. Update Magento 2

Ensure your Magento 2 installation is up to date by running: composer update

Read More: Essential Benefits of Magento Customization

4. Check for Errors

To detect errors in your installation, use the following command: php bin/magento setup:di:compile

If any errors are reported, resolve them before retrying the commands.

5. Confirm Command Syntax

Double-check the syntax of the commands you are using. Refer to the Magento 2 documentation for accurate syntax.

Additional Help

If these steps do not resolve your issue, consider reaching out to the Magento community or technical support for further assistance.

That’s a Wrap!

While the Magento CLI might seem complex at first, mastering its basics can empower anyone—not just developers—to effectively manage a Magento store.

This blog aimed to provide clear, practical guidance on creating Magento CLI commands.

We tried our best to cover all the essential Magento 2 commands, so now you can bookmark this blog.

Leave a Reply

Your email address will not be published. Required fields are marked *