Magento 2 Command Line Tips




While working with Magento 2 you will be using the command line a lot more often. I have started a collection of commands that have come in handy while working on various client installations:

Tip #1

If you are getting a permission denied when running the bin/magento command

bin/magento
-bash: bin/magento: Permission denied

You'll be able to work around it by explicitly calling php:

php -f bin/magento
Magento CLI version 2.1.3

Keep in mind that on some systems the cli command for using php might be different

php-cli -f bin/magento
Magento CLI version 2.1.3

especially if you have multiple versions of PHP present the 7.0 executable might need to be called like this:

php70 -f bin/magento
Magento CLI version 2.1.3

Tip #2

A lot of the time the user you use to login via SSH is different to the user who should ultimately own the files. This can cause problems with file permissions down the track (for example files/directories can't be deleted)

The easiest I have found if you have root privileges is that you can change the user you are operating under with the following command

su - www-user -s /bin/sh

In this case we are switching to the user www-user and this will be different for your circumstances. A quick way to check which user to use is to list the current owner of the files with ls -ll. Additionally we specify to use a shell with -s /bin/sh as quite often the user doesn't have a shell set up for us to use. Once switched to the user any commands that you run that create new files will be created under this user.

Tip #3

Composer might not be installed globally - you can still use the version of composer that Magento pulls in as a dependency via

php -f vendor/composer/composer/bin/composer

(I have found using the symlinked version under vendor/bin/composer is often not working)

Tip #4

Your standard memory limit might not be enough to run certain composer commands. Instead of changing the memory limit system wide for the CLI you can set it for one command only with this:

php -d memory_limit=1536M -f vendor/composer/composer/bin/composer require vendor/package

Tip #5

In newer versions of Magento you can selectively use the static compilation process which is a lot faster than having to compile everything. For example here we only compile the back-end adminhtml area for the nl_NL locale:

php -f bin/magento setup:static-content:deploy --area adminhtml -l nl_NL

Tip #6

If you want to quickly check if a module is enabled you can run the following command:

php -f bin/magento module:status | grep 'Fooman\|module'

The above will output the status of all currently known to Magento modules from Fooman:

List of enabled modules:
Fooman_GoogleAnalyticsPlus
List of disabled modules:

Community Tips

To quickly flush varnish (thanks Danny Nimmo)

Run varnishadm ban "req.url ~ /" to quickly flush Varnish (4)

Max Pronko's 10+ favourites

Max Pronko's video shares the top 10 CLI commands he uses all the time when developing Magento 2 modules.

Let me know in the comments your commands that you use frequently or that have helped you out of a tricky situation.

Kristof Ringleff

Kristof Ringleff

Founder and Lead Developer at Fooman

Join the Fooman Community

We send our popular Fooman Developer Monthly email with Magento news, articles & developer tips, as well as occasional Fooman extension updates.