diff --git a/core/includes/common.inc b/core/includes/common.inc
index f954b20..08b3fe7 100644
--- a/core/includes/common.inc
+++ b/core/includes/common.inc
@@ -2,6 +2,7 @@
 
 use Drupal\Component\Utility\Crypt;
 use Drupal\Component\Utility\Json;
+use Drupal\Component\Utility\Settings;
 use Drupal\Component\Utility\SortArray;
 use Drupal\Component\Utility\String;
 use Drupal\Component\Utility\Tags;
@@ -269,11 +270,17 @@ function drupal_get_region_content($region = NULL, $delimiter = ' ') {
 function drupal_get_profile() {
   global $install_state;
 
-  if (isset($install_state['parameters']['profile'])) {
-    $profile = $install_state['parameters']['profile'];
+  if (drupal_installation_attempted()) {
+    // If the profile has been selected return it.
+    if (isset($install_state['parameters']['profile'])) {
+      $profile = $install_state['parameters']['profile'];
+    }
+    else {
+      $profile = '';
+    }
   }
   else {
-    $profile = variable_get('install_profile', 'standard');
+    $profile = Settings()->get('install_profile') ?: 'standard';
   }
 
   return $profile;
diff --git a/core/includes/install.core.inc b/core/includes/install.core.inc
index 8e4b66e..d75d399 100644
--- a/core/includes/install.core.inc
+++ b/core/includes/install.core.inc
@@ -1234,6 +1234,14 @@ function install_settings_form_submit($form, &$form_state) {
     );
   }
 
+  // Remember the profile which was used.
+  $settings['settings'] = array(
+    'install_profile' => (object) array(
+      'value'    => $install_state['parameters']['profile'],
+      'required' => TRUE,
+    ),
+  );
+
   drupal_rewrite_settings($settings);
 
   // Add the config directories to settings.php.
@@ -1986,8 +1994,6 @@ function install_update_configuration_translations(&$install_state) {
  */
 function install_finished(&$install_state) {
   $profile = drupal_get_profile();
-  // Remember the profile which was used.
-  variable_set('install_profile', $profile);
 
   // Installation profiles are always loaded last.
   module_set_weight($profile, 1000);
diff --git a/core/includes/update.inc b/core/includes/update.inc
index 5e8ff4e..113dcdf 100644
--- a/core/includes/update.inc
+++ b/core/includes/update.inc
@@ -439,6 +439,23 @@ function update_prepare_d8_bootstrap() {
       }
     }
   }
+  // Moves install_profile from variable to settings. You can't do that in
+  // system.install because _system_rebuild_module_data() needs the profile
+  // directly. Check that it has not been set already. This is the case for
+  // Simpletest upgrade path tests.
+  if (!settings()->get('install_profile')) {
+    $old_variable = unserialize(Drupal::database()->query('SELECT value FROM {variable} WHERE name = :name', array(':name' => 'install_profile'))->fetchField());
+    $settings = array(
+      'settings' => array(
+        'install_profile' => (object) array(
+            'value'    => $old_variable,
+            'required' => TRUE,
+        ),
+      )
+    );
+    drupal_rewrite_settings($settings);
+  }
+
   // Now remove the cache override.
   $settings = settings()->getAll();
   unset($settings['cache']['default']);
diff --git a/core/modules/system/lib/Drupal/system/Tests/Bootstrap/GetFilenameUnitTest.php b/core/modules/system/lib/Drupal/system/Tests/Bootstrap/GetFilenameUnitTest.php
index d2d4cc7..c939630 100644
--- a/core/modules/system/lib/Drupal/system/Tests/Bootstrap/GetFilenameUnitTest.php
+++ b/core/modules/system/lib/Drupal/system/Tests/Bootstrap/GetFilenameUnitTest.php
@@ -26,6 +26,11 @@ public static function getInfo() {
    * Tests that drupal_get_filename() works when the file is not in database.
    */
   function testDrupalGetFilename() {
+    // drupal_get_profile() is using obtaining the profile from state if the
+    // install_state global is not set.
+    global $install_state;
+    $install_state['parameters']['profile'] = 'testing';
+
     // Assert that the test is meaningful by making sure the keyvalue service
     // does not exist.
     $this->assertFalse(drupal_container()->has('keyvalue'), 'The container has no keyvalue service.');
diff --git a/core/modules/system/lib/Drupal/system/Tests/Upgrade/SystemUpgradePathTest.php b/core/modules/system/lib/Drupal/system/Tests/Upgrade/SystemUpgradePathTest.php
index a53b70f..f6e43f9 100644
--- a/core/modules/system/lib/Drupal/system/Tests/Upgrade/SystemUpgradePathTest.php
+++ b/core/modules/system/lib/Drupal/system/Tests/Upgrade/SystemUpgradePathTest.php
@@ -80,6 +80,10 @@ public function testVariableUpgrade() {
       'page.front' => 'node',
     );
 
+    // The upgrade path converts "install_profile" to the settings key
+    // "install_profile".
+    $this->assertEqual('minimal', $this->container->get('settings')->get('install_profile'));
+
     $expected_config['user.settings'] = array(
       'cancel_method' => 'user_cancel_reassign',
     );
diff --git a/core/modules/system/lib/Drupal/system/Tests/Upgrade/UpgradePathTestBase.php b/core/modules/system/lib/Drupal/system/Tests/Upgrade/UpgradePathTestBase.php
index d7052fa..e6d57d9 100644
--- a/core/modules/system/lib/Drupal/system/Tests/Upgrade/UpgradePathTestBase.php
+++ b/core/modules/system/lib/Drupal/system/Tests/Upgrade/UpgradePathTestBase.php
@@ -162,6 +162,19 @@ protected function setUp() {
     // Restore necessary variables.
     $this->variable_set('site_mail', 'simpletest@example.com');
 
+    // Override $update_free_access in settings.php to allow the anonymous user
+    // to run updates.
+    $old_variable = unserialize(db_query('SELECT value FROM {variable} WHERE name = :name', array(':name' => 'install_profile'))->fetchField());
+    $settings = array(
+      'settings' => array(
+        'install_profile' => (object) array(
+          'value'    => $old_variable,
+          'required' => TRUE,
+        ),
+      )
+    );
+    $this->writeSettings($settings);
+
     drupal_set_time_limit($this->timeLimit);
     $this->setup = TRUE;
   }
diff --git a/core/modules/system/tests/upgrade/drupal-7.system.database.php b/core/modules/system/tests/upgrade/drupal-7.system.database.php
index beebe4e..f15d406 100644
--- a/core/modules/system/tests/upgrade/drupal-7.system.database.php
+++ b/core/modules/system/tests/upgrade/drupal-7.system.database.php
@@ -243,3 +243,8 @@
     'value' => serialize('public://color/seven-09696463/dummy-screenshot.png'),
   ))
   ->execute();
+
+db_update('variable')
+  ->fields(array('value' => 's:7:"minimal";'))
+  ->condition('name', 'install_profile')
+  ->execute();
