1. Next I will explain how to install Vagrant for each of the three OS
But first let's download Vagrant by going to https://www.vagrantup.com, choose the installer that matches your OS and install it. After this, some Vagrant plugins must be installed, plugins that will make our life a lot easier when we want to run the VirtualBox machine.
Click the Start button type cmd and open the windows command prompt and run:
vagrant --version
to check that vagrant has been successfully installed.
To automaticaly set the size of the display for the virtual machine and enable bidirectional copy/paste and drag and drop from host to guest and vice versa, run:
vagrant plugin install vagrant-vbguest
To be able to increase the disk size of our virtual machine, which by default has 10GB of disk space, run:
vagrant plugin install vagrant-disksize
Open terminal and run the same command lines as in Windows.
Check if vagrant has been successfully installed:
vagrant --version
To automaticaly set the size of the display for the virtual machine and enable bidirectional copy/paste and drag and drop from host to guest run:
vagrant plugin install vagrant-vbguest
To be able to increase the disk size of our virtual machine, which by default has 10GB of disk space, run:
vagrant plugin install vagrant-disksize
Open terminal and run the same commands.
Check if vagrant has been successfully installed:
vagrant --version
To automaticaly set the size of the display for the virtual machine and enable bidirectional copy/paste and drag and drop from host to guest run:
vagrant plugin install vagrant-vbguest
To be able to increase the disk size of our virtual machine, which by default has 10GB of disk space, run:
vagrant plugin install vagrant-disksize
2. Next, we will install VirtualBox
We do this by going to this link and choose the installer that matches your OS.
If you want to learn more about Vagrant, check the official website .
So, let’s edit the Vagrantfile we have just created so that we can set up a VirtualBox machine with a GIS micro-environment.
What we'll install:
Copy/Paste the content below (between the hashtag lines) to the Vagrantfile inside the folder Project or download Vagrantfile from github.
Please also read the commented lines (the ones that have # at the begining of the line) in this file!
###############################################################
# -*- mode: ruby -*-
# vi: set ft=ruby :
#We will add the 18.04 version of ubuntu
Vagrant.configure("2") do |config|
config.vm.box = "ubuntu/bionic64"
#And change the disk space from 10 GB to 30 GB
config.disksize.size = "30GB"
#We will share the Downloads folder from the Windows host machine with Ubuntu guest OS #from our virtual machine
#Make sure to change this path with the one coresponding to your computer !!!!!!!!!!!!!!!!!!
#This will help us share the Downloads folder from the Windows machine with the virtual #machine. But on the guest machine the Downloads folder will be found as vagrant_data
#for Windows OS!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Comment out next line if you are using macOS or Ubuntu!!!!!!!!!!
config.vm.synced_folder "C:/Users/your_user_name/Downloads", "/vagrant_data"
#For macOS and Ubuntu!!!!!!!!!!!!!!!!!!!!! Uncomment next line if you are using macOS or Ubuntu!!!!!!!!!
#config.vm.synced_folder "/Users/your_user_name/Downloads", "/vagrant_data"
#This is where we pass the bootstrap.sh file so that Vagrant can run install all we need in the #machine.
config.vm.provision :shell, path: "bootstrap.sh"
# Add the VirtualBox machine:
config.vm.provider "virtualbox" do |vb|
# Display the VirtualBox GUI when booting the machine
vb.gui = true
# Set the amount of memory allowed for the VM:
vb.memory = "2048"
# set cpu’s
vb.cpus = 2
#Enable biderectional “copy/paste” and “drag and drop”
vb.customize ['modifyvm', :id, '--clipboard', 'bidirectional']
vb.customize ['modifyvm', :id, '--draganddrop', 'bidirectional']
#Change video memory to 128MB
vb.customize ['modifyvm', :id, "--vram", "128"]
end
end
###############################################################
4. Lets create the bootstrap.sh file!
In the bootstrap.sh file we will write all the linux commands that normaly we run one by one in terminal, which can be very tedious if we have to do it everytime we create a new machine. Here is where Vagrant is very helpfull, it does the job for us.
The bootstrap.sh file has to be in the same folder with the Vagrantfile!
Copy/Paste the content below (between the hashtag) in a txt file and save it as bootstrap.sh or download the bootstrap.sh file from github.
Please also read the commented out lines inside the bootstrapt.sh file!
###############################################################
#!/usr/bin/env bash
5. Lets run vagrant up!
So now you should have 2 files in the folder Project: Vagrantfile and bootstrap.sh
Make sure that in your shell/terminal you are inside the folder Project !
You are now ready to run vagrant up from command prompt/terminal !
The installation is going to take some time to complete, depending also on the resources of the host machine.
If everything went all, you should see a black screen inside the VirtualBox machine. No worries, everything is ok, you just have to run vagrant reload on the shell/terminal. It will be necessary to run this command twice, first time virtualbox guest addition will be installed and second time to be able to login the vagrant machine.
The credentials to login into the machine are:
username: vagrant
password: vagrant
6. Create our own box package.
But what if we have developed things in our virtual machine, we installed GeoServer, for example, and we don't want to lose the work, the files inside the machine and we want to have everything on an external hard disk?
The answer is createing our box package!
If we go back to our Vagrantfile we see that we are using this box config.vm.box = "ubuntu/xenial64", but instead of ubuntu/xenial64 we want our own box package developed on top on ubuntu/xenial64 with GeoServer and all our files.
What we need to do is enter in the folder Project and run vagrant package . This will create a file named package.box that will contain everything we have installed and developed inside the virtual machine, including all we have installed using the bootstrap.sh file. So we will not have to run everything that is in that file with the new box we create.
What we need to do next is copy the folder Project with only this files: package.box, Vagrantfile, ubuntu-xenial-16.04-cloudimg-console.log, bootstrat.sh to the new location, an external HDD for example.
After this, in the shell/terminal run vagrant box add my-box name-of-the-box.box, where my-box is the name of ower new box, lets say gisbox and name-of-the-box.box is the name of the package box we just created, in our case is package box. So this command could in the end look like: vagrant box add gisbox package.box . This will add gisbox among the boxes list in vagrant. To check if gisbox was really added in vagrant list boxes, run vagrant box list and you should be able to see it there.
Next go to the new location where we just copied the files (package.box, Vagrantfile, ubuntu-xenial-16.04-cloudimg-console.log, bootstrat.sh) and open Vagrantfile. Replace the line "config.vm.box = "ubuntu/xenial64" with "config.vm.box = "gisbox" .
Your new Vagrantfile should look like this:
1. Copy/Paste the content below to the the Vagrantfile inside the folder Project!
###############################################################
#-*- mode: ruby -*-
# vi: set ft=ruby :
#We will add the 16.04 version of ubuntu
#And change the disk space from 10 GB to 30 GB
Vagrant.configure("2") do |config|
config.vm.box = "gisbox"
config.disksize.size = "30GB"
#We will share the Downloads folder from the Windows host machine with Ubuntu guest OS #from our virtual machine
#Make sure to change this path with the one coresponding to your computer !!!!!!!!!!!!!!!!!!
#This will help us share the Downloads folder from the Windows machine with the virtual #machine. But on the guest machine the Downloads folder will be found as vagrant_data
#for Windows OS!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Comment out next line if you are using macOS or Ubuntu!!!!!!!!!!
config.vm.synced_folder "C:/Users/your_user_name/Downloads", "/vagrant_data"
#For macOS and Ubuntu!!!!!!!!!!!!!!!!!!!!! Uncomment next line if you are using macOS or Ubuntu!!!!!!!!!
#config.vm.synced_folder "/Users/your_user_name/Downloads", "/vagrant_data"
#This is where we pass the bootstrap.sh file so that Vagrant can run install all we need in the #machine.
config.vm.provision :shell, path: "bootstrap.sh"
# Add the VirtualBox machine:
config.vm.provider "virtualbox" do |vb|
# # Display the VirtualBox GUI when booting the machine
vb.gui = true
# Customize the amount of memory on the VM:
vb.memory = "2048"
# set cpu’s
vb.cpus = 1
#Enable biderectional “copy/paste” and “drag and drop”
vb.customize ['modifyvm', :id, '--clipboard', 'bidirectional']
vb.customize ['modifyvm', :id, '--draganddrop', 'bidirectional']
end
end
Bootstrap.sh
Next we modify the bootstrap.sh file. Because our new box will install almost everything we have in this file, we will only keep the line that configures the virtualbox guest additions.
The file should look like this:
###############################################################
#!/usr/bin/env bash
sudo apt-get install virtualbox-guest-x11 virtualbox-guest-utils virtualbox-guest-dkms -y
###############################################################
Now, in windows command prompt/terminal run vagrant up .
Attention!!! You will most probably see this error message if you create the new box on a Windows machine and unpack it in a macOS or Ubuntu OS:
"There was an error while executing `VBoxManage`, a CLI used by Vagrant for controlling VirtualBox. The command and stderr is shown below. Command: ["startvm", "93344143-9326-4423-81c1-715484930dde", "--type", "headless"] Stderr: VBoxManage: error: RawFile#0 failed to create the raw output file C:/gis/ubuntu-xenial-16.04-cloudimg-console.log (VERR_FILE_NOT_FOUND) VBoxManage: error: Details: code NS_ERROR_FAILURE (0x80004005), component ConsoleWrap, interface IConsole"
What you'll have to do is open VirtualBox stop the new machine we are trying to create and right click on the new machine, then go to Settings -> Ports and in Path/Address change the path to ubuntu-xenial-16.04-cloudimg-console.log to point to the folder of the new machine. Remember that previously we also copied this file to the location of the new machine. Then run vagrant up again. Everything should work like a charm and you should also be able to see all of your work there.
7. SSH into virtual machine and skip installing a GUI
Of course we could skip the installation of the GUI and the configuration VirtualBox and ssh into virtual machine.
The command is: vagrant ssh .
The small problem with this approach, is the difficulty of running software that uses a GUI, like QGIS, for example. For this we should enable X11 on our computer and install a X11 display server, so things might get even more complicated. For each OS there is a different X11 display server, for Windows is Xming, for macOS is XQuartz and for Ubuntu it's Xorg.
8. Conclusions
So if you are still asking yourself why use Vagrant, I will give you a few reasons:
If you had the same issues as the ones listed above, then use Vagrant!
Although Vagrant can do a lot more than what the tutorial shows, respectively provision it with Puppet, Chef, Ansible, Salt, or Docker, this tutorial is about how to use Vagrant with VirtualBox provider and boot it with a GUI because I usually also use QGIS and other software that need a GUI and many times having a graphical user interface makes things a lot easier.