[This blog post was originally written by Triet Le]
From the first day at Pivot Freight we had two specific goals with regards to development environments:
- We can’t afford to slow down adding new features.
- We need a stable development environment that allows us to easily and quickly test our code.
Hence, we need to have a sustaining local development environment that is:
- easy to develop and deploy testing code.
- as similar as possible to our production environment (PHP, PostgreSQL, Nginx).
There are many techniques and methodologies to set up an isolated environment either hosted in the cloud like Rackspace or AWS, or a local self-sustaining environment like as Docker (we are still considering Docker for our deployment strategy – more to come).
We decided to use Vagrant because we feel like it’s lightweight and makes it easy to virtualize a Linux box on our desktop. In the future, we may use Vagrant to manage Linux VMs to run Docker containers.
- Install Vagrant from https://www.vagrantup.com/.
Install VirtualBox from https://www.virtualbox.org/.
More details on why we need virtualbox can be found @ http://docs.vagrantup.com/v2/virtualbox.
- Go to https://atlas.hashicorp.com/boxes/search and copy the link for the Ubuntu box (14.04 LTS as of this post).
- The flavor of linux is up to you. We are using Ubuntu to be compatible with the Linux version of our production system. Later we will talk about packaging your own Vagrant “box” to be shared.
- Run: Vagrant box add <link>. As of this post it will be Vagrant box add https://atlas.hashicorp.com/ubuntu/boxes/trusty64.
- Run: Vagrant box list and verify the Ubuntu box is available.
- Create a local path to create the actual Vagrant instance, e.g. ~/vagrant/ubuntu/trusty64 and cd into it.
- Run: vagrant init ubuntu/trusty64
Set up your VagrantFile
- VagrantFile is basically the way you customize your initial Vagrant box.
- Run: cat Vagrantfile and verify the file exists and has expected contents.
- More of what you can do with VagrantFile can be found at http://docs.vagrantup.com/v2/vagrantfile/index.html. I found the following config to be very useful:
- Add a config.vm.network entry to forward 8080 (host) to 80 (guest). I’m planning to use this box as my basis for my Nginx webserver. This is a very powerful technique.
- Edit Vagrantfile and: add a config.vm.synced_folder entry to mount “../data” (as an example, this could be where the v2 Git code is, relative to where your Vagrantfile is). This will allow a very rapid development and testing cycle.
- Please share any other cool config you are trying in the comment section below.
Pivot Freight currently has 2 Vagrant boxes (more posts on this to come):
- Set up a PHP / PostgreSQL / Nginx testing and development environment.
- Set up a Java development testing and developing to work on our Java-based API.
Start Your Vagrant VM
- Run: Vagrant up, make sure all is well.
- Run: Vagrant ssh, make sure all is well.
Sharing is Caring
There is no point of building up a VM artifact if you can’t share it. You can package what you have so far by packing your Vagrant VM.
You can then “import” or box that environment by importing a prepared box.