Well, this is going to be another straight forward post with loads of shell commands. The background is simple, I love OS X for the many apps it offers but at the same time I always had the joy of developing on a Linux machine. I decided to use Vagrant to have one central box for my projects. I use separate vagrant boxes for complicated projects. But didn’t like the idea of having individual boxes for simple ones.
I work on Python and Django a lot. So in this post I am going to document my setup.
Vagrant File:
# I have assigned a private IP to the box
# I have assigned a hostname to it.
# I am using Ubuntu Precise Pangolin 64 bit as my base box (which I already have added)
1 2 3 4 5 6 7 8 9 10 11 12 13 |
# -*- 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| config.vm.box = "precise64" config.vm.network :private_network, ip: "192.168.234.131" config.vm.hostname = "tardis.dev" end |
Hosts File (/etc/hosts):
1 |
192.168.234.131 tardis.dev |
After I setup the box, I logged in using SSH:
1 |
vagrant ssh |
Now, the usual Ubuntu setup.
1 2 3 4 5 |
sudo apt-get install python-pip sudo pip install virtualenv mkdir ~/.virtualenvs sudo pip install virtualenvwrapper sudo apt-get install python-dev libmysqlclient-dev libpq-dev |
Bash Profile in the Ubuntu machine:
# To make the Django built in server available from outside the box I need to run it on 0.0.0.0 so that it listens on all interfaces. So I added a handy alias.
# Added the virtualenv stuff to the profile
1 2 3 4 5 6 |
# Virtual Env export WORKON_HOME=~/.virtualenvs . /usr/local/bin/virtualenvwrapper.sh # Aliases alias djrun="python manage.py runserver 0.0.0.0:8000" |
Bootstrapping a project:
1 2 3 4 5 6 7 |
mkvirtualenv project cd ~/project pip install -r requirements.txt ## Later workon env_name djrun |
Now the app would be available on: http://tardis.dev:8000
I also modify the ~/.virtualenvs/
That’s it 🙂
8 replies on “Python and Django on Vagrant”
Nice post!
You can also automate the dependency installation via Vagrant provisioning, https://docs.vagrantup.com/v2/provisioning/.
Yes, I would probably put this in a shell script and setup provisioning. Just wanted to share what I did.
And I would probably package the box in a stable state and save it somewhere for future usages 🙂
Which markdown do you use in the comment section?
Not really sure. I use the rich text formatting tools.
[…] Python and Django on Vagrant […]
You have skipped many important steps like – vagrant up, so if a beginner is reading this article he will receive a serious error which will become a headache for him.
Anyone who would understand how a Vagrant file works should know to do
vagrant up
before doingvagrant ssh
. But yes, it’s not beginner friendly. It was not supposed to be.