How do I programmatically install a module? 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.
Use "module_installer" service.

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

// Install single module.
$installer-&gt;install(['module_name']);
// Install multiple modules.
$installer-&gt;install(['module_name_1','module_name_2']);


Also for uninstallation of the module use the next example:

$installer-&gt;uninstall(['module_name']);