Modules are used to extend and customize Drupal Core. To install module normally we can navigate to "Admin => Extend" link in toolbar. Like older versions of Drupal, Drupal 8 doesn't allow modules to be enable / disable. The only thing we can do is either install a module or completely uninstall it. Below example shows how to install module using code:
<?php
  \Drupal::service('module_installer')->install(['admin_toolbar']);
?>

Below example shows how to uninstall module using code:
<?php
  \Drupal::service('module_installer')->uninstall(['admin_toolbar']);
?>

Reference:

Tags
Submitted by ychaugule on