Virtualizing your development environment with Vagrant and Ansible
Tools for virtualization and provisioning like Vagrant and Ansible have been around for a while now, but I haven’t met many people that use them for their everyday development work. So I am going to try to make some PR for Vagrant and Ansible.
I first started working in virtual machines because I wanted to be independent of the hardware my employer provided me with. At that time, we moved from a desktop computer to a frequently crashing Windows laptop, to a perfectly good System76 laptop with Ubuntu pre-installed. By the end of that transition, I did all my development work in a vm. I had an Ubuntu guest running inside an Ubuntu host and I was happy; because I knew that when I would have to switch hardware again, I could just export my vm and import it somewhere else.
Back then I only used VirtualBox. Nowadays I use Vagrant and Ansible to completely automate the process of setting up a new development environment. Whenever I start working on a new project, I let Vagrant and Ansible do their magic. Vagrant takes care of configuring VirtualBox and it will also pull a base image with some OS pre-installed. Ansible does provisioning, which, in a nutshell, means installing packages and copying configuration files.
This approach has several advantages
- Your host OS will not become a monolith with all kinds of packages installed that you no longer need.
- Each one of your projects has its own personal sandbox. It’s like Python’s virtualenv or Ruby’s RVM on an OS level.
- If you manage to break apt-get or grub, it will only affect 1 project. And you can easily recreate that 1 vm.
- If your hardware breaks or you need to switch hardware for some other reason, you won’t have to cry. If you backed up your VirtualBox files, you can continue working on another machine without having to reinstall anything. And if you didn’t make backups, you can just rerun Vagrant and Ansible.
- When you close your vm, you can choose to save its state. Memory will be freed so you will be able to use all your system’s resources for something else. And when you start your vm again, it’s like you never left and you are right back in the middle of that Jupyter notebook file.
- You can easily share your Ansible roles with your team members. If there is a new colleague coming to work, you won’t have to sit next to him/her for an entire day to help him set up his environment. Just sit next to him while his machine is provisioning and explain to him in general terms what Ansible is installing and why.
- In case you need to transfer your work to a co-worker, you can give him/her your vm image in addition to any other files/information.
- When you are done working on a project, make sure all your code is in git, like a proper developer. Then you can just delete your vm without leaving any debris.
And of course there are drawbacks as well
- Obviously, you would need to know a little bit about Vagrant and Ansible.
- Both your hardware and host OS need to support hardware virtualization.
- There is some overhead involved in running an OS on top of another OS. But I don’t think I ever noticed enough to complain about it. In any case, storing your VirtualBox files on an SSD will certainly help.
- You need a little bit of extra disk space for all those guest OS’s that will start gathering dust. You should just delete old vm’s that you don’t use anymore, or at least remove them from your SSD.
- If you use proprietary software, you might run into licensing issues. In the case of IntelliJ, it seems you cannot run 2 instances of the IDE simultaneously.
- Occasionally you need to have 2 vm’s open at the same time. Make sure you have enough memory available to support both vm’s. If not, the 1st vm you started will be terminated while starting up or un-suspending the 2nd. Depending on the memory usage of your vm’s and your host, running 2 vm’s simultaneously on an 8 GB host can get tricky. I have learned to appreciate extra RAM as well as lightweight Linux distributions.
I hope this piece of propaganda will motivate people to try something new. On my Github account, you will find some scripts that I use to set up a development environment for data science projects that I sometimes do. Take note that it’s a work in progress and it probably won’t work out of the box, because I use my own private base box. If you are completely new to Vagrant or Ansible, it’s best you start with some tutorial.