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.