Magento 2 Sample Data

- Magento 2

Sample Data for Magento 2

If you don’t know what is the Magento 2 sample data, then you’ve come to the right place. Below, we will explain why is it useful and how to leverage it on the platform. Using the Magento 2 sample data is the most convenient way to get a storefront with products, categories, and other data. You get a store which is based on the Luma theme and functions just like a default Magento website – a perfect opportunity to learn how to manage prices, pricing rules, inventory, etc. In other words, with the Magento 2 sample data, you can easily master the Magento 2 Admin.

And it is possible to install sample data even before installing the Magento 2 software. Of course, you can do it right after.  Also note that the Magento 2 sample data can be either removed or freshed.

There are two ways of installing optional Magento 2 sample data: via Composer and by installing cloning repositories.

If you decide to use Composer, don’t forget to change the root composer.json file of Magento 2 to enable sample data modules. This approach requires understanding Composer and accessing the Magento 2 file system.

In case you are going to clone the repository of Magento 2, note that it is also necessary to clone the sample data repository as well as create symbolic links. This method is intended for contributing developers only.

Installing Magento 2 sample data via Composer

Below, we shed light on the installation of the Magento 2 sample data if your Magento software has been downloaded as a compressed archive or via the composer create-project command.

The method is suitable for both Magento Community Edition and Enterprise Edition.

Magento 2 sample data prerequisites

The only requirement in this section is related to Magento’s composer.json. It should be able to get components from repo.magento.com. In order to achieve this goal, you should:

  1. be logged as the Magento file system owner into the Magento server;

  2. clone the GitHub repository of Magento 2;

  3. run the composer install command to update dependencies.

  4. change to the installation directory of your Magento 2 (it can be /var/www/html/magento2)

  5. require the https://repo.magento.com repository with the sample data code:

Installing Magento 2 sample data via the command line

Now when you are logged as the Magento file system owner, you can install the Magento 2 sample data via the command line:

Please note that it is necessary to authenticate to complete the action.

If you see the following authentication error

it is necessary to change to your Magento 2 installation directory and then run the following command:

Please note that it will prompt you for your authentication keys.

Installing Magento 2 sample data by cloning repositories

The following section of the article describes the Magento 2 sample data installation by cloning the Magento 2 GitHub repository. Use this method only if you are a contributing developer. Otherwise, rely on the method described above.

And even if you are a contributing developer, there are two strict requirements: 1) you should use Magento CE, 2) you should clone only the Magento 2 develop branch.

It is also necessary to mention that there are two types of Magento 2 sample data. The more current one – the develop branch; and more stable – a released branch. As mentioned above, Magento 2 contributors should use the develop branch.

In both cases, it is necessary to clone the corresponding branch of the Magento 2 GitHub repository. For instance, the develop branch sample data can be used only with the appropriate branch – Magento 2 develop branch.

Cloning the Magento 2 sample data repository

You can install the Magento 2 sample data by cloning the sample data repository with the SSH protocol or with the HTTPS protocol.

SSH

If you’ve decided to use the SSH protocol for cloning the Magento 2 sample data GitHub repository, perform the following actions:

  • Open your browser and visit the Magento 2 sample data repository.

  • Choose SSH from the list of available options situated next to the ‘Find file’ button.

  • Click ‘Copy to clipboard’ which is right after the URL.

Magento 2 Sample Data

  • Now, it is necessary to change to the docroot directory of your web server. In case of Ubuntu, it often has the following structure /var/www. For CentOS, it can be /var/www/html.

  • The next step requires entering git clone and pasting the value from step 1. The official documentation provides the following example:

  • The process of the repository cloning will take a few minutes. In case of the error displayed below, make sure you’ve shared the SSH key with GitHub:

  • Now change to the following directory:

  • And run this command:

If everything is ok, you will have to set up file system permissions. The procedure is described after the HTTPS section.

HTTPS

If you’ve decided to use the HTTPS protocol for cloning the Magento 2 sample data GitHub repository, perform the following actions:

  • Choose SSH from the list of available options situated next to the ‘Find file’ button.

  • Click ‘Copy to clipboard’ which is right after the URL.

Magento 2 Sample Data

  • Now, it is necessary to change to the docroot directory of your web server. In case of Ubuntu, it often has the following structure /var/www. For CentOS, it can be /var/www/html.

  • The next step requires entering git clone and pasting the value from step 1. The official documentation provides the following example:

  • The process of the repository cloning will take a few minutes. If everything goes without errors, change to the following directory:

  • And run this command:

  • The official documentation provides the following example:

The completion of the command will take several minutes. Then you can start the next section.

How to set up file system ownership and permissions

Since the php build-sample-data.php script is responsible for creating symlinks between the repositories with sample data and Magento 2, it is required tj set file system permissions and ownership related to the Magento 2 sample data repository; otherwise, you won’t access the storefront. In order to do so, perform the following actions:

  • First of all, you should change to your clone directory with sample data and set ownership:

In case of CentOS, use chown -R :apache .; in case of Ubuntu – chown -R :www-data .

  • Now, you should set permissions:

In case you must enter the commands as sudo, the command looks as follows:

  • The last step requires clearing static files:

Sources

The following guide is based on the following official documents: