Using below example we can check if module is installed or not :

<?php
 
if (\Drupal::service('module_handler')->moduleExists('comment')) {
    echo
'Module Installed';
  } else {
    echo
'Module not installed';
  }
?>

Reference:

Tags: