How to Install Sample Data in Magento 2?
Magento community people often ask about the Magento sample data. Sample data can be used to test performance and scalability on your store. In this blog, we will see how you can install sample data on your Magento 2 store.
There are three methods to install sample data on Magento 2:
- Install using Magento CLI
- Install by cloning repositories
- Install by composer.json file
Method 1: Install using Magento CLI
- Connect your Magento store by launching the SSH terminal
- Go to the root directory (public_html) of your Magento 2 store.
- To install Magento 2 sample data, run the following command
php bin/magento sampledata:deploy
- Now to authenticate from repo.magento.com, you will need your username and password to start
To find your username and password, log in to your Magento Marketplace Account and Navigate to My Profile.
Under the Marketplace tab, Go to the My Products section and Click Access Keys
If you don’t have access keys, you will have to create new keys. To create, click Create A New Access Key.
You will get two kinds of keys: a public and a private key.
- Public Key: It will be used as your username.
- Private: It will be used as your password.
- Now return to the SSH terminal and enter your username, password and enter ‘Y’:
- Now click enter to start the installation process of Magento 2 sample data.
If you get a memory error message, use the following command.
php -d memory_limit=2G bin/magento sampledata:dep
- After Magento 2 sample data is successfully installed, run the following commands one after one.
php bin/magento setup:upgrade
php bin/magento setup:di:compile
php bin/magento setup:static-content:deploy -f
- Vist your store’s front page.
Now, you will be able to see the sample data.
Now, let’s move ahead and check other methods to install Magento 2 sample data.
Method 2: Install by cloning repositories
Cloning with SSH Protocol
- Visit Magento sample data repository
- Click SSH from the list.
- Click Copy to clipboard
- Visit your web server’s docroot directory.
For Ubuntu, the directory is /var/www &
For CentOS, the directory is /var/www/html.
- Now enter git clone and paste the value copied in the previous step.
For example,
git clone git@github.com:
magento/magento2-sample-data.git
- Wait until the repository is cloned on your server.
- Check the branch of the sample data repository which corresponds with the branch you used from the main Magento 2 repository.
For example,
If the branch of Sample data is 2.3.2, the Magento repository branch should be 2.3.2
Check the correct branch using the command –
$git checkout 2.3.2
- Change to <magento_root>
- Now you will have to create symbolic links between cloned files, use this command to create symbolic links –
php -f <sample-data_clone_dir>/dev/tools/build-sample-data.php — –ce-source=”<path_to_your_magento_instance>”
Cloning with HTTPs Protocol
- Visit Magento sample data repository
- On the right side, click HTTP under the clone URL field
- Click Copy to clipboard
- Change your web server’s docroot directory.
For Ubuntu, the directory is /var/www &
For CentOS, the directory is /var/www/html.
- Now enter git clone and paste the value copied in the previous step.
For example,
git clone git@github.com:magento/magento2-sample-data.git
- Wait until the repository is cloned on your server.
- Check the branch of the sample data repository which corresponds with the branch you used from the main Magento 2 repository.
For example,
If the branch of Sample data is 2.3.2, the Magento repository branch should be 2.3.2
Check the correct branch using the command –
git checkout 2.3.2
- Change to <magento_root>
- Now you will have to create symbolic links between cloned files; use this command to create symbolic links –
php -f <sample-data_clone_dir>/dev/tools/build-sample-data.php -- --ce-source="<path_to_your_magento_instance>"
Set File System Ownership and permissions:
- Navigate to your sample data clone directory.
- Set ownership using command–
chown -R :<your web server group name> .
- Set system permission using command–
find . -type d -exec chmod g+ws {} \;
- Clear cache & update database
Method 3: Install by composer.json file
Add this code to your root composer.json file
For Magento 2.4:
{
"require": {
"magento/module-bundle-sample-data": "100.4.*",
"magento/module-catalog-rule-sample-data": "100.4.*",
"magento/module-catalog-sample-data": "100.4.*",
"magento/module-cms-sample-data": "100.4.*",
"magento/module-configurable-sample-data": "100.4.*",
"magento/module-customer-sample-data": "100.4.*",
"magento/module-downloadable-sample-data": "100.4.*",
"magento/module-grouped-product-sample-data": "100.4.*",
"magento/module-msrp-sample-data": "100.4.*",
"magento/module-offline-shipping-sample-data": "100.4.*",
"magento/module-product-links-sample-data": "100.4.*",
"magento/module-review-sample-data": "100.4.*",
"magento/module-sales-rule-sample-data": "100.4.*",
"magento/module-sales-sample-data": "100.4.*",
"magento/module-swatches-sample-data": "100.4.*",
"magento/module-tax-sample-data": "100.4.*",
"magento/module-theme-sample-data": "100.4.*",
"magento/module-widget-sample-data": "100.4.*",
"magento/module-wishlist-sample-data": "100.4.*",
"magento/sample-data-media": "100.4.*"
}
}
- For Magento 2.3, replace “100.4.*” with “100.3.*”
- For Magento 2.2, Replace “100.2.*” with “100.2.*” and so on.
- Now run command– composer update
- If it asks for a username and password, use the method shown in method 1 to get your username and password.
Your sample data will be installed after following the procedure.
That’s all about the sample data installation process. You can test features and functionalities with the data you want.