Setup Magento 2 with Sample Data through Composer

Magento can be setup in 4 different ways, here i’m touching just the 4th – through composer.

  1.  Composer create project, the final parameter is where it will be installed Magento. In my case “./”, it is in the current directory. 
composer create-project --repository-url=https://repo.magento.com/ magento/project-community-edition ./

During installation I got a series of problems, I’ll try to add them between steps so you know what it can happen, example:

Composer failing on Mac (OSX) with MAMP Pro with the following message

  - Your requirements could not be resolved to an installable set of packages.
  - the requested PHP extension mcrypt is missing from your system

More about this error in this post: Composer – PHP Error

so it has to be run this command :

php /usr/local/bin/composer create-project --repository-url=https://repo.magento.com/ magento/project-community-edition project_name

2. Follow the Web setup of your domain (my localhost domain is: mage.dev).

 http://mage.dev/setup
  • If after setup you get this error:
"Unable to retrieve deployment version of static files from the file system...."

That means the static files were not generated and you have to go in command line (cli) and run the following command

# it is very important to have the "en_GB" (or other language)
# as this was the main reasons not to generate properlly
php bin/magento setup:static-content:deploy en_GB

Another encountered error was:

[error] 13168#2222: *43 FastCGI sent in stderr: 
"PHP message: PHP Fatal error:  Uncaught Error: Cannot instantiate interface Magento\Framework\App\Config\Scope\ReaderPoolInterface 
in /var/www/magento/web/vendor/magento/framework/ObjectManager/Factory/Dynamic/Developer.php:73"

The solution was well described in this post on StackExchange which is :

If you use Nginx configuration from the official sample and running Magento in the default/developer mode 
this situation might happen with document root global value set as

>  root $MAGE_ROOT/pub;

The issue can be fixed by setting Magento root from pub 
directory to the root index.php

>   root $MAGE_ROOT;
  • Problem: No images, css or js files loading (404 error) –  after reaching this point and your website is still not accessible is seems there could have been an error on generating  in correct order the static files:
#make sure the project has the correct GROUP permissions
sudo chown -R :www-data ./

# make sure the permissions for this 2 folders are 0777
sudo chmod -R 0777 var/ 
sudo chmod -R 0777 pub/static

After permissions checks we have to generate the static files (thanks to Angelo’s tips)

# Clearing cache...
sudo rm -rf var/cache/ \
&& sudo rm -rf var/generation/ \
&& sudo rm -rf var/di/ \
&& sudo rm -rf var/page_cache/ \
&& sudo rm -rf var/view_preprocessed/

# Compiling dependency injection...
php bin/magento setup:di:compile

# Deploying static content...
php bin/magento setup:static-content:deploy
php bin/magento setup:static-content:deploy en_GB

3. Make sure you have the latest packages

composer update
bin/magento setup:upgrade

4. To add the Sample Data make sure this configuration is present into composer.json :

"repositories": [
        {
            "type": "composer",
            "url": "https://repo.magento.com/"
        }
    ],

 

4..1 Deploy sample data

bin/magento sampledata:deploy

 This step is a bit misleading as you get this message:

And my initial thought and action was to add my username  from https://magentocommerce.com but it doesn’t work so after a bit of research I found out I need in fact to get the magentocommerce.com > Connect > Secure Keys my Public and Private Key which are looking like this:

or in the new interface at
https://marketplace.magento.com/customer/accessKeys/list/

After that the installation of sample data went smoothly.

 

4.2. After deploy yo have to run:

php bin/magento setup:upgrade

5. Developing in Magento (Set the Mode, enable URN for PhpStorm)

### Developer Enviroment ### 
#Set developer mode
bin/magento deploy:mode:show
bin/magento deploy:mode:set developer

#Generate URN cache (XSDs) for use in PhpStorm:
bin/magento dev:urn-catalog:generate .idea/misc.xml

#Enable Magento_Developer module
bin/magento module:enable Magento_Developer

#Optionally enable profiler in shell
export MAGE_PROFILER=firebug

### For PRODUCTION you would go with this values
Set production mode
bin/magento deploy:mode:set production

#Disable Magento_Developer module
bin/magento module:disable Magento_Developer