Developing modern web-based applications can be complicated. Different projects have their own requirements and dependencies, which are often incompatible with one another. A legacy project might require a specific version of PHP or specific versions of extensions Apache, whereas another project may require a newer version of PHP and running on Nginx. Project switching in this scenario is not easy.
A virtualized development environment can help this situation. Instead of having to battle configurations when working on other projects, each project can simply have its own virtualized environment. It can have its own dedicated web server, database server, and the versions of the programming language and other dependencies it needs. Since it is virtualized, it doesn’t impact other projects, just shut it down and boot up the environment for the other project.
Vagrant is a tool for building complete development environments. With an easy-to-use workflow and focus on automation, Vagrant lowers development environment setup time, increases development / production parity, and makes the “it works on my machine” excuse a thing of the past.
We can achieve the following with Vagrant:
How to install and run Vagrant:
1. Install VirtualBox
Download and install VirtualBox from https://www.virtualbox.org/wiki/Downloads
2. Install Vagrant
Download and install vagrant from https://www.vagrantup.com/downloads.html
Install vbguest plugin:
vagrant plugin install vagrant-vbguest
3. Create project directory
Create your base vagrant install directory somewhere on your machine.
(ex: mkdir /var/www/vagrant)
4. Initialise Vagrant inside the newly created directory
vagrant init ubuntu/trusty64
(This will create a `Vagrantfile` file inside directory. This is the configuration file of vagrant.)
5. Run Vagrant Machine
(This will boot up your virtual machine with configurations specified inside `Vagrantfile`)
At this point your virtual box will be running and active. You can ssh into the virtual machine using command `
vagrant ssh` and you can install desired packages inside your new virtual machine. To shutdown your virtual machine you can use `
vagrant halt` or `
vagrant suspend` commands.
Once you configured your virtual machine with all required packages and dependencies needed for your project, you can export this machine to a `*.box` file using `
vagrant package` command. Now host this box file somewhere accessible for all developers. Inside your vagrant file set `
config.vm.box_url` to the URL pointing to that box file. Now share this Vagrantfile with all developers in your project. All they have to do is to fire `vagrant up` command from the directory where this Vagrantfile resides. It will download the box file and will import the virtual machine from that box file. Simple as that.
You can also configure box versions to create virtual machines with versions. In that case when a new version of box is released, you will get notified about the new release when you do `vagrant up` next time. At that time you can update your virtual machine to your new version by simply firing `
vagrant box update`.
(Ref : https://github.com/hollodotme/Helpers/blob/master/Tutorials/vagrant/self-hosted-vagrant-boxes-with-versioning.md)
Most commonly used vagrant commands are these:
vagrant box add [--provider provider] [--force]
vagrant init vagrant box list vagrant box remove vagrant box repackage vagrant box update