Skip to content
AslakHol edited this page Nov 2, 2016 · 18 revisions
TLDR at the bottom. If you haven't used Vagrant before, read this article.

What is Vagrant?

Vagrant is a VirtualBox-wrapper that automatically installs, configures and provides network and sharing access to the virtual machine.

Stolen from Why Vagrant?

Vagrant will isolate dependencies and their configuration within a single disposable, consistent environment, without sacrificing any of the tools you're used to working with (editors, browsers, debuggers, etc.). Once you or someone else creates a single Vagrantfile, you just need to vagrant up and everything is installed and configured for you to work. Other members of your team create their development environments from the same configuration, so whether you're working on Linux, Mac OS X, or Windows, all your team members are running code in the same environment, against the same dependencies, all configured the same way. Say goodbye to "works on my machine" bugs.

The benefit of using Vagrant: all you basically have to do in order to have a development environment ready to go with OnlineWeb4, is running two commands each time you need to set up a new environment. You don't have to manually install many packages and figuring out what the errors you encounter are.

Additional to that, a Vagrant box uses virtually no resources - only about 40MB RAM and minimal CPU power


Preparations

If you are running Windows, or any other OS where git config --global core.autocrlf true seems like a good idea - revert it to git config --global core.autocrlf false right now. If you are using a decent editor, it can handle UNIX line breaks just fine. If not, get a decent editor.

  • If you installed git with MSysGit or any other tool that gave you the option to set this variable to true:
  • Edit your git/etc/gitconfig file and make sure that the file says autocrlf false. This value actually overwrites gits global config. This might require opening and editing the file in administrative mode, because git has a lock on the file.

Having different settings on this variable on your host and guest machine is NOT good.


Install VirtualBox

Depending on your OS, you may need to put VirtualBox's binary files on your environment path.

Install Vagrant

git clone git@github.com:dotKom/onlineweb4.git

Place yourself in the folder containing the git project, and type

vagrant up

The virtual machine will then proceed installing all packages required by OnlineWeb4.


Setting up your virtual environment

Place yourself in the folder containing the git project, and type

vagrant ssh

If you want you can also ssh to the machine with your regular way of ssh-ing. Username and password are both vagrant

Making sure autocrlf in your git config is set to false

The virtual machine is configured to allow sharing of folders between the guest and host OS. This folder is located at /vagrant. Place youself in the project directory with

cd /vagrant

First make sure that the previously mentioned git config --global core.autocrlf false in your host OS is correct

git status. If this returns a list of all of the files in the project, you most likely have different autocrlf settings on your host and guest machine.

Create a virtual environment for the project

mkvirtualenv -p $(which python3) onlineweb4

Installing required python packages for OnlineWeb4

pip install -r requirements.txt


First run

Sync and migrate DB

python manage.py syncdb --migrate

Create a superuser

python manage.py createsuperuser

Run the server

python manage.py runserver 0.0.0.0:8000

Access the page from your host machine using http://localhost:8001


To get back into the virtual environment next time you fire up the project, type

workon <env name> i.e. workon onlineweb4


Things to know about your Vagrant VM

localhost on your VM defaults to the loopback interface. You must therefor find your eth0 ip address by typing ip a to make the web server accessible by the host machine.

Run your django-server with python manage.py runserver <ip>:8000

  • To reach your standard python manage.py runserver running on port 8080
  • Open your browser on localhost:8080

All of these commands are run on the host OS in your project directory.

  • To start your vm, type vagrant up
  • To suspend your vm, type vagrant suspend
  • To shut down your vm, type vagrant halt
  • To reload your config, type vagrant reload

The current config will set up port-forwarding for you. Port 22 is forwarded to 2222, and 8080 to 8080 on your host OS.

  • You can ssh into your machine with either of these:
  • vagrant ssh
  • ssh vagrant@localhost:2222

TLDR;

  • Host machine:

  • Edit git/etc/gitconfig, replace autocrlf true with autocrlf false

  • git config --global core.autocrlf true

  • Install VirtualBox

  • Install Vagrant

  • git clone git@github.com:dotKom/onlineweb4.git

  • vagrant up

  • vagrant ssh

  • Guest machine:

  • mkvirtualenv onlineweb4

  • cd /vagrant

  • pip install -r requirements.txt

  • python manage.py syncdb --migrate

  • python manage.py createsuperuser

  • python manage.py runserver 0.0.0.0:8000