Vagrant up and running
Haven't blogged awhile now, but still trying to push on and get this into my
weekly routine. Meanwhile, I have changed company where I worked - joined with
startup Tebo. Right now trying to catch up what they have made those years when
I was not involved in this project.
I'm not going to stop on this topic a
lot because I want to let you know about an awesome tool that colleague Erkki
introduced me, it's just amazing. Makes developer life lot easier, it's called
Vagrant. I have played around now for two weekends with it, even added to my
toolbox and use it right now, when I write this blog. But yeah, I try to give
breathe overview of it and hope you will find use of this tool too :)
What is Vagrant?
Vagrant is a tool for building complete development environment, sandboxed in a
virtual machine. Vagrant lowers developer environment setup time.
With one
command, it does all the following in minutes:
Creates a virtual machine for you.
Modifies the physical properties of this virtual machine (e.g., RAM, number of CPUs, etc.).
Establishes network interfaces so that you can access your virtual machine from your own computer, another device on the same network, or even from another virtual machine.
Sets up shared folders so that you can continue editing files on your own machine and have those modifications mirror over to the guest machine.
Boots the virtual machine.
Sets the hostname of the machine.
Provisions software on the machine via a shell script or configuration management solution such as Chef, Puppet, or a custom solution.
Performs host and guest specific tweaking to work around known issues that may arise.
When it’s all done, you have completely sandboxed development environment.
Due to the shared folder and networking, you can continue development using your
own editor and browser, but the code runs on a virtual machine, that vagrant has
set up for us.
Requirements
Vagrant requires virtualization application, like VMWare or VirtualBox. Because by default vagrant supports VirtualBox, I’m going to use that too.
Vagrantfile
Vagrant is configured per project and each project has it’s own Vagrantfile. In Vagrantfile is described OS, RAM, network settings and so on. This file can be version controlled, so rest development team can stay sync with changes.
Installation
VirtualBox installation
I’m using Fedora 25, so I’m going to describe here how to get VirtualBox and
Vagrant installed on Fedora 25. If you are using another operation system,
please check out Vagrant website, they have really good documentation.
Add VirtualBox repository and update system.
1sudo su -2cd /etc/yum.repos.d/3wget https://download.virtualbox.org/virtualbox/rpm/fedora/virtualbox.repo4dnf update
Make sure you are running latest installed kernel version.
1rpm -qa kernel |sort -V |tail -n 12uname -r
If you got a kernel update, reboot system.
Install Virtualbox dependencies.
1dnf install binutils gcc make patch libgomp glibc-headers \2 glibc-devel kernel-headers kernel-devel dkms3dnf install VirtualBox-5.1
Build needed kernel modules.
1/usr/lib/virtualbox/vboxdrv.sh setup
In order to use VirtualBox, a user must be added to a vboxusers group. This group was created automatically during VirtualBox installation process.
1usermod -a -G vboxusers USERNAME
Vagrant installation
1sudo dnf install vagrant
Note that Fedora use libvirt as default provider, so to use Virtualbox instead you need to set an environment variable for it.
1sudo dnf install -y vagrant vagrant-libvirt libvirt-devel
Project setup
First we gonna need project folder (create one or use existing). Navigate to the
folder in terminal.
Create inital Vagrantfile.
1vagrant init centos/7
To run Vagrant machine, run following command. This wil install and set up our virtual machine with setting that is described in Vagrantfile.
1vagrant up --provider virtualbox
By default Vagrant shares project folders with quest machine. For better overview, I’m gonna create new folder under project folder and move my project files there. In Vagrantfile I define new shared folder.
1config.vm.synced_folder "./game", "/server/website"
Now, before rebooting virtual machine, if you are using VirtualBox we need to install Vagrant plugin to make folder sharing possable between guest virtual machine and host machine.
1vagrant plugin install vagrant-vbguest
When installation is completed, we need to reboot virtual machine.
1vagrant reload
To access my project thought webbrowser we gonna need to forward ports and set up simple server.
1config.vm.network "forwarded_port", guest: 80, host: 8080
Reboot guest.
1vagrant reload
Now ssh to guest machine and navigate to /vagrant folder.
1vagrant ssh
Now we need to set up python http server.
1sudo python -m SimpleHTTPServer 80
For now, we have vagrant machine up. Inside that we are running server where is hosted my tetris game (not ready yet). If I navigate to https://localhost:8080/game/, I see my tetris game.
Useful commands
1#SSH to machine.2vagrant ssh34#Check running vagrant machines.5vagrant status67#Destroy vagrant machine (files in shared folders are not destroyed).8vagrant destroy910#Shut down virtual machine.11vagrant halt