Deploy development environments with Vagrant

Erstellt von Francis Varghese am 08. Jul 2016

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:

  • Your development environment can mimic the production environment.
  • Integrated provisioning tools such as Puppet and Chef allow you to store configuration in a standard format, which can also be used to update production environments.
  • Each project is separate in its own virtualized environment, so issues as a result of configuration and version differences for dependencies on different projects are things of the past.
  • New team members can be onboarded to new projects as easy as git clone && vagrant up.
  • „It works on my machine“ is an excuse of the past.
  • The headache of linking code that you write on your own machine to your virtualized development environment, is taken care of either through custom-synced folders or the default-synced folder (everything in your project’s folder gets mapped to Vagrant).
  • The environment can act as if it was your local machine and can map the web server port (80) of your development machine to your development environment if you wish.
  • You can let colleagues view your own development environment, as well as easily share the development environment.
  • Your local WAMP or MAMP installations will be gathering dust!

 

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

vagrant up
(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

References:
https://www.vagrantup.com/
http://www.vagrantbox.es/
https://www.packtpub.com/application-development/creating-development-environments-vagrant

Schreibe einen Kommentar

Nach oben scrollen