Magento on Windows using Hyper-V

July 9, 2019 - Reading time: 8 minutes

I have been hard at work on a way to effectively work on Magento within Windows 10. Microsoft has come a long way in their quest to be more linux/unix-friendly, however nothing beats the ease of installing linux/unix-native applications in a native environment. My quest brought me to Hyper-V, a Windows 10 Professional Virtual machine application I have used with all manner of Windows virtual machines. This article covers how you can have Magento up and running on Ubuntu within your Windows 10 desktop in roughly an hour.

Installation

Requirements:

  • Windows 10 Professional installed on the target machine. (Home edition cannot use Hyper-V.)
  • A minimum of 8GB of RAM on the system. Ideally, you want 16GB.
  • 30GB+ of free hard drive space.
  • An active broadband connection. The faster the better!
  • Hyper-V installed (Install Guide)
  • A Magento.com user account to create developer access keys for repo.magento.com

Optional:

  • Github.com account with knowledge (or moxie) to setup Personal Access Tokens if you are using Two-Factor Authentication on GitHub

Configuring Hyper-V and Installing Ubuntu:

  • Launch Hyper-V Manager.
  • Under Actions on the right panel, click 'Quick Create'.

  • Select Ubuntu 19.04 under 'Select an operating system'.

  • Click 'Create Virtual Machine'.
  • Once the download completes, Hyper-V will create a new Virtual machine (VM).

Now let's stop and fix something important. One of the biggest issues with the Ubuntu VM out of the box is that the hard drive space allotted for the expandable drive is woefully small at 11GB. If you don't fix this now, once you begin to install Magento, the VM will run out of space. Let's fix that.

  • Right-click the new virtual machine in the main Manager panel. On the right panel under 'Actions', select 'Edit Disk'.
  • In the Edit Virtual Hard Disk Wizard, select Next and browse to the location of the *.vhdx file named after your Ubuntu install and select it.
  • Click Next. Select Expand.
  • Under New Size, enter a size at a minimum of 30GB and select finish.

Important Note: The available partition space for the VM will now be 30GB. However, Ubuntu will still assume it is using the default physical space assigned to it with Windows' VM settings since we haven't told Ubuntu it can use the new space. We'll fix that later with an app called gparted.

Booting the Ubuntu VM

In the Hyper-V Manager, right-click the Ubuntu VM and select 'Connect'. The VM will boot up and begin configuration. Configure as you desire, but the most important detail with configuration is to remember: do not select 'Connect automatically' on the screen where you enter login credentials during configuration. Doing so will cause the VM to refuse to boot while in Enhanced Boot mode (the default mode).

Allow Ubuntu to reboot and login using your Ubuntu account credentials to the entirely out-of-place xrdp session panel. Once the OS successfully boots to the desktop, it would be a good idea to create a Hyper-V checkpoint in the event something goes wrong so you can restore and start again.

A quick note about Hyper-V VM checkpoints... this creates an extra file that will be used by the VM next to the *.vhdx in your Windows file system that can grow to a size equal to your *.vhdx file. If you are strapped for space, use them sparingly.

Configuration

Configuring Ubuntu

Once at the desktop, let the Ubuntu install update repository sources. Super important. The prompt to do so should appear within a couple of minutes. Enter your password and download updates. Let the updates install and reboot Ubuntu to be sure everything is installed correctly.

Let's go ahead and take care of that partition issue before we go much farther. Install gparted from the Software application (access via search from 'Activities'). if it is not available, you can install it manually. Use gparted to expand the primary partition to fill the entire available space.

At this point, install any IDEs you use for code editing using the software center or any manual terminal install instructions available for your chosen IDE. Ubuntu is Debian-based, so you can use most environment-agnostic Debian install instructions as well as Debian standalone installers.

Getting Ready for Magento

We're on the home stretch now! Next up, we'll install Docker CE, get our containers from DockerHub, and install Magento 2.x.

Installing Docker

We're going to use Docker to handle our Magento containers so we don't have to go through the process of getting php, sql, etc. installed. Docker also containerizes the services, which allows you to experiment with configurations if you're feeling saucy.

Digital Ocean has a great tutorial to get you up and running with Docker. This is written for 18.04, but it also works with 19.04. I recommend using the Alternate Version if you want to use this guide. One thing that tripped me up in subsequent steps was the lack of `docker-compose` availability. You need to install this separately and here is a guide for that.

Getting our containers

We're going to be using Docker Magento by Mark Shust for our Docker experience. This is well documented, feature-rich, and very easy to use for even the novice Magento developer. Please take a moment to peruse the repository documentation `readme.md` file.

Ready? Open terminal. Create a folder called `repos` (or whatever you call your repo storage) within the user space. Create a folder called `magento` within that. In terminal, use `cd repos/magento'. Either download a zip of the Docker Magento repository or use your Github.com account and git to download the repo to the `magento` directory. This is where you will need to use your GitHub.com username/password or a Personal Access Token if you have Two-Factor authentication enabled.

Now that we have the docker *.yml (yaah-mail) files, we can pull our containers. Ensure docker is running using `docker info`. If not, start docker using `docker restart`. Per the repo's DockerHub page, use `docker pull markoshust/magento-nginx` to retrieve the Magento containers for use.If you ran this command from the `magento' repo directory, it should ready the *.yml files there and grab the containers! You should see quite a bit of activity if this works as expected.

Once the containers are downloaded and the prompt is available, start them using `'docker-container start`.

Installing Magento

Now that we have our repo and containers, we're ready to install Magento! Run the following command at the termianl prompt in `repos/magento`, per documentation in the repo:

`curl -s https://raw.githubusercontent.com/markshust/docker-magento/master/lib/onelinesetup | bash -s -- magento2.test 2.3.2`

Let's break down what is happening here beyond the URL curl call:

  • `magento2.test` is the URL you want to access your install from. Change this to whatever you want to use, but avoid using *.dev domains.
  • `2.3.2` is the version of Magento you want to install. Keep this at the version in the readme to ensure no conflicts with the docker containers.

Once this completes, open your browser to the URL chosen for the install and Magento should load without issue!

Optional Configurations and Notes

  • If you want to use Windows 10's native browsers to access the docker container in the VM, first you need to know the IP of the Ubuntu VM. Open Powershell in Windows 10 as an Administrator and run `Get-VM | Select-Object -ExpandProperty NetworkAdapters | Select-Object VMName,IPAddresses`. Note the IP address of the VM name matching the VM running in the Hyper-V Manager. Open your hosts file in Windows 10 (`[OS drive]/System32/drivers/etc/hosts`) and add `[VM IP] [Magento URL]` as a new entry there. (ex. 0.0.0.0     magento2.test)
  • Use Git. Track the `magento` folder. Make a feature branch and watch it as you make changes. Learn what code changes when you perform updates to core functions like composer so you aren't chasing your tail on updates.
  • If you are going to work with frontend development, Grunt is your friend for styling updates. Research it and learn to use it and love it. It will save you an immense amount of time compiling LESS files.This is pre-installed with your docker containers, so see the documentation on how to use it.
  • Since composer is installed in the docker container, you can use it to augment your install quickly and easily. I recommend considering mage2tv's cache-clean as the first composer addition to the install. Follow the documentation there and use composer as documented in the Docker Magento... documentation.
  • Watch your virtual machine memory usage within the Hyper-V Manager. If you see the allotted memory max out and still see slow page refreshes and cache flush response from Magento, consider allocating more memory to the VM and/or adding more system memory. (Magento 2 can be quite a monster on memory usage 0 especially when using the bin/magento command line utilities!)
  • Finally, I find that the refresh rate for my code editor IDE (Sublime Text) is very good. However, if you want to use an code editor in Windows 10, set the `magento` folder up as a share between the VM and Windows 10 and edit semi-remotely.

In conclusion

I hope you found this guide useful and now have a working Ubuntu VM with a working, fast Magento 2 installation. It took me a few attempts to get my environment right where I want it to be, but I am now able to operate within Windows 10 and preview my changes freely within Windows 10.

Feel free to let me know if you have any questions!

About


Hi there! My name is Kevin Earl Gardner. I am a Web Developer and (very) Amateur Content Creator based out of Birmingham, Alabama. I like to blog about coding and streaming.