Running Linux on Windows with Vagrant

Testing Linux software on Windows with Vagrant

There is no magic, you will not able to run a Linux software natively on Windows without third-parties addon.

But thanks to a lot of vendor, you have virtualization tools available for doing your tests.
Everything that has been tested and presented on this site, if not done directly on physical Linux host, has been done with the help of a virtualization software.
Even if your host OS is Linux, there are advantages to use the virtualization:
  •  you can use different version of your distribution
  •  you can use a different Linux distribution
  •  you can run Windows on Linux (or Linux on Windows)
  •  you can use pre-build images, no need to install everything from scratch yourself
  •  you can use snapshots to save intermediate states of your VM and revert to it if you did some fatal mistakes during your tests
  •  you just need to run what you need for your test, then stop it and archive it if you want to use it again after some time.
On Linux, (free) various tools are available:
  • Dockers and LXC, to create containers. This will created isolated Linux guest on top of your Linux host.
  • Open solutions like KVM and QEMU to run a full virtualization, so you can run Windows guests on top of Linux
  • Proprietary solutions like Vmware Workstation (free version just allows you to run already build Vmware images) and Oracle VirtualBox. As VirtualBox is (still) free for personal use, this became our platform of choice to perform our tests.
On Windows, we have continued to use Oracle VirtualBox. We were able to reuse image created on Linux on our Windows installation of VirtualBox.

But on Windows 10, there are other possibilities:
  1. To have a full non-Windows guest OS, you can use VirtualBox. Once installed, you have a nice GUI to create any kind of virtual machines. Once you have created a configuration and started it, you'll have to do the OS installation using the physical medium or a CD-ROM ISO file attached to the VM. You can use the "clone" function of VirtualBox to create a copy of an existing VM that you can confgure differently. VirtualBox has a build-in DHCP server, so your VM can receive non-overlapping IP automatically. There are various way to connect the VM to the network, the most straight forward is to use the NATing that will allow the machine to access the network outside your host using it's IP and network connection. You can define internal-only virtual network, so your VM can be reach from the host over its virtual network adapter.    
  2. You can install on Windows 10 a Linux subsystem.
  3. You can enhance VirtualBox with Vagrant, another Open Source project.
    

Let's use the Linux subsystem

It will act more like a Wine in Linux, but with an Ubuntu 14.04 LTS OS inside. It is the result of a collaboration between Microsoft and Canonical (the company behind Ubuntu Linux).
In this system, because it is a full Linux system based on Bash and Ubuntu running in Windows thus without the underlying Linux kernel, you will be able to run a lot of command lines tools in it. You won't be able to run graphical and server software.
To activate this feature, you will have to follow these steps:
  1. You need to have the Windows 10 Anniversary Update installed. This only works on 64-bit builds of Windows 10.
  2. Open the Settings app and go to "Update & Security" -> "For Developers". Activate the "Developer Mode" switch there to enable the Developer Mode.
windows_bash_1.png
  1. Open the Control Panel, go into "Programs" -> "Programs and Features" -> "Turn Windows Features On or Off" . Enable the "Windows Subsystem for Linux (Beta)" option in the list here and click "OK".
windows_bash_2.png
  1. You are prompted to reboot your Windows computer
  2. After the reboot, click the "start" button, type "bash" and hit "return"
windows_bash_3.png
  1. The first time this command is run, you are prompted to accept the terms of service
  2. The application starts automatically to download "Bash on Ubuntu on Windows" from the store
windows_bash_4.png
  1. You are prompted to create a user and password for the Bash environment (this user & password will be totally independent of the one you use in Windows)
When you are at the Bash prompt, you can run any Linux commands or install new one with "apt-get". No need to use "sudo", as the shell you got is already running as root. You Windows filesystems are located under /mnt/c, /mnt/d, ...    
windows_bash_5.png


Let's use Vagrant on Windows

Vagrant is not a virtualization software, but a software that will automate the provisioning of new virtual machines in the underlying virtualization software.
Vagrant can retrieve an already build VM image on any repository or public catalog and deploy it without human intervention on your host. You just need to know from where to fetch images, and which images are available.
First, we will install it
  1. Download it from the web site https://www.vagrantup.com/downloads.html and save the MSI file.
  2. Install it (just double-click on the MSI file from where you saved it)
    1. Click "Next"
    2. Accept the license and "Next"
    3. Choose a directory for the installation and "Next"
    4. Click "Install"
    5. Because Administrator rights are needed to install it, you will be prompted to let the installer perform changes on your system. Click "yes"
  3. Click "Finish" and you will be prompted to restart your computer to complete the installation. Let’s reboot thus.

Now we are going to test our installation by launching a VirtualBox machine with Centos 7. We need to look in the public repository if a Box exists for the OS we want to run, if it supports VirtualBox and how to launch it.
In Vagrant terminology, a Box is a virtual image packaged to be automatically run by Vagrant. It contains all the needed parameters to setup your VM in your local virtualization software.
  1. Look for a Vagrant Box you want to run locally on the public catalog.
    1. Filter on VirtualBox, as this is our local virtualization software
    2. Let’s look for CentOS 7, click on the name or go directly to this URL : https://atlas.hashicorp.com/centos/boxes/7
    3. Look for the instructions for VirtualBox, that is : vagrant init centos/7; vagrant up --provider virtualbox
Then let’s launch this Box
  1. Open a Windows Command prompt
  2. Type vagrant init centos/7 and read the output, it should be mentioned that a vagrantfile was created
vagrant_init.PNG
  1. Type vagrant up –-provider virtualbox. This will start up the virtual machine defined in the vargrantfile that was just created. If you plan to start various Vagrant Boxes at the same time, you will have to initiate the vargrant init command in separate directories.
  2. The download of the image start. Once the process is finished, you should see something similar on your screen
vagrant_up-final.PNG
  1. If you have a look into your VirtualBox interface, you now have a new VM listed
vagrant_vbox_result.PNG
  1. Launch this VM as you do for other VirtualBox VM.