Running Django application on Vagrant

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 : do |config|
# Base box to build off, and download URL for when it doesn’t exist on the user’s system already = “ubuntu/trusty32”
config.vm.box_url = “”

# As an alternative to precise32, VMs can be built from the ‘django-base’ box as defined at
# , 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 yourself, and substitute your own URL below. = “django-base-v2.2”
#config.vm.box_url = “” # Torchbox-internal URL to

# 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.
# “”

# 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/”, :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 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
   Enter password:
      > create database my_db
      $ exit

11. Synchronize the database state with the current set of models and migrations
        $ python syncdb

12. To load fixtures run
     $ ./ loadfixtures

13. Up and Running
    $ python runserver

Now start the browser and hit localhost:9000 or 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.