We’ve already described the handy CLI tool for Magento 1 developers called n98-magerun and the ability to speed up the development process using it – in the article. And now we would like to talk about new n98-magerun2 for Magento 2 provided by netz98. It worth mentioning that its previous version was very popular among Magento developers and we think that n98-magerun2 should be another very useful command line interface tool for new Magento version.
So, let’s install n98-magerun2 and try how it works.
It can be downloaded in few ways:
curl -O https://files.magerun.net/n98-magerun2.phar
Then, execute the downloaded .phar file:
chmod +x n98-magerun2.phar
We would recommend moving it into /usr/local/bin. It will give an ability to run n98-magerun2.phar from any place of your file system via command line.
sudo mv n98-magerun2.phar /usr/local/bin/
New n98-magerun2 version looks very similar to the previous one and involves the same actions. Moreover, it contains a whole bunch of commands known from n98-magerun (version for Magento 1). So, to list all available commands just type the following line in your terminal:
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
Managing admin accounts:
- admin:user:change-password – changes password of an admin user
- admin:user:delete – deletes account of an admin user
- admin:user:list – lists admin users
- sys:check – checks Magento system
- sys:cron:history – last executed cronjobs with status
- sys:cron:list – lists all cronjobs
- sys:cron:run – runs a cronjob by a job code
- sys:info – prints information about the current Magento system
- sys:maintenance – toggles store maintenance mode
Additionally, you have commands for development, database, config and other Magento parts.
As you may also know, Magento 2 contains its own command line interface tool and there is a number of shell script utilities to automate development and maintenance operations. Also, it contains some duplicated functionality from the admin backend (such as “clean cache” or “reindex”), and some other that are needed during deployment, such as “compile dependency injection definitions”. The built in tool seems very similar to n98-magerun2, and you can even assume that there is no need to use it. Do not make hasty conclusions as each of these tools includes many different commands that fill up each other.
Since the Magento software uses dependency injection, n98-magerun2 has two more corresponding commands. The first one generates DI configuration and all non-existing interceptors and factories:
The second one generates all non-existing proxies and factories, then pre-compiles class definitions, inheritance information and plugins definitions:
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 remember, there is an ability to generate a new empty module in n98-magerun. The new n98-magerun2 also 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
As well as n98-magerun, the new n98-magerun2 can download and install Magento 2. Here is this command:
Let’s try it out:
$ n98-magerun2.phar install Magento 2 Installation  magento-ce-2.0.2  magento-ce-2.0.1  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
Then it 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 : 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: [firstname.lastname@example.org]: email@example.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' --firstname.lastname@example.org' --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 – the same as in 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
And the following commands you can run using Magento’s built in tool or n98-magerun2:
- deploy:mode:set – sets application mode
- deploy:mode:show – displays 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 maintenance mode except IPs
- maintenance:disable – disables maintenance mode
- sampledata:reset – enables maintenance mode
- maintenance:status – displays 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.