Magento 2 - 'Area code not set:' after adding sample data

14,280

Solution 1

Had the same issue and was able to resolve it.

Try running this command first:

php bin/magento sampledata:reset

And then re-run:

php bin/magento setup:upgrade

In case you had a memory exhausted error (which I encountered), try adding -dmemory_limit=6G on your setup:upgrade command.

php -dmemory_limit=6G bin/magento setup:upgrade

Solution 2

Try running this command first:

php bin/magento app:config:import

and then clear the cache and run setup:upgrade

Solution 3

I faced this issue on Magento 2.4.2 after installing sample data and again uninstalling few selected sample data module using composer. To fix this issue, I manually changed app\etc\config.php by setting 0 instead of 1 for those extension which are not needed for Sample data (what I tried to remove using CLI command).

Solution 4

I have the same issue on after installing Magento 2.0.7, and then try to install sample data.

Module 'Magento_ConfigurableSampleData':  [Magento\Framework\Exception\SessionException]  Area code not set: Area code must be set before starting a session.

I also tried to reset (php bin/magento sampledata:reset), but there is another error:

  [Zend_Db_Statement_Exception]
  SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'module' cannot be null, query was: INSERT INTO `setup_module` (`modul
  e`, `data_version`) VALUES (?, ?)

Because my setup was based on CLI (php bin/magento setupinstall --...), so I couldn't know why. Until it's been setup again on the website front. In the setup php checking process, there is an php value error:

Your PHP Version is 5.6.18, but always_populate_raw_post_data = 0. $HTTP_RAW_POST_DATA is deprecated from PHP 5.6 onwards and will be removed in PHP 7.0. This will stop the installer from running. Please open your php.ini file and set always_populate_raw_post_data to -1. If you need more help please call your hosting provider.

Therefore, after setting always_populate_raw_post_data = -1 in php.ini or in .htaccess. (restart php or php-fpm, if needed). Then, the sample data is installed and running well.

Hope these steps are helpful for someones.

Solution 5

I had same issue and was able to solve

Try this command for magento (2.4.1) only

php -dmemory_limit=5G bin/magento cache:flush
php -dmemory_limit=5G bin/magento sampledata:reset
php -dmemory_limit=5G bin/magento setup:upgrade

Try this command for magento (2.4.2+) Search within any extensions for usages of the setAreaCode() method, and check where they are being called from.

Replace code

protected function execute(InputInterface $input, OutputInterface $output)
{
    $this->appState->setAreaCode('frontend');
}

By

protected function execute(InputInterface $input, OutputInterface $output)
{
try{
    $output->setDecorated(true);
    $this->appState->setAreaCode(\Magento\Framework\App\Area::AREA_GLOBAL);
} catch (\Magento\Framework\Exception\LocalizedException $exception) {
       // do nothing
   }
}

Alternately use the core \Magento\Framework\App\State::emulateAreaCode method to temporarily set the area code:

$self = $this;
   try {
       $this->appState->emulateAreaCode('frontend', function () use ($self) {
           // operations
       });
   } catch (\Exception $exception) {
       // handle exception
   }
Share:
14,280

Related videos on Youtube

andy jones
Author by

andy jones

Nice to meet you, I’m Andrew Jones. I'm a professional web designer/frontend web developer from Staffordshire. I snowboard in my spare time and make great eCommerce websites in my professional time with https://www.awaredigital.co.uk

Updated on June 04, 2022

Comments

  • andy jones
    andy jones almost 2 years

    So i've gotten magento 2 working all running fine, I've even started on my own theme, but i've tried to install the sample data after I've installed magento. And it has returned 'Area code not set:' in terminal, below is a little run through of my steps.

    bin/magento sampledata:deploy    
    composer update
    

    Which returns:

    Loading composer repositories with package information
    Updating dependencies (including require-dev)
      - Installing magento/module-catalog-sample-data (100.0.3)
        Downloading: 100%         
    
      - Installing magento/module-bundle-sample-data (100.0.3)
        Downloading: 100%         
    
      - Installing magento/module-widget-sample-data (100.0.3)
        Downloading: 100%         
    
      - Installing magento/module-customer-sample-data (100.0.3)
        Downloading: 100%         
    
      - Installing magento/sample-data-media (100.0.3)
        Downloading: 100%         
    
      - Installing magento/module-theme-sample-data (100.0.3)
        Downloading: 100%         
    
      - Installing magento/module-cms-sample-data (100.0.3)
        Downloading: 100%         
    
      - Installing magento/module-catalog-rule-sample-data (100.0.3)
        Downloading: 100%         
    
      - Installing magento/module-sales-rule-sample-data (100.0.3)
        Downloading: 100%         
    
      - Installing magento/module-review-sample-data (100.0.3)
        Downloading: 100%         
    
      - Installing magento/module-tax-sample-data (100.0.3)
        Downloading: 100%         
    
      - Installing magento/module-grouped-product-sample-data (100.0.3)
        Downloading: 100%         
    
      - Installing magento/module-downloadable-sample-data (100.0.3)
        Downloading: 100%         
    
      - Installing magento/module-msrp-sample-data (100.0.3)
        Downloading: 100%         
    
      - Installing magento/module-sales-sample-data (100.0.3)
        Downloading: 100%         
    
      - Installing magento/module-product-links-sample-data (100.0.3)
        Downloading: 100%         
    
      - Installing magento/module-configurable-sample-data (100.0.3)
        Downloading: 100%         
    
      - Installing magento/module-wishlist-sample-data (100.0.3)
        Downloading: 100%         
    
      - Installing magento/module-swatches-sample-data (100.0.3)
        Downloading: 100%         
    
      - Installing magento/module-offline-shipping-sample-data (100.0.3)
        Downloading: 100%         
    
    Writing lock file
    Generating autoload files
    

    I've then run:

    bin/magento setup:upgrade
    

    And i get the following error shown in a screen shot below, i can't seem to find anyone else having the same error online.

    terminal window of Area code not set:

      [Magento\Framework\Exception\SessionException]                       
      Area code not set: Area code must be set before starting a session.  
    
      [Magento\Framework\Exception\LocalizedException]  
      Area code is not set    
    

    And now my mage install just isn't working - error returns:

    Please upgrade your database: Run "bin/magento setup:upgrade" from the Magento root directory.

    Which I can't do because it returns and error, any help would be awesome.

    • Maddy
      Maddy over 8 years
      Please check if the sessions.save_path specified in php.ini is readable by the web user. Also check server and php logs.
    • andy jones
      andy jones over 8 years
      Took a look for the sessions.save_path and made sure it was readable by the web user. chmod & chown to correct settings. Logs show 'Unable to load dynamic library' of 'intl.so' however this has been installed. Thanks
    • rajat kara
      rajat kara over 2 years
  • Morgy
    Morgy over 8 years
    Anytime bro. Glad I was able to help :)
  • Ebin Manuval
    Ebin Manuval over 6 years
    also please check for your current Magento mode magento deploy:mode:show if it is default mode change to to developer mode
  • RedaMakhchan
    RedaMakhchan about 6 years
    Just if someone ( like me ) recieved when calling "php bin/magento setup:upgrade", an Error "Warning: Error while sending QUERY packet. PID=...". He could change "max_allowed_packet" for his mysql (or mariaDB). link : stackoverflow.com/questions/30753674/… He should change
  • Daan van den Bergh
    Daan van den Bergh about 6 years
    I suggest always using the --define (-d) flag and setting the memory_limit much higher (2G or 4G should suffice) after deploying sampledata. It's a long process.