Learn 3 Different Ways to Install Sample Data in Magento 2
Magento 2 sample data is a test sample data based on the Luma theme with sample products, categories, customers’ data, etc.
The objective of the sample data after you have installed Magento 2 is to use it as a test environment where you can tweak the orders, and prices, add products, manage inventory, test the pricing rules, and so on. Let’s learn the different ways to install sample data in Magento 2.
There are two methods to install Magento 2 sample data:
You can also bookmark this Magento 2 sample data installation guide for future reference to install sample data in Magento 2.
Methods to install Magento 2 Sample Data:
-
Install using Magento CLI
- Export Magento CLI’s path with the below command
1export PATH=$PATH:$(pwd)/bin - Change your current directory to Magento’s Webroot
1magento sampledata:deploy - If you get the URL authentication error to repo.magento.com, execute the “composer update” command. You’d require the username and password from the Magento Connect Account section.
- Recompile the code and clear the cache
1magento setup:upgrade
- Export Magento CLI’s path with the below command
-
Install by cloning repositories
- Cloning with SSH Protocol
- Visit the Magento sample data repository
- Next to the name of the branch, click SSH from the list.
- Click Copy to clipboard
- Go to your web server’s docroot directory. For Ubuntu, the directory is /var/www and for CentOS it’s /var/www/html.
- Enter
git clone
and paste the value from the previous step. For example,
git clone [email protected]:magento/magento2-sample-data.git - Wait for the repository to clone on your server.
- Check out the branch of the sample data repository that corresponds with the branch you used from the main Magento 2 repository.
For eg: if the branch of the Magento repository is 2.3.3 develop, the sample data branch should be 2.3.3
You can check the correct branch using the below command from the sample data repository’s root directory – $git checkout 2.3.3 - Change to <magento_root>
- Create symbolic links between the files you just cloned with the below command:
1php -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
- Click HTTP under the clone URL field on the right side
- Click Copy to clipboard
- Change to your web server’s docroot directory. For Ubuntu, it’s
/var/www
and for CentOS it’s/var/www/html
. - Enter
git clone
and paste the value from the previous step. For example,
git clone [email protected]:magento/magento2-sample-data.git - Wait for the repository to clone on your server.
- Make sure to check out the branch of the sample data repository that corresponds with the branch you used from the main Magento 2 repository.
For eg: if the branch of Magento repository is 2.3.3 develop, the sample data branch should be 2.3.3
You can check the correct branch using the below command from the sample data repository’s root directory – git checkout 2.3.3 - Change to <magento_root>
- Create symbolic links between the files you just cloned with the below command:
1php -f <sample-data_clone_dir>/dev/tools/build-sample-data.php -- --ce-source="<path_to_your_magento_instance>"
- Cloning with SSH Protocol
Set File System Ownership and permissions:
- Go to your sample data clone directory
- Set ownership
1chown -R :<your web server group name> . - Set system permission
1find . -type d -exec chmod g+ws {} \; - Clear cache
- Update database
3. Install by composer.json file
You can follow these steps to install sample data in Magento 2 by composer.json:
- Add following code to your root composer.json file.
- For magento 2.4.*
123456789101112131415161718192021222324{"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.*” in the above code. - For magento 2.2
Replace “100.2.*” with “100.2.*” and so on.
- Now pass the below command
1composer update - If it asks for a username and password, then follow the following guide:
- Where the public key is username
- The private key is the password
- For magento 2.4.*
That was all about Magento 2 sample data installation. Once you have installed sample data in Magento 2 store, you can test the functionalities with the test data as much as you want.
If you have any doubts about the steps to install sample data in Magento 2, mention them in the Comments section below. I’d be glad to help you.
Do not forget to share the tutorial via social media.
Thank you.
Still need help? Hire our Adobe-certified Magento experts.
Sanjay Jethva
Sanjay is the co-founder and CTO of Meetanshi with hands-on expertise with Magento since 2011. He specializes in complex development, integrations, extensions, and customizations. Sanjay is one the top 50 contributor to the Magento community and is recognized by Adobe.
His passion for Magento 2 and Shopify solutions has made him a trusted source for businesses seeking to optimize their online stores. He loves sharing technical solutions related to Magento 2 & Shopify.
2 Comments
The following message is appearing to me after following all the steps on step 1 (install with cli)
An error has happened during application run. See exception log for details. Could not write error message to log. Please use developer mode to see the message.
Hello,
The actual error will display if the developer mode gets on.
Set ‘MAGE_MODE’ => ‘developer’ in app/etc/env.php file
Thank You