N98-magerun2 Tool for Magento 2 Overview
Dmytro Cheshun Avatar

We’ve previously talked about a handy n98-magerun CLI tool, which was a game-changer for Magento 1 developers, turbocharging their development workflows.

Now, let’s shift our focus to n98-magerun2, the next-gen counterpart tailored for Magento 2, courtesy of netz98. Its previous version was popular among Magento developers and we think that n98-magerun2 should be another useful command line interface tool for every new Magento version.

So, let’s install n98-magerun2 and try how it works.

n98-magerun2 can be downloaded in a few ways:

Using wget:

wget https://files.magerun.net/n98-magerun2.phar

Using curl:

curl -O https://files.magerun.net/n98-magerun2.phar

Once downloaded, execute the downloaded .phar file:

chmod +x n98-magerun2.phar

It is recommend to move it into /usr/local/bin. This way, you will be able to run n98-magerun2.phar from any place of your file system via the command line.

sudo mv n98-magerun2.phar /usr/local/bin/

A new n98-magerun2 version looks very similar to the previous one and contains a whole bunch of commands known from the Magento 1 version of n98-magerun. To list all available commands, just type the following line in your terminal:

n98-magerun2.phar list

From the most useful commands represented in the previous version of this tool, n98-magerun2 has the following:

Interaction with Magento cache:

  • cache:clean — cleans Magento caches
  • cache:disable — disables Magento caches
  • cache:enable — enables Magento caches
  • cache:flush — flushes Magento cache storage
  • cache:list — lists all Magento caches

Admin accounts management:

Magento systems:

  • sys:check — checks the Magento system
  • sys:cron:history — last executed Magento 2 cron jobs with status
  • sys:cron:list — lists all Magento 2 cron jobs
  • sys:cron:run — runs a Magento 2 cron job by a job code
  • sys:info — prints information about the current Magento system
  • sys:maintenance — toggles the store maintenance mode

Additionally, you have commands for development, database, config and other Magento parts.

As you might be aware, Magento 2 includes its own command-line interface tool with several shell script utilities for automating development and maintenance operations. It also has some duplicated functionality from the admin backend (such as “clean cache” or “reindex”) as well as others required for deployment, such as “compile dependency injection definitions”.

The built-in tool seems very similar to n98-magerun2, and you can even assume there is no need to use it. However, don’t jump to conclusions, as each of these tools includes distinct commands that complement each other.

Since Magento relies heavily on dependency injection, n98-magerun2 has two more corresponding commands. The first one generates DI configuration and all non-existing interceptors and factories:

n98-magerun2.phar setup:di:compile

The second one generates all non-existing proxies and factories, then pre-compiles class definitions, inheritance information, and Magento 2 plugin definitions:

n98-magerun2.phar setup:di:compile-multi-tenant

Furthermore, n98-magerun2 has one more command which will be useful for Magento 2 developers: “generation:flush”. It flushes generated code like factories and proxies which are, by default, located in var/generation/ folder of your Magento 2 system.

$ n98-magerun2.phar generation:flush
Removed Atwix folder
Removed Symfony folder
Removed Composer folder
Removed Magento folder

As you might remember, developers can generate a new empty module in n98-magerun. The new n98-magerun2 has the same command with the same options. It will create and register new empty Magento 2 module and generate all required config files which you can specify if you want.

Now, we will create a module using n98-magerun2:

$ n98-magerun2.phar dev:module:create --add-all Atwix Example
Created directory: /Users/mitry/Sites/atwix-mage2/app/code/Atwix/Example
Created directory: /Users/mitry/Sites/atwix-mage2/app/code/Atwix/Example/etc
Created directory: /Users/mitry/Sites/atwix-mage2/app/code/Atwix/Example/Block
Created directory: /Users/mitry/Sites/atwix-mage2/app/code/Atwix/Example/Helper
Created directory: /Users/mitry/Sites/atwix-mage2/app/code/Atwix/Example/Model
Created directory: /Users/mitry/Sites/atwix-mage2/app/code/Atwix/Example/Setup/
Created file: /Users/mitry/Sites/atwix-mage2/app/code/Atwix/Example/registration.php
Created file: /Users/mitry/Sites/atwix-mage2/app/code/Atwix/Example/etc/module.xml
Created file: /Users/mitry/Sites/atwix-mage2/app/code/Atwix/Example/etc/config.xml
Created file: /Users/mitry/Sites/atwix-mage2/app/code/Atwix/Example/etc/di.xml
Created file: /Users/mitry/Sites/atwix-mage2/app/code/Atwix/Example/etc/events.xml
Created file: /Users/mitry/Sites/atwix-mage2/app/code/Atwix/Example/etc/crontab.xml

Just like n98-magerun, the new n98-magerun2 can download and install Magento 2. Here is this command:

n98-magerun2.phar install

Let’s try it out:

$ n98-magerun2.phar install
                          
Magento 2 Installation  
                          
[1]  magento-ce-2.0.2
[2]  magento-ce-2.0.1
[3]  magento-ce-2.0.0
Choose a magento version: 1
Enter installation folder: [./magento] atwix-mage2
Found executable composer
Installing atwix-mage2/project-community-edition (2.0.2)
  - Installing atwix-mage2/project-community-edition (2.0.2)
    Downloading: 100%         

...

Created project in /Users/mitry/Sites/atwix-mage2
Loading composer repositories with package information
Installing dependencies (including require-dev)
Writing lock file
Generating autoload files

The system will ask you to provide some required information. Everything is similar to Magento 1 installation using n98-magerun.

Please enter the database host [localhost]: localhost
Please enter the database port  [3306]: 
Please enter the database username [root]: web
Please enter the database password []: web
Please enter the database name [magento]: atwix_mage2
Created database atwix_mage2
Please enter the session save: [files]: 
Please enter the admin/backend frontname: [admin] 
Please enter the default currency code: [EUR]: USD
Please enter the locale code: [de_DE]: en_US
Please enter the timezone: [Europe/Berlin]: Europe/Kiev
Please enter the admin username: [admin]: 
Please enter the admin password: [password123]: 
Please enter the admin's firstname: [John]: Dmytro
Please enter the admin's lastname: [Doe]: Cheshun
Please enter the admin's email: [john.doe@example.com]: test@atwix.com
Please enter the base url: http://atwix.mage2.dev
Start installation process.

After downloading Magento 2, it will run “setup:install” from Magento standard CLI tool:

/usr/bin/env php /Users/mitry/Sites/atwix-mage2/bin/magento setup:install 
--language='en_US' --timezone='Europe/Kiev' --db-host='localhost' 
--db-name='atwix_mage2' --db-user='web' 
--base-url='http://atwix.mage2.dev/' --use-rewrites='1' 
--use-secure='0' --use-secure-admin='1' --admin-user='admin' 
--admin-lastname='Cheshun' --admin-firstname='Dmytro' 
--admin-email='test@atwix.com' --admin-password='password123' 
--session-save='files' --backend-frontname='admin' 
--currency='USD' --db-password='web' 

[SUCCESS]: Magento installation complete.
[SUCCESS]: Magento Admin URI: /admin
Write BaseURL to .htaccess file? [n]: 
Install sample data? [y]: n
Successfully installed magento

For managing sample data, n98-magerun2 includes three commands just like the standard Magento 2 CLI tool:

  • sampledata:deploy — deploys sample data modules
  • sampledata:remove — removes all sample data packages from composer.json
  • sampledata:reset — resets all sample data modules for re-installation

Here are the following commands you can run using Magento’s built in tool or n98-magerun2:

  • deploy:mode:set — sets an application mode
  • deploy:mode:show — displays the current application mode
  • sampledata:reset — resets all sample data modules for re-installation
$ n98-magerun2.phar deploy:mode:set --skip-compilation developer
Enabled developer mode.

$ n98-magerun2.phar deploy:mode:show
Current application mode: developer.

One more ability to manage maintenance mode of your store came from the standard bin/magento tool. Here is the list of build in commands:

  • maintenance:allow-ips — sets a maintenance mode except IPs
  • maintenance:disable — disables a maintenance mode
  • sampledata:reset — enables a maintenance mode
  • maintenance:status — displays a maintenance mode status

For example, we enable maintenance mode for all hosts except the localhost:

$ n98-magerun2.phar maintenance:enable
Enabled maintenance mode

$ n98-magerun2.phar maintenance:allow-ips 127.0.0.1
Set exempt IP-addresses: 127.0.0.1

Then, check the status:

$ n98-magerun2.phar maintenance:status
Status: maintenance mode is active
List of exempt IP-addresses: 127.0.0.1

n98-magerun2 is another handy tool which provides new features for Magento 2 development process. It already contains bunch of commands well-known from previous n98-magerun version and also includes new commands from standard Magento command line interface tool.