How to create Virtual Machine[VM] on Windows 10 using Vagrant/Virtual Box.
In this exercise, we will see the following sections/topics/activities:
Let us consider the below two activities.
I. How to Install Vagrant and VirtualBox ?
II. How to Initialize our first virtual environment with Vagrant setup ?
I. How to Install Vagrant and VirtualBox ?:
Step1: Selecting the Virtual Box
Initially, we need to install the software required to manage a virtual machine
environment, which is a hypervisor as well as the Vagrant software itself.
In this activity, we will install VirtualBox to use it with Vagrant.
VirtualBox is an open source hypervisor that was initially made as the only
hypervisor and supported by Vagrant.
The VirtualBox is broadly supported by the Vagrant community.
Before we install the VirtualBox and Vagrant software, we need to obtain its latest versions.
VirtualBox can be downloaded from its project website at http://virtualbox.org.
Please note, in this exercise we are going to use for Linux installations [VMs] only. Also note, in this exercise I am using Windows10 as OS. Hence I need to select for my use the windows version under Windows hosts.
When we click on it, it downloads the file named as “VirtualBox-5.2.16-123759-Win”, as on date this is the file exists. [it might vary time to time]. You can install it on your desktop/Laptop now.
A new installation of VirtualBox will display a welcome message in a window titled as
“Oracle VM VirtualBox Manager”.
When you have created more than one virtual machines, this dialog displays information about the machines created using VirtualBox.
Step2: Getting Vagrant to operate the Virtual Box
Now, we need to get the Vagrant file to install.
Vagrant packages are operating system-specific and can be downloaded from the
Vagrant website at http://vagrantup.com.
Download the appropriate version for your system [example: Windows10].
You can find, Windows 32-bit or 64-bit. In my current Laptop, I use 64 bit.
You can find the file as “vagrant_2.1.5_x86_64” to download the same.
Now install it after its download.
Vagrant setup wizard will drive you with it easy guidelines.
It installs by default into:C:\HashiCorp\Vagrant\
It takes few minutes to copy the files and install into your Desktop/Laptop.
Step3: Identify the Vagrant installation
Now, It will ask you to reboot your machine. Once you rebooted your system, please follow the below steps.
How Vagrant works?:
Let us understand; Vagrant followed the below procedure in the above Step2.
The Vagrant installer extracted and copied its files, and added the vagrant command to the executable path.
On Windows OS, this will install Vagrant to the default OS X Applications/ directory.
Also let us note; Vagrant is a command-line driven application, there are no programs
accessed from this OS.
Now, by opening a command prompt [CMD in windows] let us verify that Vagrant is working. We need to do it by executing: “vagrant version” command in CMD.
We can see the below screen output in CMD window:
=== CMD window output ====>
Installed Version: 2.1.5
Latest Version: 2.1.5
You’re running an up-to-date version of Vagrant!
From the above output;
It is confirmed, we are ready to start using Vagrant!
Note: Please visit https://docs.vagrantup.com/v2 , to know further the Vagrant installation guidelines.
Let us recap, What we have done is; we installed a working Vagrant environment which consists of:
a) A hypervisor application that can contain virtual machines.
b) Also let us note; Vagrant is a tool that makes managing these machines simpler and available.
c) It is important here to note that Vagrant is simply a framework to manage virtual machines.
d) It is not an application to create and host virtual machines.
II. How to Initialize our first virtual environment with Vagrant setup ?:
As mentioned in the above sections, once we have a working Vagrant environment with a hypervisor, we can initialize our first environment.
There are two ways we can work with Vagrant to achieve this task:
a) In a new environment with a newly initialized Vagrantfile.
b) In an environment which is maintained in source control system (such as Git, SVN, etc.), which has a Vagrantfile included in a project and keeping Vagrantfiles for different purposes of the projects.
This is a powerful technique to manage and track changes in Vagrant environments. This kind of method can be more useful to build the Environments easily with the concept of Infrastructure As a Code [IAC] under DevOps culture. Which is also a script driven [automated IAC] process.
But in this example, we will initialize a new environment with the basic configuration of a Vagrantfile.
Step1: Let us create a vagrantfile with vagrant init command as below to get an ubuntu 16.04 machine with 32 bit.
I have copied my screen display with used commands for this process.
I need to have a different folder and used the below command in that folder”.
vagrant init puppetlabs/ubuntu-16.04-32-nocm
=== Screen output ===>
C:\>md dir vagrant-files
C:\vagrant-files>vagrant init puppetlabs/ubuntu-16.04-32-nocm
A `Vagrantfile` has been placed in this directory. You are now
ready to `vagrant up` your first virtual environment! Please read
the comments in the Vagrantfile as well as documentation on
`vagrantup.com` for more information on using Vagrant.
==== We have created a vagrantfile now===>
Step2: Making Vagrant up
Before running this step please make sure you have installed the Oracle VirtualBox as mentioned in the previous section.
Now, In Command Window execute the below command.
This command might output several results; I am copying my screen output which was displayed. It takes few minutes to complete this activity.
=== Screen output for vagrant up=====>
Bringing machine ‘default’ up with ‘virtualbox’ provider…
==> default: Box ‘puppetlabs/ubuntu-16.04-32-nocm’ could not be found. Attempting to find and install…
default: Box Provider: virtualbox
default: Box Version: >= 0
==> default: Loading metadata for box ‘puppetlabs/ubuntu-16.04-32-nocm’
default: URL: https://vagrantcloud.com/puppetlabs/ubuntu-16.04-32-nocm
==> default: Adding box ‘puppetlabs/ubuntu-16.04-32-nocm’ (v1.0.0) for provider: virtualbox
default: Downloading: https://vagrantcloud.com/puppetlabs/boxes/ubuntu-16.04-32-nocm/versions/1.0.0/providers/virtualbox.box
default: Download redirected to host: s3.amazonaws.com
==> default: Successfully added box ‘puppetlabs/ubuntu-16.04-32-nocm’ (v1.0.0) for ‘virtualbox’!
==> default: Importing base box ‘puppetlabs/ubuntu-16.04-32-nocm’…
==> default: Matching MAC address for NAT networking…
==> default: Checking if box ‘puppetlabs/ubuntu-16.04-32-nocm’ is up to date…
==> default: Setting the name of the VM: vagrant-files_default_1536967979621_87087
Vagrant is currently configured to create VirtualBox synced folders with
the `SharedFoldersEnableSymlinksCreate` option enabled. If the Vagrant
guest is not trusted, you may want to disable this option. For more
information on this option, please refer to the VirtualBox manual:
This option can be disabled globally with an environment variable:
or on a per folder basis within the Vagrantfile:
config.vm.synced_folder ‘/host/path’, ‘/guest/path’, SharedFoldersEnableSymlinksCreate: false
==> default: Clearing any previously set network interfaces…
==> default: Preparing network interfaces based on configuration…
default: Adapter 1: nat
==> default: Forwarding ports…
default: 22 (guest) => 2222 (host) (adapter 1)
==> default: Booting VM…
==> default: Waiting for machine to boot. This may take a few minutes…
default: SSH address: 127.0.0.1:2222
default: SSH username: vagrant
default: SSH auth method: private key
default: Warning: Connection reset. Retrying…
default: Warning: Remote connection disconnect. Retrying…
default: Warning: Connection aborted. Retrying…
default: Vagrant insecure key detected. Vagrant will automatically replace
default: this with a newly generated keypair for better security.
default: Inserting generated public key within guest…
default: Removing insecure key from the guest if it’s present…
default: Key inserted! Disconnecting and reconnecting using new SSH key…
==> default: Machine booted and ready!
==> default: Checking for guest additions in VM…
default: The guest additions on this VM do not match the installed version of
default: VirtualBox! In most cases this is fine, but in rare cases it can
default: prevent things such as shared folders from working properly. If you see
default: shared folder errors, please make sure the guest additions within the
default: virtual machine match the version of VirtualBox you have installed on
default: your host and reload your VM.
default: Guest Additions Version: 5.0.20
default: VirtualBox Version: 5.2
==> default: Mounting shared folders…
default: /vagrant => C:/vagrant-files
=== End of Vagrant up process ====>
Please note with the above activity is completed, your VirtualBox window will show this VM as its status “running”. It means you are able to see this newly created VM in your “Oracle VM VirtualBox Manager”.
Step3: Checking the box
Now let us use check this VM in a Command line:
After Vagrant returns to the command line, executing the “vagrant ssh” command will open a command-line interface into the newly initialized Virtual Machine[VM].
You can see the screen output:
==== vagrant ssh ====>
Welcome to Ubuntu 16.04 LTS (GNU/Linux 4.4.0-21-generic i686)
* Documentation: https://help.ubuntu.com/
New release ‘18.04.1 LTS’ available.
Run ‘do-release-upgrade’ to upgrade to it.
At the same time you can also go to your “Oracle VM VirtualBox Manager”, and double click on it. It will show a button “Show”, you can click on it. Opens a window with this VM.
Please note; In this exercise we have created the base box only. Hence we are not going to use this VM for our future operation.
You can see the local dir as:
== Current dir details ===>
Volume in drive C has no label.
Volume Serial Number is 5A33-A94E
Directory of C:\vagrant-files
09/14/2018 04:14 PM <DIR> .
09/14/2018 04:14 PM <DIR> ..
09/14/2018 04:25 PM <DIR> .vagrant
09/14/2018 04:12 PM 3,108 Vagrantfile
1 File(s) 3,108 bytes
3 Dir(s) 96,101,879,808 bytes free
Step4: Destroying the Virtual box
Now, we will see : a) How to exit, b) How to Destroy this VM.
a) How to exit: We can exit it by using the command :
“control-d command, or by typing exit”
b) How to Destroy this VM: We can Destroy the virtual machine, by discarding the entire working environment. This can be done with the “vagrant destroy” command.
When you use this command, Vagrant will now prompt you to make sure that you want to destroy the environment; type “y” to proceed with destroying the environment and deleting the VM.
=== Output for Destroy===>
VM must be running to open SSH connection. Run `vagrant up`
to start the virtual machine.
default: Are you sure you want to destroy the ‘default’ VM? [y/N]
==> default: Destroying VM and associated drives…
You can also check in the GUI of the “Oracle VM VirtualBox Manager”. It will not have this VM.
In this simple vagrant exercise in summary we have done the below steps;
- We have downloaded and configured “Oracle VM VirtualBox Manager”.
- We have installed Vagrant.
- Created a Vagrant virtual box.
- We could connect to it in a command line interface and checked the linux [ls] commands. We also cross verified it in “Oracle VM VirtualBox Manager” on its appearance as VM.
- Finally, we destroyed this VM.
That is all about this exercise. You can create any number of VMs like this.
You can keep checking this site or follow it to get the new/updated blogs.
If you are interested in learning AWS, go through my blogs: