26

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.

andy jones
  • 904
  • 1
  • 12
  • 37
  • Please check if the sessions.save_path specified in php.ini is readable by the web user. Also check server and php logs. – Maddy Feb 01 '16 at 21:40
  • 1
    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 – andy jones Feb 02 '16 at 09:21
  • https://tagvibe.com/magento2/how-to-fix-the-area-code-not-set-issue/ – rajat kara Dec 10 '21 at 12:04

5 Answers5

48

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
halfer
  • 19,824
  • 17
  • 99
  • 186
Morgy
  • 666
  • 6
  • 6
  • Anytime bro. Glad I was able to help :) – Morgy Feb 11 '16 at 09:50
  • also please check for your current Magento mode `magento deploy:mode:show` if it is default mode change to to developer mode – Ebin Manuval Nov 15 '17 at 19:45
  • 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 : https://stackoverflow.com/questions/30753674/error-while-sending-query-packet He should change – RedaMakhchan Mar 09 '18 at 18:03
  • 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. – Daan van den Bergh Mar 20 '18 at 12:15
6

Try running this command first:

php bin/magento app:config:import

and then clear the cache and run setup:upgrade

Shreya Maria
  • 488
  • 8
  • 18
2

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).

Wasim
  • 1,146
  • 11
  • 25
1

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.

Jialing1000
  • 21
  • 1
  • 1
  • 6
1

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
   }
Kaushal Sachan
  • 1,175
  • 11
  • 8