Hassle free Linux development on Mac using Vagrant and VirtualBox
Seemingly these days majority of web projects are hosted in cloud on Linux instances such as AWS, Digital Ocean, etc. Subsequently these projects requires prerequisite tools to be installed and configured for running your application on production servers. Similar issues arises when doing cross platform C++ development when you have to test you code on various platforms and those requires specific libraries or softwares to work seamlessly. Vagrant is a tool to handle most of these issues without any complexity, it helps to manage Linux environment on Mac without leaving your favourite development tools.
Why I use Vagrant?
Primarily I use Mac to work on development projects, either it is web project or cross platform C++ development. Previously I had written an article on using development tools on Mac while developing remotely. Accordingly these projects require installing and configuring tools such as PHP, Apache server, MySQL and Python, Node tools, C++ compilers, profilers, debuggers and testing tools, etc. All of this creates lots of management hassle and increasingly frustrating to do this overtime on production server (mostly Linux virtual instances) for my clients. Consequently installing development tools on Mac raises versioning issues and personally I believe it adds up clutter and consumes space on Mac. Basically problems occurs when projects were setup with development environment on Mac but actual production environment is Linux therefore developers have to make tweaks to handle any surprising version and other production environment related issues.
An introduction from makers
Vagrant provides easy to configure, reproducible, and portable work environments built on top of industry-standard technology and controlled by a single consistent workflow to help maximize the productivity and flexibility of you and your team. see Vagrant…
Vagrant facilitates creating portable development environments and enables sharing and bootstrapping extremely easy for developers. Basically Vagrants simplifies most things with few commands, such as initial configuration, adding tools to development environment, file sharing, execution and deployment. Moreover due to its compatibility with most popular cloud based platforms and virtual machines it is my favourite and must have tool on Mac which enables using Mac environment without leaving it.
Vagrant setup is inherently simple with few commands you could kick start your development environment.
- Initialise vagrant -
- install box -
vagrant box add hashicorp/precise64
- start virtual machine -
- ssh into your virtual machine
- do any manual setup
Basically when I start any project I create an dedicated vagrant directory inside working directory to ensure I have a dedicated Vagrant box for each project. When Vagrant is initialised inside the directory it creates a Vagrant file, this file allows various options to be added or edited. Next specifying a box, there are various boxes provided by Vagrant and also popular cloud providers such as AWS, Digital Ocean, VMWare, etc. However it is alway a good idea to start with simple Vagrant box which could be a simple Ubuntu 64bit then make appropriate changes to Vagrant file as per preferable development environment and a bootstrap file then continue your development. Once you are done and all set to deploy simply change Box to your preference and then provision, Vagrant will automatically adapt to changes without any issues.
Work with same code base
One of the best option in Vagrant I found useful personally is shared folders, you could share directory location on Mac which you could edit modify simultaneously same code can be used to compile, build, run, test, whatever you like to do. Similarly a shared code can be used as apache virtual host on guest machine and that can be accessed in browser on host machine. Shared folders can be configured in VagrantFile
config.vm.synced_folder "location_on_host_machine", “location_on_guest_machine”
Run site on linux and test on host
Port Forwarding features in Vagrant context are a great way to work with your shared code especially working on web projects. A guest machine configured with dedicated IP address exposes apache sites and with a dedicated port forwarded between host and guest, host machine redirects request to a port on guest machine.
Basically the shared folder could be enabled to setup as site through Apache VirtualHosts with specific port, which will serve web request. Then with port forwarding enabled on guest machine any http request on host port number will automatically redirect to the site enabled on guest. The following lines in Vagrant file will setup dedicated ip and port forwarding in vagrant.
config.vm.network "private_network", ip: "192.168.10.10" config.vm.network "forwarded_port", guest: 80, host: 8088
Comparing Vagrant and Docker
Docker is a great tool to manage development environments backed by containers rather than Virtual machines. It runs as light weight process on your linux instance by executing a light weight process under linux kernel resulting in using minimal RAM (memory) on your server. It is a great tool to have on Linux for managing your development environment and can be easily shared with other developers and used to configure server environments.
Although Docker has several advantages over Vagrant and I have used it while working with one of client. Typically it is seamless on Linux but on Mac both Vagrant and Docker requires Virtual Box, while installing Docker on Mac it also downloads Virtual Box to support Linux virtualisation. Moreover personally I believe Vagrant is rather more organisation on Mac compared to Docker which I found a bit confusing or rather disorganised.
Vagrant and Docker are great tools to manage development environments, I believe instead of competing them I would rather complement to make development and environment easier. Considering Docker is a great tool built for Linux and highly efficient on Linux but in my experience Vagrant makes life easier on Mac with its simplified approach. Preferably I would use both together such as using Vagrant with Docker box and provisioning as well.