How to enable (install) a module programmatically in Drupal 8

How to enable (install) a module programmatically in Drupal 8

10.11.2015

A big change in Drupal 8 is that you can no longer enable and disable modules. You can only install and uninstall. The difference is paramount.

In Drupal 7 if you disabled a module and then enabled it back, you retained the modules settings all data stored by that module. This is not longer the case with Drupal 8. Uninstalling means completely removing all data and settings added to the module when it was installed. So please, be careful :)

How to install a module programmatically? Check below:

Lets install Admin Toolbar. A nice handy module extending the Drupal default toolbar.

\Drupal::service('module_installer')->install(['admin_toolbar']);

Looking for web development experts?
Check our services

All services
Drupal
Symfony
React
PHP
DevOps
Nodejs
Design/UX