Vagrant is a great tool for all those who want to create and manage virtual machines easily, this article shows how to install it on Ubuntu 14.04 LTS server and show some basic configuration and commands.
- Installing dependencies:
apt-get install curl openssh-client ruby-childprocess ruby-erubis ruby-i18n ruby-log4r ruby-net-scp ruby-net-ssh ruby-net-ssh ruby
And install the last vagrant debian package (in this time 1.7.4) :
wget https://releases.hashicorp.com/vagrant/1.7.4/vagrant_1.7.4_x86_64.deb dpkg -i vagrant_1.7.4_x86_64.deb
Install the last virtualbox (in this time 5) :
apt-get install gcc make wget http://download.virtualbox.org/virtualbox/5.0.10/virtualbox-5.0_5.0.10-104061~Ubuntu~trusty_amd64.deb dpkg -i virtualbox-5.0_5.0.10-104061~Ubuntu~trusty_amd64.deb
In case if you are facing any broken dependency issue while installing virtualbox package, then run the below command to fix it :
apt-get install -f
After this, try again :
dpkg -i virtualbox-5.0_5.0.10-104061~Ubuntu~trusty_amd64.deb
Create some folders to hold all your vagrant stuff :
mkdir -p VAGRANT_PROJECTS/{BOXES,VAGRANT_DOTFILE,CONF,VAGRANT_HOME}
Edit ~/.bashrc file and add vagrant environment variables :
# For vagrant # To disable Vagrant software update check export VAGRANT_CHECKPOINT_DISABLE=1 # Vagrant current working directory, in this directory vagrant will look for Vagrantfile export VAGRANT_CWD=/home/tarek/VAGRANT_PROJECTS/CONF # The directory where Vagrant stores VM-specific state export VAGRANT_DOTFILE_PATH=/home/tarek/VAGRANT_PROJECTS/VAGRANT_DOTFILE # The Vagrant home directory is where things such as boxes are stored export VAGRANT_HOME=/home/tarek/VAGRANT_PROJECTS/VAGRANT_HOME
Download images and store them locally (Debian Jessie, Ubuntu server 14.04 LTS, Centos 7 and FreeBSD 10) :
cd ~/VAGRANT_PROJECTS/BOXES wget https://atlas.hashicorp.com/debian/boxes/jessie64/versions/8.2.0/providers/virtualbox.box wget https://cloud-images.ubuntu.com/vagrant/trusty/current/trusty-server-cloudimg-amd64-vagrant-disk1.box wget http://cloud.centos.org/centos/7/vagrant/x86_64/images/CentOS-7.box wget https://atlas.hashicorp.com/freebsd/boxes/FreeBSD-10.2-RELEASE/versions/2015.08.13/providers/virtualbox.box
Create a Vgrantfile (this file will hold the configuration of all you virtual machines) ~/VAGRANT_PROJECTS/CONF/Vagrantfile, mine looks like this :
# This is my conf file $yum_upgrade = <<SCRIPT echo "Start centos provisionning" sudo yum -y install epel-release sudo yum -y update SCRIPT $apt_upgrade = <<SCRIPT echo "Start debian provisionning" sudo apt-get update sudo apt-get -y upgrade SCRIPT Vagrant.configure("2") do |config| # Global for all VMs #config.vm.provision "shell", inline: "echo Hello" config.vm.network "public_network" # centos7one VM config.vm.define "centos7one" do |centos7one| centos7one.vm.box = "centos7one" centos7one.vm.provision "shell", inline: $yum_upgrade centos7one.vm.provider "virtualbox" do |centos7one| centos7one.customize [ "modifyvm", :id, "--cpus", "2" ] centos7one.customize [ "modifyvm", :id, "--memory", "1024" ] end end # ubuntu14one VM config.vm.define "ubuntu14one" do |ubuntu14one| ubuntu14one.vm.box = "ubuntu14one" ubuntu14one.vm.provision "shell", inline: $apt_upgrade ubuntu14one.vm.provider "virtualbox" do |ubuntu14one| ubuntu14one.customize [ "modifyvm", :id, "--cpus", "2" ] ubuntu14one.customize [ "modifyvm", :id, "--memory", "1024" ] end end # debian8one VM config.vm.define "debian8one" do |debian8one| debian8one.vm.box = "debian8one" debian8one.vm.provision "shell", inline: $apt_upgrade debian8one.vm.provider "virtualbox" do |debian8one| debian8one.customize [ "modifyvm", :id, "--cpus", "2" ] debian8one.customize [ "modifyvm", :id, "--memory", "1024" ] end end # debian8two VM config.vm.define "debian8two" do |debian8two| debian8two.vm.box = "debian8two" debian8two.vm.provision "shell", inline: $apt_upgrade debian8two.vm.provider "virtualbox" do |debian8two| debian8two.customize [ "modifyvm", :id, "--cpus", "2" ] debian8two.customize [ "modifyvm", :id, "--memory", "1024" ] end end # freeBSD10 VM config.vm.define "freeBSD10" do |freeBSD10| freeBSD10.vm.box = "freeBSD10" freeBSD10.vm.synced_folder ".", "/vagrant", id: "vagrant-root", disabled: true freeBSD10.ssh.shell = "sh" freeBSD10.vm.base_mac = "080027D14C66" freeBSD10.vm.provider "virtualbox" do |freeBSD10| freeBSD10.customize [ "modifyvm", :id, "--cpus", "2" ] freeBSD10.customize [ "modifyvm", :id, "--memory", "1024" ] end end end
You can customize your Vagrantfile if you want, in my case i create :
- Centos VM
- Ubuntu VM
- 2 Debian VMs
- FreeBSD VM
Add vagrant boxes :
vagrant box add debian8one /home/tarek/VAGRANT_PROJECTS/BOXES/debian-jessie-64 vagrant box add debian8two /home/tarek/VAGRANT_PROJECTS/BOXES/debian-jessie-64 vagrant box add ubuntu14one /home/tarek/VAGRANT_PROJECTS/BOXES/ubuntu-trusty-64.box vagrant box add centos7one /home/tarek/VAGRANT_PROJECTS/BOXES/centOS-7.box vagrant box add freeBSD10 /home/tarek/VAGRANT_PROJECTS/BOXES/freebsd-10.2.box
You can start all vms like this :
vagrant up
Or you can start just the debian8one like this :
vagrant up debian8one
To see the state of your VMs :
vagrant status
To ssh to a running VM :
vagrant ssh debian8one
To stop all VMs :
vagrant halt
To stop a running VM :
vagrant halt debian8one