Setting Up Alpine Linux For Django Development

TL;DR

Here’s the bash script. Just change your shell to bash, install vagrant plugin and edit Vagrantfile settings. Then run the script.


If you’re here, then you probably know what Alpine is. So here’s a tutorial on how I setup my Django projects with it. (Projects are in Python 3.5). Also, I assume you have vagrant installed.

Installing the Plugin

I use Vagrant for VM’s since it’s convenient but before we can install our Alpine image we have to install a plugin that will make our lives easier. It basically “patches” some of the vagrant commands to work with Alpine. Just follow the installation instructions there.

Setting up the VM

After that we can go ahead and create our VM. Go to any directory that you want your project to be in and run:

vagrant init maier/alpine-3.4-x86_64

This will create a file called Vagrantfile. Let’s go ahead on edit this file and uncomment the lines that have the following:

config.vm.network "forwarded_port", guest: 80, host: 8000
config.vm.network "private_network", ip: "192.168.33.10"
config.vm.synced_folder "../data", "/vagrant_data"

The first line is pretty self-explanatory, we access port 8000 on our local machine and then it goes, fetches and displays whatever port 80 on our VM points to. The second line is needed by the line below it. The final line is pretty important too, it’s so we can share the folders/files between machines, this makes life a whole lot easier. Let’s edit that line so that it fits our needs. On my directory I have another directory called project and I place everything there, so let’s re-write this to:

config.vm.synced_folder 'project/', '/home/vagrant/', type: 'nfs'

I just placed everything in the home directory of the VM, you can change it to wherever you’d like. You probably noticed the type argument as well. We need to pass this so that we can sync folders/files without any problem, I couldn’t get it to work through the traditional way, so this is a work around.

Update the Package Manager

Now that everything is setup, let’s start our VM with.

vagrant up

You should start seeing text popup, just let it all go through. Eventually you should see some errors too. Don’t panic, that’s what supposed to happen because the VM needs to fix it’s repo url. After we fix that, the next time you boot you should be asked to enter your password, this is so that we can use NFS.

Once it’s all done throwing errors, run:

vagrant ssh

If it asks you for a password, it’s vagrant. Now just update and upgrade.

sudo apk update
sudo apk upgrade

Changing Shells

By default, Alpine sets you up with /bin/ash but I like /bin/bash. Unfortunately there is no chsh command so we’re going to have to edit /etc/passwd manually.

You should see something like this:

vagrant:x:1000:1000:Linux User,,,:/home/vagrant:/bin/ash

just change the last bit to:

vagrant:x:1000:1000:Linux User,,,:/home/vagrant:/bin/bash

Installing Python 3 and Pip

Alright, now for Python. This is where it gets easy. Just run:

sudo apk add python3 python3-dev gcc musl-dev jpeg-dev zlib-dev

You should be able to use Python 3.5 after running python3 on the shell. The other stuff is just dependencies that we’ll need later on.

Next, is pip. These commands should do the trick:

wget https://bootstrap.pypa.io/get-pip.py
sudo python3 get-pip.py
rm get-pip.py

Ta-da! We have pip installed too. Life is wonderful huh?

Installing Postgresql

I’ll be using Postgresql for the database in this tutorial. So here’s how to install it.

sudo apk add postgresql postgresql-client postgresql-dev

Next, let’s start the service.

sudo /etc/init.d/postgresql setup
sudo /etc/init.d/postgresql start

If you want to start the DB on boot, do rc-update add postgresql.

Let’s setup the the DB

sudo psql -U postgres -c "CREATE DATABASE tutorial";
sudo psql -U postgres -d tutorial -c "CREATE USER vagrant WITH PASSWORD 'password'";
sudo psql -U postgres -d tutorial -c "GRANT ALL PRIVILEGES ON DATABASE tutorial to vagrant";

Getting virtualenvwrapper

We’ll use virtualenvwrapper for our needs

sudo pip install virtualenvwrapper

Next, let’s add this to our .bashrc file.

export WORKON_HOME=$HOME/.virtualenvs
export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3.5
source /usr/bin/virtualenvwrapper.sh

The just run source .bashrc. Note: If you’re not using bash then sourcing the file will raise errors.

Nginx

Finally, we can just install nginx using the package manager.

sudo apk add nginx

Next Steps.

If you want a conf file for nginx and gunicorn working together then I recommend you read this post by another person. It’s easy to follow.