Vagrant is useful to create and configure lightweight, reproducible, and portable development environments.
Here I am going to show you how to setup Vagrant on Ubuntu 14.04 and how to up and running a Django application on it.(You can try same with Rails application)
Follow these steps.
1. Install Virtualbox:
$ sudo apt-get install virtualbox
2. Install Vagrant
$ sudo apt-get install vagrant
3. Preparing Django application for VM:
Go to application home directory and Initializes the it to be a Vagrant environment aware.
$ vagrant init
This will create an initial Vagrantfile if one doesn’t already exist. You might have to override some of the default configurations. Here is the whole config file from my local setup.
# -*- mode: ruby -*-
# vi: set ft=ruby :
Vagrant::Config.run do |config|
# Base box to build off, and download URL for when it doesn’t exist on the user’s system already
config.vm.box = “ubuntu/trusty32”
config.vm.box_url = “https://cloud-images.ubuntu.com/vagrant/trusty/current/trusty-server-cloudimg-i386-vagrant-disk1.box”
# As an alternative to precise32, VMs can be built from the ‘django-base’ box as defined at
# https://github.com/torchbox/vagrant-django-base , which has more of the necessary server config
# baked in and thus takes less time to initialise. To go down this route, you will need to build
# and host django-base.box yourself, and substitute your own URL below.
#config.vm.box = “django-base-v2.2”
#config.vm.box_url = “http://vmimages.torchbox.com/django-base-v2.2.box” # Torchbox-internal URL to django-base.box
# Boot with a GUI so you can see the screen. (Default is headless)
# config.vm.boot_mode = :gui
# Assign this VM to a host only network IP, allowing you to access it
# via the IP.
# config.vm.network “188.8.131.52”
# Forward a port from the guest to the host, which allows for outside
# computers to access the VM, whereas host only networking does not.
config.vm.forward_port 8000, 9000
# Share an additional folder to the guest VM. The first argument is
# an identifier, the second is the path on the guest to mount the
# folder, and the third is the path on the host to the actual folder.
config.vm.share_folder “project”, “/home/vagrant/dfportal_shared”, “.”
# Enable provisioning with a shell script.
# config.vm.provision :shell, :path => “etc/install/install.sh”, :args => “myproject”
4. Start Vagrant
$ vagrant up
5. After successful login, go to root directory and run following command:
$ cd home/vagrant/mydjango_apps_shared
You should be able to find manage.py there.
The mydjango_apps_shared is the shared directory between your machine and VirtualMachine. Quite Amazing!!
Install required packages for Django Application:
6. Install python package installer on VM
$ sudo apt-get install python-pip
7. Install Django using pip:
$ sudo pip install django
8. Install required packages:
$ sudo pip install -r requirements.txt
9. Setup Mysql DB in VM
$ sudo apt-get install mysql-server
10. Then connect to mysql and create a DB for your application
$ mysql -u root -p
> create database my_db
11. Synchronize the database state with the current set of models and migrations
$ python manage.py syncdb
12. To load fixtures run
$ ./manage.py loadfixtures
13. Up and Running
$ python manage.py runserver 0.0.0.0:8000
Now start the browser and hit localhost:9000 or 127.0.0.1:9000 and your application works!!!
If you can’t see the page, just look back to Vagrant file and check config.vm.forward_port settings to exactly know which port the VM is listening and forwarding.