How to set up magento 2 with sample data using Vagrant box on Windows 10 with Ubuntu guest
Recommended Ubuntu book
I have been trying different configurations to set up magento 2 with sample data using Vagrant within the last few days. I am using Windows 10 as host machine and now successfully running Ubuntu 14.04 Trusty Tahr on a guest machine with magento 2 and sample data.
Basically, you will need to set up virtual environment first, download VitualBox and Vagrant. VirtualBox lets you create and run virtual machine within your environment, so head to https://www.virtualbox.org/wiki/Downloads, download and install the latest release first. Vagrant lets you create scripts to preinstall and configure all the packages on a virtual machine. Once configured, you can run a virtual machine with the same configuration from anywhere, destroy and re-create a box that suits your development needs in one simple command. So head to https://www.vagrantup.com/downloads.html to download and install Vagrant.
Next comes the configuration part. We have tried many. Magento 2 is complicated, resource hungry and slow as is. Our goal was to set up a shared folder that we can use for development and a virtual box to run the beast. After tinkering around different file systems and configurations, we have found a simple solution with minimal configuration that gave us satisfactory results. We also need to deploy magento 2 sampledata package, which provides some sample data to play with. It was not possible to get it installed with out of the box configurations due to dependency issues, so we made a little adjustment as explained below.
Basically, vagrant-magento2-apache-base by Alan Kent gave us a headstart on the configuration. Please follow https://github.com/alankent/vagrant-magento2-apache-base and clone the repository but do not start Vagrant with vagrant up yet as we need to make additional changes to the configuration. Using default settings resulted in the following error message when trying to install sampledata package:
[ReflectionException]
Class Magento\Catalog\Model\ResourceModel\Product\Attribute\Backend\Media does not exist
Instead of using magento2 folder that has a copy of magento2 from the repo, we will get a fresh copy from official repo.magento.com. In order to do so, you need to be registered or register with magento connect – https://www.magentocommerce.com/magento-connect/. Login, go to “My Account” -> “Developers” -> “Secure Keys”. Get your public and private keys that are used as a username and password to access Magento GitHub repository.
Next, default configuration that we used, has /magento2 folder with magento code. We are not going to use it. Instead, we need to create a new folder to which we will install the latest magento 2 from the repo from the vagrant machine. Create a new folder in the same folder where you placed /magento2 and /vagrant-magento2-apache-base using Alan’s sample configuration. We will call it /magento_code, but feel free to change the name as you wish.
First, lets update vagrant configuration. Go to /vagrant-magento2-apache-base and open configuration – Vagrantfile in text editor. Replace:
config.vm.synced_folder "../magento2", "/var/www/magento2", :mount_options => ["dmode=777","fmode=666"]
with
config.vm.synced_folder "../magento_code", "/var/www/magento2", :mount_options => ["dmode=777","fmode=666"]
respectively to reflect the changes we made to switch shared folder to a blank folder where magento 2 will be installed during the first vagrant setup run.
While we are here, we need to make additional changes to actually install magento 2 on startup. At the end of the file, right after:
config.vm.provision "shell", path: "scripts/install.sh"
add these two lines, changing your repo.magento.com username (public key) and password (private key). Also, head to github.com and grab your github oauth, as you will need it as well:
config.vm.provision "shell", inline: "echo '{\"http-basic\": {\"repo.magento.com\": {\"username\": \"your_magento_connect_username\",\"password\": \"your_magento_connect_password\"}}, \"github-oauth\": {\"github.com\": \"your_github_oauth\"}}' >> /root/.composer/auth.json"
config.vm.provision "shell", inline: "composer create-project --repository-url=https://repo.magento.com/ magento/project-community-edition /var/www/magento2/"
This will install magento2 on startup into /var/www/magento2, which is linked to your empty folder /magento_code
One other change I had to make is adding php5-5.6 repository to vagrant-magento2-apache-base/scripts/install.sh as it is required to run magento2.
Open the file in the text editor and add:
#edit for php 5.6
sudo apt-get install python-software-properties
sudo add-apt-repository ppa:ondrej/php5-5.6
sudo apt-get update
right before #Install MySQL.
Next, open deploy.sh in the same /config folder and add –use-sample-data at the end of install command for magento2, your file should look something like:
#!/bin/sh
# Show what we execute
set -x
cd /var/www/magento2/
# Wipe out all caches
/bin/rm -rf var/*
# Set up, rebuilding database from scratch.
# Port 8080 must match what you expose via Vagrantfile.
php bin/magento setup:install \
--cleanup-database \
--db-host=localhost \
--db-name=magento \
--db-user=magento \
--db-password=secret \
--backend-frontname=admin \
--base-url=http://127.0.0.1:8080/ \
--language=en_US \
--timezone=America/Los_Angeles \
--currency=USD \
--admin-lastname=Smith \
--admin-firstname=John \
--admin-email=john.smith@example.com \
--admin-user=admin \
--admin-password=admin123 \
--use-secure=0 \
--use-sample-data
This is it for configuration. We now follow instructions as described on sample config page and install additions. Open command line, go to /vagrant-magento2-apache-base and enter:
vagrant up
Once the machine is built:
vagrant ssh
Once you are logged in, type:
sudo sh /vagrant/scripts/deploy.sh
This should deploy everything we have predefined.
Next are the set of commands to set up magento 2 as we need it:
cd /var/www/magento2/
php bin/magento setup:static-content:deploy
to deploy static content if the default images on localhost:8080 do not load. Next:
php bin/magento sampledata:deploy
to actually deploy the sample data package. Next:
composer update
to update the dependencies. Next:
php bin/magento setup:upgrade
to upgrade the magento 2 installation with sample data.
This is it. You now should have a fresh magento 2 install with sample data and a virtual box ready for development. Enjoy!