Vagrant is a trend among development tools at present. Here will be description of features and guide to quick start with Vagrant. The main idea of Vagrant in development space control through SSH with “root” rights. If you conduct any Linux distro, you don’t need to use a “sudo” to rewrite a file or execute a command.
Documentation of project is definitely fine. But I want to express my own standpoint towards Vagrant. Won`t go down to the beginnings of this project, here is what’s going on right now. So, Vagrant has a lot of benefits, as for developers, as for admins. Complex of features for both sides makes it as ideal tool for DevOps.
- Cross-platformed project.
Contemporary software has to fit well for each operating system: Linux, Mac, Windows. This attribute is a key to success. Vagrant provides this opportunity: as for guest systems as for virtual platforms. What can be more pleasure than control Linux machine through the Windows command line? All you need – make sure that your directory Vagrant binary files in PATH variable.
- Easy to deploy environment.
For now IT community didn`t invent such light way to deploy environment beside “vagrant up” command. You can force Vagrant to switch on VM and update dependencies by one command.
This conception is actually smart. Usually developer goes through some identical steps to make his own space for development. Why should he do few command if it could be done by one? That’s the point of Vagrant.
Just initialize ready image from Hashicorp base which fits your necessities and make “vagrant up”. There is no more direct downloading by Internet-browser. And of course you can create your own box by Packer. That’s what I said in previous article.
- Easy to come back.
If your project is absent and you need to return to it after some time, you shouldn`t have any problems. “vagrant up” installs all appeared package dependencies and update package versions in selected box.
- Opportunity to make a project in Guest OS.
This benefits reached with shared folders mechanism. As usual you can write source code in your guest OS, verify it by IDE and test how it works in environment by Vagrant. Every change of file in shared folder will be synced with Vagrant platform automatically.
By default, Vagrant will share your project directory to /vagrant. You can replace it by Vagrantfile configuration.
- Virtualization independence.
Every Vagrant project based on virtualization platform, where box is launched. I mean any virtualization platform: from VBox to VMware, from Amazon EC2 to DigitalOcean. Declare your virtualization software in project Vagrantfile and you will be fine. Also you can make a point in command directly. By way of
vagrant up --provider=vmware_fusion
Of course, there can some obstacles referred with features of every platform. For example, you would be better to install GuestAdditions in machine running on VirtualBox. But it’s not so catastrophically and has simple solutions.
- Easy to share.
Changes in environment is local, but you can spread changes you’ve done despite of this fact. Usually changes shared by Vagrantfile. For example, you can capture all shell commands that`s basically changed environment in bash-script. Further make a script file on guest OS directory. After that and the file link to Vagrantfile configuration in provisioning directive. And share your Vagrantfile+script with your team at last! The last step could be done with VCS as well as Git, Mercurial, Subversion and so on.
1. Find out desired box.
Here you need to figure out: what do you need for your project neighborhood. Choose your operating system and required software: DB server, web-server, libraries and so on. Take a look on Hashicorp list at first. If you find there what it takes – start to deploy environment. If you think there is no option that fits you or this boxes have a lot of useless components – make your own box. You can do that by Vagrantfile configuration, but Packer gives you much more flexibility. As a software engineer you can ask your operations engineer to build a box, because it’s his own job. Or take it by yourself if you don’t have a choice
2. Put the box to the Vagrant list.
There is 2 ways dependent on box layout.
- remote box
If you’ve found what you need in Hashicorp page or anywhere else, just init chosen box. Here is example of Ubuntu server initialization.
vagrant init ubuntu/trusty64
Repeating myself, you don’t need to download it by web-browser. Vagrant will download ready image and add it to general boxes base.
- local box
If you’ve downloaded box or have your personal built box – add it to boxes base. Usually developers uses Vagrant from Windows. Here is the easiest way to do it.
cd \your\path\to\box\directory vagrant box add box_name box_file_name.box
It’s going to be laid in your base. By default Windows save boxes in C:\profile_name\.vagrant_d\boxes directory. Absolutely, you can change it. Set environmental variable set it as a path to desired directory.
set VAGRANT_HOME=X:\PATH\TO\VAGRANT ;windows export VAGRANT_HOME=PATH/TO/VAGRANT ;bash
3. Add some configuration settings.
After box addition configured Vagrantfile should be put in the box directory. Come there and change this file as you wish. At first you have to declare the name of box you’ve set in “vagrant box add” command. You can check it through “vagrant box list” command. Also you can set name and password of connected user. By default it’s “vagrant”. The same way declare VM provider. Later you might put provisioning commands/scripts that will be launched in “vagrant up”. There is a wide choice of different configuration tools which Vagrantfile has. Here is the example of expanded configuration (by digitalsparky).
Vagrant.configure(2) do |config| config.vm.hostname = 'moodle.local' config.vm.box = "ubuntu/trusty64" config.vm.network "private_network", ip: "192.168.33.10" config.vm.synced_folder "./moodle/", "/var/www/moodle", create: true, owner: 'www-data', group: 'www-data' config.vm.provider "virtualbox" do |vb| vb.name = "moodle" vb.memory = 1024 vb.cpus = 2 end config.vm.provision "shell", inline: <<-SHELL sudo bash /vagrant/provision.sh SHELL end
4. Get it up.
One command. Two words. No worries.
During the time when your machine goes up, required configuration is checking. There are SSH tweaks, compatibility with box hardware settings, provisioning execution and so forth. Also recognize and verify private features of chosen VM platform. For instance, if VirtualBox image doesn’t have installed GuestAdditions, Vagrant will show warning message. The main key for troubleshooting on this stage – Vagrantfile configuration. And have some patience. OS deployment and provisioning configuration can not be executed in a couple of seconds. But it gives you an opportunity to focus on something else.
5. Connect to Vagrant environment.
As mentioned, Vagrant provides SSH connection between guest OS and virtual machine. All you need is type one command:
Sometimes Windows users have an issue with it. If you run Vagrant from Windows, you have to set SSH utility. It` usually included in programs like Git, Cygwin, MinGW, Putty. Find SSH binary file and add file directory to the %PATH% variable.For instance, For instance, if you have installed Git, you can find this file in Git subdirectories \usr\bin or \bin. It should solve the problem and allow SSH connections from command line.
That`s all! Honestly, was it such complicated? There is much more extensions which you can find in Vagrant documentation. Throw out biases like “deployment is hard and trivial” and “i need a distinct server to test my developments”. It`s time to leave compatibility anxieties behind. Focus on activity where you’re the best – write a code!