Magento Tips & Tricks

Install Magento 2.4 on Windows Problem Solved

If you have tried to install Magento 2 on Windows – you have encountered some issues with it already. With each version the number of such errors is growing and on the latest Magento 2.4 we have three such errors now.

This is not a surprise – Magento 2 is not supposed to be runned on Windows system – that’s why it is not much tested on it.

You say – who needs to install Magento 2 on Windows anyway? Yes, sure, obviously we’re not going to use it alive in production on Windows local server. Its totally dumb. But you know. Some of the developers are working in the Windows operating system. And imagine – developing and coding Magento 2 themes and extensions. So yes – this could be needed sometimes.

And now we will begin – we will install the latest Magento 2.4 on the Windows local PHP web server and eliminate those errors one by one.

First we will clone the Magento repository. Create a new empty database. Do the composer install. And run the Magento installation command in the command line.

But stop. That’s not all. Starting from Magento 2.4 – ElasticSearch is required.  So we need to install it on Windows first. You can download its installation here: https://www.elastic.co/downloads/elasticsearch

Ok now everything is ready. Go go go: php bin/magento setup:install

Boom. Here goes the first error:

Magento 2.4 Installation Error

In PatchApplier.php line 170:

  Unable to apply data patch Magento\Theme\Setup\Patch\Data\RegisterThemes for module Magento_Theme. Original exception message: Wrong file

In Gd2.php line 64:

  Wrong file

Lets see what is wrong here. Open: lib/internal/Magento/Framework/Image/Adapter/Gd2.php and find this code around lines 85:

     private function validateURLScheme(string $filename) : bool
     {
         $allowed_schemes = ['ftp', 'ftps', 'http', 'https'];
         $url = parse_url($filename);
         if ($url && isset($url['scheme']) && !in_array($url['scheme'], $allowed_schemes)) {
             return false;

This code is validating the url schema of the file. But in Windows files system – you will have something like this C:/LocalServer/WWW/Magento24/…

So in this case the scheme will be just “C”. And this is obviously not on this list. So let’s add it there (or “D” if you local server is installed on another drive):

     private function validateURLScheme(string $filename) : bool
     {
         $allowed_schemes = ['ftp', 'ftps', 'http', 'https', 'C'];
         $url = parse_url($filename);
         if ($url && isset($url['scheme']) && !in_array($url['scheme'], $allowed_schemes)) {
             return false;

So this one is fixed. Lets go again: php bin/magento setup:install

Ok. It’s installed now. Lets go to the backend. Oops here goes another one:

Magento 2.4 Template Validation Error

1 exception(s):

Exception #0 (Magento\Framework\Exception\ValidatorException): Invalid template file: ‘C:/WinNMP/WWW/magento24temp/app/code/Magento/Backend/view/adminhtml/templates/page/js/require_js.phtml’ in module: ‘Magento_Backend’ block’s name: ‘require.js’

Hmm. And why is this template invalid? Because validator is incorrectly fails for validating file path on Windows system in lib/internal/Magento/Framework/View/Element/Template/File/Validator.php around line 117:

                 ($this->isPathInDirectories($filename, $this->_compiledDir)
                     || $this->isPathInDirectories($filename, $this->moduleDirs)
                     || $this->isPathInDirectories($filename, $this->_themesDir)
                     || $this->_isAllowSymlinks)
                 && $this->getRootDirectory()->isFile($this->getRootDirectory()->getRelativePath($filename));
         }
         return $this->_templatesValidationResults[$filename];

What are we gonna do? Lets just bypass this validation making this condition always be true by adding “|| true” there:

                 ($this->isPathInDirectories($filename, $this->_compiledDir)
                     || $this->isPathInDirectories($filename, $this->moduleDirs)
                     || $this->isPathInDirectories($filename, $this->_themesDir)
                     || $this->_isAllowSymlinks
                     || true)
                 && $this->getRootDirectory()->isFile($this->getRootDirectory()->getRelativePath($filename));
         }
         return $this->_templatesValidationResults[$filename];

Don’t forget – we are doing it to make it work on Windows for development purposes. I strongly don’t recommend doing anything like this on live production systems.

Great now we are able to login to the backend. But what happens if we need to re-compile it. For example after upgrading when installing the new module.

Let’s give it a try: php bin/magento setup:di:compile

And we have another one:

Magento 2.4 Compilation Error

In ErrorHandler.php line 61:

  Warning: file_put_contents(C:/WinNMP/WWW/magento24temp/generated/metadata/primary|global|plugin-list.php): failed to open stream: No such file or directory in C:\WinNMP\WWW\magento24temp\lib\internal\Magent

  o\Framework\Interception\PluginListGenerator.php on line 414

Why did this happen? The compiler was trying to create new files with “|” character in its name. It’s totally cool on Linux file systems but not allowed in Windows file systems.

To fix it we need to open this file: lib/internal/Magento/Framework/Interception/PluginListGenerator.php around line 411:

         $this->initialize();
         $configuration = sprintf('directoryList->getPath(DirectoryList::GENERATED_METADATA) . '/' . $key  . '.php',
             $configuration
         );
     }

We will replace “|” with “_” in $key variable which contains the file name:

         $this->initialize();
         $configuration = sprintf('directoryList->getPath(DirectoryList::GENERATED_METADATA) . '/' . str_replace('|', '_', $key)  . '.php',
             $configuration
         );
     }

Now we will check how it works. Do the compile one more time: php bin/magento setup:di:compile

Yippee – compiled successfully. So your Windows Magento 2.4 installation is alive now. Install Magento 2.4 on Windows problem solved!

Have happy coding!

Update: 2021-07-07.

There is a one more bug in Magento 2.4.2-p1 that can cause the static files 404 not found error. It was caused by misuse of the DIRECTORY_SEPARATOR constant. It was used for the “area/theme” definition instead of the regular slash character.

You can fix it in lib/internal/Magento/Framework/App/StaticResource.php line 170. Replace this line:

if (!($this->isThemeAllowed($params['area'] . DIRECTORY_SEPARATOR . $params['theme'])

with the following code:

if (!($this->isThemeAllowed($params['area'] . '/' . $params['theme'])

Thanks to Jay for the update.

14 thoughts on “Install Magento 2.4 on Windows Problem Solved

  1. Hello I already installed the elasticsearach app but still encountering the error “Could not validate a connection to Elasticsearch. No alive nodes found in your cluster” everytime i did the setup:upgrade command any ideas?

    • Hi

      Please check if Elasticsearch is running. You can do it by opening this url in the browser: http://localhost:9200/ (Or use your localhost IP instead). It should return some json encoded data if Elasticsearch is running correctly. If it is not – please make sure that it is set to launch on Windows start up. Or try to run it as a service.

      Regards, Eugene.

    • Hi

      Unfortunately can’t tell you the exact solution for this problem. But can assume as it gives you proper response on 9200 port – the elsaticsearch is running correctly. So the problem is somewhere in the Magento configuration.

      You can try to check in System -> Configuration -> Elastic Configuration if the Server List is set to localhost:9200.

      Also you can try to remove cache and the deployed static content manually before doing the upgrade command.

      Regards, Eugene.

    • Hi Mark.

      Depending of the installation type (git, composer, archive) the Magento library files could be in the vendor folder.

      So you can find it in vendor/magento/framework

      Regards, Eugene.

  2. Hi,
    There is a small mistake in your solution for Validator.php
    In the code part you added:
    || true)
    But in the text where you explain things you have done, you have put the correct code:
    || true
    There shouldn’t be “)” after true.

    • Hi Benoit

      That’s right, there is no need to add the closing bracket after “|| true” as it was already there. So the full code of this updated function looks like this:

          public function isValid($filename)
          {
              $filename = str_replace('\\', '/', $filename);
              if (!isset($this->_templatesValidationResults[$filename])) {
                  $this->_templatesValidationResults[$filename] =
                      ($this->isPathInDirectories($filename, $this->_compiledDir)
                          || $this->isPathInDirectories($filename, $this->moduleDirs)
                          || $this->isPathInDirectories($filename, $this->_themesDir)
                          || $this->_isAllowSymlinks
                          || true)
                      && $this->getRootDirectory()->isFile($this->getRootDirectory()->getRelativePath($filename));
              }
              return $this->_templatesValidationResults[$filename];
          }
      

      Regards, Eugene.

  3. Hi,
    Thanks for all those tips. They worked perfectly with Magento 2.4.1 and XAMPP (Windows 10) but it seems Magento 2.4.2 doesn’t work with XAMPP. As suggested by severaI people I also enabled libsodium.dll but it didn’t work. Everytime I get Error 404 Object not found!
    Magento team seems to have changed a lot of things and NWD great tips worked only until Magento 2.4.1. Thank you.

    • Hi

      This is strange. We are running Magento 2.4.2 on Windows 10 and only had to add libsodium besides of the previous fixes. But it is running on WinNMP via Nginx.

      Maybe try to trace by exception logs or debug where this Object not found error is originating.

      Regards, Eugene.

  4. There is one more issue for windows that will make static files show 404 not found. You can force static file generation or change the following code:

    vendor\magento\framework\App\StaticResource.php
    Line: 170
    Change:
    `if (!($this->isThemeAllowed($params[‘area’] . DIRECTORY_SEPARATOR .` $params[‘theme’])
    To
    `if (!($this->isThemeAllowed($params[‘area’] . “/” . $params[‘theme’])

    This is on Magento 2.4.2-p1

    • Hi Jay.
      You are absolutely right. This is an obvious error. As the DIRECTORY_SEPARATOR should be used for the file system paths only. Not for the area/theme definition.
      Thank you for your update. We will add it to the article.
      Regards, Eugene.

Leave a Reply

Your email address will not be published. Required fields are marked *