Magento 2: Command Line Tool / Interface (CLI)

Magento 2 uses Symfony’s Console Component for its command-line interface or tool (CLI) where we can execute different commands for different tasks related to installation and configuration.

Through the Command Line Interface (CLI) tool in Magento 2, you can perform different tasks like:

  • Installing Magento
  • Clearing the cache
  • Managing indexes, including reindexing
  • Creating translation dictionaries and translation packages
  • Generating non-existent classes such as factories and interceptors for plug-ins
  • Generating the dependency injection configuration for the object manager
  • Deploying static view files
  • Creating CSS from LESS

Read more: Introducing the Magento command-line interface (CLI)

Running/Executing Commands

  • Open terminal.
  • Go to your magento installation directory root.
  • Run the following command. Here we don’t pass any command or argument to the interface.
  • This will list all available installation and configuration commands.

php bin/magento

Note: If it doesn’t work or shows error then you can run the command with sudo:


sudo php bin/magento

Or, you can also pass the list command to the ‘magento’ CLI to list all available commands:


php bin/magento list

You will get the following result when you run the above commands:

Magento CLI version 2.0.0

Usage:


command [options] [arguments]

Options:


 --help (-h)               Display this help message

 --quiet (-q)              Do not output any message

 --verbose (-v|vv|vvv)     Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug

 --version (-V)            Display this application version

 --ansi                    Force ANSI output

 --no-ansi                 Disable ANSI output

 --no-interaction (-n)     Do not ask any interactive question

Available commands:


 help                      Displays help for a command
 list                      Lists commands

admin (Command to create and unlock administrator)


 admin:user:create         Creates an administrator
 admin:user:unlock         Unlock Admin Account

cache (Command to enable, disable, clear, flush cache)


 cache:clean               Cleans cache type(s)
 cache:disable             Disables cache type(s)
 cache:enable              Enables cache type(s)
 cache:flush               Flushes cache storage used by cache type(s)
 cache:status              Checks cache status

catalog (Command to resize product images)


 catalog:images:resize     Creates resized product images

cron (Command to run cron job)


 cron:run                  Runs jobs by schedule

customer (Command to upgrade customer’s hash)


 customer:hash:upgrade     Upgrade customer's hash according to the latest algorithm

deploy (Command to set and show deployment mode)


 deploy:mode:set           Set application mode.
 deploy:mode:show          Displays current application mode.

dev (Command to deploy theme and run tests)


 dev:source-theme:deploy       Collects and publishes source files for theme.

 dev:tests:run                 Runs tests

 dev:urn-catalog:generate      Generates the catalog of URNs to *.xsd mappings for the IDE to highlight xml.

 dev:xml:convert               Converts XML file using XSL style sheets

i18n (Command to install and uninstall language packages)


 i18n:collect-phrases      Discovers phrases in the codebase
 i18n:pack                 Saves language package
 i18n:uninstall            Uninstalls language packages

indexer (Command to reindex and check indexer status)


 indexer:info              Shows allowed Indexers
 indexer:reindex           Reindexes Data
 indexer:set-mode          Sets index mode type
 indexer:show-mode         Shows Index Mode
 indexer:status            Shows status of Indexer

info (Command to get different information about admin URI, framework dependency, module dependency, currency, backup, language and timezone)


 info:adminuri                             Displays the Magento Admin URI

 info:backups:list                         Prints list of available backup files

 info:currency:list                        Displays the list of available currencies

 info:dependencies:show-framework          Shows number of dependencies on Magento framework

 info:dependencies:show-modules            Shows number of dependencies between modules

 info:dependencies:show-modules-circular   Shows number of circular dependencies between modules

 info:language:list                        Displays the list of available language locales

 info:timezone:list                        Displays the list of available timezones

maintenance (Command to enable/disable maintenance mode)


 maintenance:allow-ips     Sets maintenance mode exempt IPs
 maintenance:disable       Disables maintenance mode
 maintenance:enable        Enables maintenance mode
 maintenance:status        Displays maintenance mode status

module (Command to enable/disable module)


 module:disable            Disables specified modules
 module:enable             Enables specified modules
 module:status             Displays status of modules
 module:uninstall          Uninstalls modules installed by composer

sampledata (Command to install/uninstall sample data)


 sampledata:deploy         Deploy sample data modules
 sampledata:remove         Remove all sample data packages from composer.json
 sampledata:reset          Reset all sample data modules for re-installation

setup (Command install/uninstall/upgrade Magento)


 setup:backup                          Takes backup of Magento Application code base, media and database

 setup:config:set                      Creates or modifies the deployment configuration

 setup:cron:run                        Runs cron job scheduled for setup application

 setup:db-data:upgrade                 Installs and upgrades data in the DB

 setup:db-schema:upgrade               Installs and upgrades the DB schema

 setup:db:status                       Checks if DB schema or data requires upgrade

 setup:di:compile                      Generates DI configuration and all non-existing interceptors and factories

 setup:di:compile-multi-tenant         Generates all non-existing proxies and factories, and pre-compile class definitions, inheritance information and plugin definitions

 setup:install                         Installs the Magento application

 setup:performance:generate-fixtures   Generates fixtures

 setup:rollback                        Rolls back Magento Application codebase, media and database

 setup:static-content:deploy           Deploys static view files

 setup:store-config:set                Installs the store configuration

 setup:uninstall                       Uninstalls the Magento application

 setup:upgrade                         Upgrades the Magento application, DB data, and schema

theme (Command to uninstall theme)


 theme:uninstall                       Uninstalls theme

Finding details about each command

To know about the arguments that can be passed to the above commands, we can use --help option to the command.

For example: To know about all arguments for creating admin user (admin:user:create) command, we can run the following command:


php bin/magento --help admin:user:create

The output of above command will be:


Usage:
 admin:user:create [--admin-user="..."] [--admin-password="..."] [--admin-email="..."] [--admin-firstname="..."] [--admin-lastname="..."] [--magento-init-params="..."]

Options:
 --admin-user           (Required) Admin user
 --admin-password       (Required) Admin password
 --admin-email          (Required) Admin email
 --admin-firstname      (Required) Admin first name
 --admin-lastname       (Required) Admin last name
 --magento-init-params  Add to any command to customize Magento initialization parameters
                        For example: "MAGE_MODE=developer&MAGE_DIRS[base][path]=/var/www/example.com&MAGE_DIRS[cache][path]=/var/tmp/cache"
 --help (-h)            Display this help message
 --quiet (-q)           Do not output any message
 --verbose (-v|vv|vvv)  Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug
 --version (-V)         Display this application version
 --ansi                 Force ANSI output
 --no-ansi              Disable ANSI output
 --no-interaction (-n)  Do not ask any interactive question

Similarly, you can find details about all other commands.

Hope this helps. Thanks.