Use multiple versions of Vagrant on the same machine
Recently I faced an issue of running Vagrant version 1.8 because of plugin incompatibility. I’ve removed my original installation (which was 2.1) and did the work needed. But a while after that I had to use that old version once again. So not to repeat this boring process once again I decided to make my system work with two different versions of Vagrant simultaneously. To accomplish that we need to throw away regular installation process and build everything from sources.
$ cd /opt
Now we have Vagrant sources for version (tag) we want and we can build from this state
$ bundle install --path vendor/bundle
We install Vagrant with a help of Bundler and create an executable we can invoke as regular
vagrant binary. To make our life easier and not to type absolute path to executable
/opt/vagrant-2.1.2/exec/vagrant each time we need to symlink it.
$ sudo ln -s /opt/vagrant-2.1.2/exec/vagrant /usr/bin/vagrant2
And try it our in our terminal
$ which vagrant2
Now we can install our second Vagrant instance, this time we can do that in a usual way
$ wget https://releases.hashicorp.com/vagrant/1.8.4/vagrant_1.8.4_x86_64.deb
The last thing to make is to change Vagrant home directory to a different place. We installed version 1.8.4 as a package and it will look at default location, so we will change that path for 2.1.2 version. The folder mentioned stores all the boxes information, plugin installed and other metadata. It’s critical to separate the data between versions of Vagrant in order to make them both work properly. There is a
VAGRANT_HOME environment variable which tells Vagrant where to look at. We can create helper script that will automatically set/unset our variable and also symlink
vagrant alias to a proper location (not to have a mess thinking every time which
vagrant2 you need to type). Create a helper script in your home directory for example
and type/copy the following content to this file
Let’s verify it works properly:
$ source .toggle-vagrant-version.sh
It works! You can do your daily routine without any troubles now. The other hint you can do to optimize your workflow even better is to create a shell alias to switch between versions event faster. Edit your
~/.bashrc (depends on which shell you are running) file adding new line
alias tvag="source $HOME/.toggle-vagrant-version.sh"
Instead of tvag alias enter any short word easy for you to remember and invoke it from the shell any time you want to switch current vagrant version.
$ source ~/.bashrc
I hope now you have one less problem.
Make sure your
VAGRANT_HOME directory exists to eliminate any runtime errors. If you want to preserve your previous settings from original installation you can manually copy that data by yourself
$ mkdir /opt/vagrant-2.1.2/.vagrant.d
And another tip is for VirtualBox users: to automatically install your VirtualBox Guest Additions you can use Vagrant plugin which will do that for you
$ vagrant plugin install vagrant-vbguest