diff --git a/core/core.services.yml b/core/core.services.yml
index 8fe29d2..a1f198c 100644
--- a/core/core.services.yml
+++ b/core/core.services.yml
@@ -1425,7 +1425,7 @@ services:
     class: Drupal\Core\Extension\InfoParser
   twig:
     class: Drupal\Core\Template\TwigEnvironment
-    arguments: ['@app.root', '@cache.default', '@twig.loader', '%twig.config%']
+    arguments: ['@app.root', '@cache.default', '%container.modules%', '@twig.loader', '%twig.config%']
     tags:
       - { name: service_collector, tag: 'twig.extension', call: addExtension }
   twig.extension:
diff --git a/core/lib/Drupal/Core/Template/TwigEnvironment.php b/core/lib/Drupal/Core/Template/TwigEnvironment.php
index dc7de16..53d9713 100644
--- a/core/lib/Drupal/Core/Template/TwigEnvironment.php
+++ b/core/lib/Drupal/Core/Template/TwigEnvironment.php
@@ -11,6 +11,7 @@
 use Drupal\Core\Cache\CacheBackendInterface;
 use Drupal\Core\PhpStorage\PhpStorageFactory;
 use Drupal\Core\Render\SafeString;
+use Drupal\Core\Site\Settings;
 
 /**
  * A class that defines a Twig environment for Drupal.
@@ -32,6 +33,13 @@ class TwigEnvironment extends \Twig_Environment {
   protected $templateClasses;
 
   /**
+   * The list of modules.
+   *
+   * @var array
+   */
+  protected $modules;
+
+  /**
    * Constructs a TwigEnvironment object and stores cache and storage
    * internally.
    *
@@ -39,13 +47,16 @@ class TwigEnvironment extends \Twig_Environment {
    *   The app root.
    * @param \Drupal\Core\Cache\CacheBackendInterface $cache
    *   The cache bin.
+   * @param array $modules
+   *   The list of modules.
    * @param \Twig_LoaderInterface $loader
    *   The Twig loader or loader chain.
    * @param array $options
    *   The options for the Twig environment.
    */
-  public function __construct($root, CacheBackendInterface $cache, \Twig_LoaderInterface $loader = NULL, $options = array()) {
+  public function __construct($root, CacheBackendInterface $cache, array $modules, \Twig_LoaderInterface $loader = NULL, $options = array()) {
     $this->cache_object = $cache;
+    $this->modules = $modules;
 
     // Ensure that twig.engine is loaded, given that it is needed to render a
     // template because functions like TwigExtension::escapeFilter() are called.
@@ -89,6 +100,27 @@ public function updateCompiledTemplate($cache_filename, $name) {
   }
 
   /**
+   * {@inheritdoc}
+   */
+  public function getCacheFilename($name) {
+    // We override the cache filename in order to avoid issues with not using
+    // shared filesystems. The twig templates for example rely on available
+    // twig extensions, so we need to take into account the list of active
+    // modules and the deployment identifier, much like for the container
+    // itself.
+
+    if (!$this->cache) {
+      return FALSE;
+    }
+
+    $class = substr($this->getTemplateClass($name), strlen($this->templateClassPrefix));
+
+    $hash = hash('crc32b', serialize($this->modules) . \Drupal::VERSION . Settings::get('deployment_identifier', \Drupal::VERSION));
+
+    return $this->getCache() . '/' . substr($class, 0, 2) . '/' . substr($class, 2, 2) . '/' . substr($class, 4) . $hash . '.php';
+  }
+
+  /**
    * Implements Twig_Environment::loadTemplate().
    *
    * We need to overwrite this function to integrate with drupal_php_storage().
