diff --git a/core/includes/install.core.inc b/core/includes/install.core.inc
index 1616f18..2b756bf 100644
--- a/core/includes/install.core.inc
+++ b/core/includes/install.core.inc
@@ -20,6 +20,7 @@
 use Drupal\Core\Site\Settings;
 use Drupal\Core\StringTranslation\Translator\FileTranslation;
 use Drupal\Core\StackMiddleware\ReverseProxyMiddleware;
+use Drupal\Core\StreamWrapper\PublicStream;
 use Drupal\Core\Extension\ExtensionDiscovery;
 use Drupal\Core\DependencyInjection\ContainerBuilder;
 use Drupal\Core\Url;
@@ -1036,6 +1037,21 @@ function install_base_system(&$install_state) {
   // Install system.module.
   drupal_install_system($install_state);
 
+  // Prevent the installer from using the system temporary directory after the
+  // system module has been installed.
+  if (drupal_valid_test_ua()) {
+    // While the temporary directory could be preset/enforced in settings.php
+    // like the public files directory, some tests expect it to be configurable
+    // in the UI. If declared in settings.php, they would no longer be
+    // configurable. The temporary directory needs to match what is set in each
+    // test types ::prepareEnvironment() step.
+    $temporary_directory = PublicStream::basePath() . '/temp';
+    file_prepare_directory($temporary_directory, FILE_MODIFY_PERMISSIONS | FILE_CREATE_DIRECTORY);
+    \Drupal::configFactory()->getEditable('system.file')
+      ->set('path.temporary', $temporary_directory)
+      ->save();
+  }
+
   // Call file_ensure_htaccess() to ensure that all of Drupal's standard
   // directories (e.g., the public files directory and config directory) have
   // appropriate .htaccess files. These directories will have already been
diff --git a/core/lib/Drupal/Core/Test/FunctionalTestSetupTrait.php b/core/lib/Drupal/Core/Test/FunctionalTestSetupTrait.php
index dbbd962..96bcdc8 100644
--- a/core/lib/Drupal/Core/Test/FunctionalTestSetupTrait.php
+++ b/core/lib/Drupal/Core/Test/FunctionalTestSetupTrait.php
@@ -316,15 +316,8 @@ protected function initSettings() {
   protected function initConfig(ContainerInterface $container) {
     $config = $container->get('config.factory');
 
-    // Manually create and configure private and temporary files directories.
-    // While these could be preset/enforced in settings.php like the public
-    // files directory above, some tests expect them to be configurable in the
-    // UI. If declared in settings.php, they would no longer be configurable.
+    // Manually create the private directory.
     file_prepare_directory($this->privateFilesDirectory, FILE_CREATE_DIRECTORY);
-    file_prepare_directory($this->tempFilesDirectory, FILE_CREATE_DIRECTORY);
-    $config->getEditable('system.file')
-      ->set('path.temporary', $this->tempFilesDirectory)
-      ->save();
 
     // Manually configure the test mail collector implementation to prevent
     // tests from sending out emails and collect them in state instead.
diff --git a/core/lib/Drupal/Core/Test/TestSetupTrait.php b/core/lib/Drupal/Core/Test/TestSetupTrait.php
index 32e6fca..31849c8 100644
--- a/core/lib/Drupal/Core/Test/TestSetupTrait.php
+++ b/core/lib/Drupal/Core/Test/TestSetupTrait.php
@@ -92,8 +92,12 @@
   /**
    * The temporary file directory for the test environment.
    *
+   * This value has to match the temporary directory created in
+   * install_base_system() for test installs.
+   *
    * @see \Drupal\simpletest\TestBase::prepareEnvironment()
    * @see \Drupal\Tests\BrowserTestBase::prepareEnvironment()
+   * @see install_base_system()
    *
    * @var string
    */
diff --git a/core/modules/config_translation/src/Tests/ConfigTranslationUiTest.php b/core/modules/config_translation/src/Tests/ConfigTranslationUiTest.php
index b69357e..5018efa 100644
--- a/core/modules/config_translation/src/Tests/ConfigTranslationUiTest.php
+++ b/core/modules/config_translation/src/Tests/ConfigTranslationUiTest.php
@@ -648,7 +648,7 @@ public function testPluralConfigStringsSourceElements() {
 
     foreach ($languages as $langcode => $data) {
       // Import a .po file to add a new language with a given number of plural forms
-      $name = tempnam('temporary://', $langcode . '_') . '.po';
+      $name = \Drupal::service('file_system')->tempnam('temporary://', $langcode . '_') . '.po';
       file_put_contents($name, $this->getPoFile($data['plurals']));
       $this->drupalPostForm('admin/config/regional/translate/import', array(
         'langcode' => $langcode,
@@ -684,7 +684,7 @@ public function testPluralConfigStrings() {
 
     // First import a .po file with multiple plural forms.
     // This will also automatically add the 'sl' language.
-    $name = tempnam('temporary://', "sl_") . '.po';
+    $name = \Drupal::service('file_system')->tempnam('temporary://', "sl_") . '.po';
     file_put_contents($name, $this->getPoFile(4));
     $this->drupalPostForm('admin/config/regional/translate/import', array(
       'langcode' => 'sl',
diff --git a/core/modules/locale/src/Form/ExportForm.php b/core/modules/locale/src/Form/ExportForm.php
index 4a4695b..47a3aea 100644
--- a/core/modules/locale/src/Form/ExportForm.php
+++ b/core/modules/locale/src/Form/ExportForm.php
@@ -3,6 +3,7 @@
 namespace Drupal\locale\Form;
 
 use Drupal\Component\Gettext\PoStreamWriter;
+use Drupal\Core\File\FileSystemInterface;
 use Drupal\Core\Form\FormBase;
 use Drupal\Core\Form\FormStateInterface;
 use Drupal\Core\Language\LanguageInterface;
@@ -24,13 +25,23 @@ class ExportForm extends FormBase {
   protected $languageManager;
 
   /**
+   * The file system service.
+   *
+   * @var \Drupal\Core\File\FileSystemInterface
+   */
+  protected $fileSystem;
+
+  /**
    * Constructs a new ExportForm.
    *
    * @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
    *   The language manager.
+   * @param \Drupal\Core\File\FileSystemInterface $file_system
+   *   The file system service.
    */
-  public function __construct(LanguageManagerInterface $language_manager) {
+  public function __construct(LanguageManagerInterface $language_manager, FileSystemInterface $file_system) {
     $this->languageManager = $language_manager;
+    $this->fileSystem = $file_system;
   }
 
   /**
@@ -38,7 +49,8 @@ public function __construct(LanguageManagerInterface $language_manager) {
    */
   public static function create(ContainerInterface $container) {
     return new static(
-      $container->get('language_manager')
+      $container->get('language_manager'),
+      $container->get('file_system')
     );
   }
 
@@ -147,7 +159,7 @@ public function submitForm(array &$form, FormStateInterface $form_state) {
 
     $item = $reader->readItem();
     if (!empty($item)) {
-      $uri = tempnam('temporary://', 'po_');
+      $uri = $this->fileSystem->tempnam('temporary://', 'po_');
       $header = $reader->getHeader();
       $header->setProjectName($this->config('system.site')->get('name'));
       $header->setLanguageName($language_name);
diff --git a/core/modules/locale/src/Tests/LocaleExportTest.php b/core/modules/locale/src/Tests/LocaleExportTest.php
index 3d861de..f9cd2ea 100644
--- a/core/modules/locale/src/Tests/LocaleExportTest.php
+++ b/core/modules/locale/src/Tests/LocaleExportTest.php
@@ -43,7 +43,7 @@ protected function setUp() {
   public function testExportTranslation() {
     // First import some known translations.
     // This will also automatically add the 'fr' language.
-    $name = tempnam('temporary://', "po_") . '.po';
+    $name = \Drupal::service('file_system')->tempnam('temporary://', "po_") . '.po';
     file_put_contents($name, $this->getPoFile());
     $this->drupalPostForm('admin/config/regional/translate/import', array(
       'langcode' => 'fr',
@@ -62,7 +62,7 @@ public function testExportTranslation() {
     $this->assertRaw('msgstr "lundi"', 'French translations present in exported file.');
 
     // Import some more French translations which will be marked as customized.
-    $name = tempnam('temporary://', "po2_") . '.po';
+    $name = \Drupal::service('file_system')->tempnam('temporary://', "po2_") . '.po';
     file_put_contents($name, $this->getCustomPoFile());
     $this->drupalPostForm('admin/config/regional/translate/import', array(
       'langcode' => 'fr',
diff --git a/core/modules/locale/src/Tests/LocaleImportFunctionalTest.php b/core/modules/locale/src/Tests/LocaleImportFunctionalTest.php
index 0b69da2..7fb93d9 100644
--- a/core/modules/locale/src/Tests/LocaleImportFunctionalTest.php
+++ b/core/modules/locale/src/Tests/LocaleImportFunctionalTest.php
@@ -369,7 +369,7 @@ public function testCreatedLanguageTranslation() {
    *   (optional) Additional options to pass to the translation import form.
    */
   public function importPoFile($contents, array $options = array()) {
-    $name = tempnam('temporary://', "po_") . '.po';
+    $name = \Drupal::service('file_system')->tempnam('temporary://', "po_") . '.po';
     file_put_contents($name, $contents);
     $options['files[file]'] = $name;
     $this->drupalPostForm('admin/config/regional/translate/import', $options, t('Import'));
diff --git a/core/modules/locale/src/Tests/LocalePluralFormatTest.php b/core/modules/locale/src/Tests/LocalePluralFormatTest.php
index 00fe8d5..c0e9041 100644
--- a/core/modules/locale/src/Tests/LocalePluralFormatTest.php
+++ b/core/modules/locale/src/Tests/LocalePluralFormatTest.php
@@ -351,7 +351,7 @@ public function testPluralEditExport() {
    *   Additional options to pass to the translation import form.
    */
   public function importPoFile($contents, array $options = array()) {
-    $name = tempnam('temporary://', "po_") . '.po';
+    $name = \Drupal::service('file_system')->tempnam('temporary://', "po_") . '.po';
     file_put_contents($name, $contents);
     $options['files[file]'] = $name;
     $this->drupalPostForm('admin/config/regional/translate/import', $options, t('Import'));
diff --git a/core/modules/system/src/Tests/Theme/TwigTransTest.php b/core/modules/system/src/Tests/Theme/TwigTransTest.php
index b8718e7..ef6fe05 100644
--- a/core/modules/system/src/Tests/Theme/TwigTransTest.php
+++ b/core/modules/system/src/Tests/Theme/TwigTransTest.php
@@ -209,7 +209,7 @@ protected function installLanguages() {
         $this->assertRaw('"edit-languages-' . $langcode . '-weight"', 'Language code found.');
 
         // Import the custom .po contents for the language.
-        $filename = tempnam('temporary://', "po_") . '.po';
+        $filename = \Drupal::service('file_system')->tempnam('temporary://', "po_") . '.po';
         file_put_contents($filename, $contents);
         $options = array(
           'files[file]' => $filename,
