For Drupal 7, I found How do I programmatically install and enable a module?

What is the equivalent code for Drupal 8?

UPD: here Configuration Manager or hook_update() to enable modules I found the case when you can use the code during deployments, specially when your module has updates in own dependencies.


1 Answer 1


Use "module_installer" service.

$installer = \Drupal::service('module_installer');
// Or from the container.
$installer = $container->get('module_installer');

// Install single module.
// Install multiple modules.

Also for uninstallation of the module use the next example:


Not the answer you're looking for? Browse other questions tagged or ask your own question.