Setting up development enviroment with Vagrant

vagrant

Nowadays we have to work on different different web stacks at the same time. Even in same web stack our production environment and development environment are totally different. It became obvious to use visualization in order to solve this problem. There are a couple of solutions exist such as vagrants, docker (based on Linux container), VirtualBox virtualization and Linux LXE container. Okay, What is vagrant?

Vagrant is an open-source software that helps to portable and reproducible development environment based on visualization. Basically, it’s a command line wrapper of different kind Virtualization providers. Vagrant supports different kinds of providers such as VMware, Hyper-V and VirtualBox. Today I will show setting up developing environment based on Oracle Virtualbox provider.

it’s a command line wrapper of different kind Virtualization providers

No more explanation… Let’s set up the environment. I will explain everything in some steps

Installation:
I am expecting that already installed vagrant in your PC. If not, then just follow the installation process from their website.

Adding Box
I will be using my own box: precise64 which has LAMP stack pre-installed.

cd ~ | wget https://dl.dropboxusercontent.com/u/6886596/precise64.box
vagrant box add vagrant-precise64 precise64.box

You will get more boxes here: HashiCorp’s Atlas box catalog.
For example, you want use Ubuntu/trusty64 (Latest Ubuntu 14.04 LTS builds)

vagrant box add Ubuntu/trusty64

There are also some unofficial boxes are available at this link: http://www.vagrantbox.es. For the unofficial boxes, you will have to download the box first, and then you can add that as a box the same way I added my own box.

Initiate the box
Your box name is vagrant-precise64. Go to the root directory of your project and run following command….

vagrant init vagrant-precise64

Yeah, you are almost done. But, let’s do little bit customization to get better flexibility. At the project root you will get a file called Vagrantfile. Vagrantfile basically keeps all the configurations that required to run the vagrant. Replace the default configurations with following configurations

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# -*- mode: ruby -*-
# vi: set ft=ruby :
 
# Vagrantfile API/syntax version. Don't touch unless you know what you're doing!
VAGRANTFILE_API_VERSION = "2"
 
Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
  #Vagrant box
  config.vm.box     = "vagrant-precise64"
  config.vm.box_url = "https://dl.dropboxusercontent.com/u/6886596/precise64.box"
 
 
  # Forward a port from the guest to the host, which allows for outside
  # computers to access the VM
  config.vm.network "forwarded_port", guest: 80, host: 8080
 
  # need a private network for NFS shares to work
  config.vm.network "private_network", ip: "192.168.50.4"
 
 
  #Sync with host PC's project root with guest file Example: Project root of Host PC ~/codes/php/hellp_world
  config.vm.synced_folder "/home/eftakhairul/codes/php/bookdeals_webapp", "/var/www"
 
  # Update the server
  config.vm.provision :shell, :inline => "apt-get update --fix-missing"
 
  #Setup some resource for virtualbox Example: set up memory to 2 GB
  config.vm.provider "virtualbox" do |vb|
        vb.customize ["modifyvm", :id, "--memory", "2048"]
 
        #You can more CPU
        #vb.cpus = 2
  end
end

Booting the vagrant
Now, boot your vagrant environment, run following command in console

vagrant up

In your console you will get something like below:

Bringing machine 'default' up with 'virtualbox' provider...
==> default: Clearing any previously set network interfaces...
==> default: Preparing network interfaces based on configuration...
    default: Adapter 1: nat
    default: Adapter 2: hostonly
==> default: Forwarding ports...
    default: 80 => 8080 (adapter 1)
    default: 22 => 2222 (adapter 1)
==> default: Running 'pre-boot' VM customizations...
==> default: Booting VM...
==> default: Waiting for machine to boot. This may take a few minutes...
    default: SSH address: 127.0.0.1:2222
    default: SSH username: vagrant
    default: SSH auth method: private key
    default: Warning: Connection timeout. Retrying...
==> default: Machine booted and ready!
==> default: Checking for guest additions in VM...
    default: The guest additions on this VM do not match the installed version of
    default: VirtualBox! In most cases this is fine, but in rare cases it can
    default: prevent things such as shared folders from working properly. If you see
    default: shared folder errors, please make sure the guest additions within the
    default: virtual machine match the version of VirtualBox you have installed on
    default: your host and reload your VM.
    default: 
    default: Guest Additions Version: 4.2.0
    default: VirtualBox Version: 4.3
==> default: Configuring and enabling network interfaces...
==> default: Mounting shared folders...
    default: /var/www => /home/eftakhairul/codes/php/bookdeals_webapp
    default: /vagrant => /home/eftakhairul/codes/php/bookdeals_webapp
==> default: Machine already provisioned. Run `vagrant provision` or use the `--provision`
==> default: to force provisioning. Provisioners marked to run always will still run.

Done. Your dev environment is ready. Type in your browser: http://localhost:8080/ or http://192.168.50.4/ 🙂

SSH to the server
You can ssh to the server easily…

 vagrant ssh

In case, you need to execute specific commands after starting your Virtual machine, you can ssh to the vagrant and execute those commands. But, the problem is every time and for all user will have to do the same thing. Rather than, you can write a shell provisioner which will be executed while vagrant booting up.

For example, you have a shell script named bootstrap.sh as follows

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#!/usr/bin/env bash
 
apt-get update --fix-missing
 
##curl install
apt-get install curl
 
##installing apache2 server
apt-get install -y apache2
 
##installing mysql server with setting up 
##usernmae as root and password as root
debconf-set-selections <<< 'mysql-server \
mysql-server/root-password password root'
 
debconf-set-selections <<< 'mysql-server \
mysql-server/root_password_again password root'
 
apt-get install -y php5-mysql mysql-server
 
service mysql restart
service apache2 restart
 
curl -sS https://getcomposer.org/installer | php
mv composer.phar /usr/local/bin/composer 
 
##removing the /var/www folder and simulink with /vagrant directory
if ! [ -L /var/www ]; then
  rm -rf /var/www
  ln -fs /vagrant /var/www
fi

Now, You will have to add this to the Vagrantfile that vagrant can execute the shell script.

    config.vm.provision :shell, path: "bootstrap.sh"

Basically, vagrant will execute the bootstrap.sh file when it gets ready.

Some important vagrant commands are below

1
2
3
4
   vagrant reload  #=> Incase you change Vagrantfile while running the server
   vagrant suspend #=> Incase you want to suspend the vagrant environment
   vagrant resume  #=> Run the vagrant environment from suspend's mode
   vagrant halt    #=> Shut down the vagrant environment. You can use -f to do it forcefully

Happy Hacking 🙂

 

Eftakhairul Islam

Hi, I'm Eftakhairul Islam, a passionate Software Engineer, Hacker and Open Source Enthusiast. I enjoy writing about technical things, work in a couple of startup as a technical advisor and in my spare time, I contribute a lot of open source projects.

 

One thought on “Setting up development enviroment with Vagrant

  1. Thanks buddy, I was struggling to add the downloaded vagrant tar file, but after this steps it was simple & easy to add & start-up vagrant.

Leave a Reply

Your email address will not be published. Required fields are marked *

 

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Read previous post:
Custom validations in Rails 3

Today, I am writing the blog post with my late apologization. Sorry to all of my readers and followers for...

Close