diff --git a/core/includes/install.core.inc b/core/includes/install.core.inc
index 46fbcbc..891174e 100644
--- a/core/includes/install.core.inc
+++ b/core/includes/install.core.inc
@@ -2172,15 +2172,6 @@ function install_update_configuration_translations(&$install_state) {
  *   A message informing the user that the installation is complete.
  */
 function install_finished(&$install_state) {
-  $profile = drupal_get_profile();
-
-  // Installation profiles are always loaded last.
-  module_set_weight($profile, 1000);
-
-  // Flush all caches to ensure that any full bootstraps during the installer
-  // do not leave stale cached data, and that any content types or other items
-  // registered by the installation profile are registered correctly.
-  drupal_flush_all_caches();
 
   drupal_set_title(t('@drupal installation complete', array('@drupal' => drupal_install_profile_distribution_name())), PASS_THROUGH);
 
@@ -2225,6 +2216,10 @@ function _install_module_batch($module, $module_name, &$context) {
  * 'Finished' callback for module installation batch.
  */
 function _install_profile_modules_finished($success, $results, $operations) {
+  $profile = drupal_get_profile();
+  // Installation profiles are always loaded last.
+  module_set_weight($profile, 1000);
+
   // Flush all caches to complete the module installation process. Subsequent
   // installation tasks will now have full access to the profile's modules.
   drupal_flush_all_caches();
@@ -2722,6 +2717,8 @@ function install_configure_form_submit($form, &$form_state) {
     if ($form_state['values']['update_status_module'][2]) {
       \Drupal::config('update.settings')->set('notification.emails', array($form_state['values']['account']['mail']))->save();
     }
+    // Flush all caches after module installation.
+    drupal_flush_all_caches();
   }
 
   // We precreated user 1 with placeholder values. Let's save the real values.
diff --git a/core/modules/simpletest/lib/Drupal/simpletest/WebTestBase.php b/core/modules/simpletest/lib/Drupal/simpletest/WebTestBase.php
index 265012e..418241c 100644
--- a/core/modules/simpletest/lib/Drupal/simpletest/WebTestBase.php
+++ b/core/modules/simpletest/lib/Drupal/simpletest/WebTestBase.php
@@ -823,16 +823,15 @@ protected function setUp() {
       }
       $class = get_parent_class($class);
     }
+
     if ($modules) {
       $modules = array_unique($modules);
       $success = \Drupal::moduleHandler()->install($modules, TRUE);
       $this->assertTrue($success, t('Enabled modules: %modules', array('%modules' => implode(', ', $modules))));
-      $this->rebuildContainer();
+      // Reset/rebuild all data structures after enabling the modules.
+      $this->resetAll();
     }
 
-    // Reset/rebuild all data structures after enabling the modules.
-    $this->resetAll();
-
     // Now make sure that the file path configurations are saved. This is done
     // after we install the modules to override default values.
     foreach ($variable_groups as $config_base => $variables) {
