Deploying Deep Learning Django app to Google Cloud Platform

Akhil Haridasan
Analytics Vidhya
Published in
6 min readJun 17, 2020

Deploying a simple Django application may seem easy on a publicly (free) available hosting website, which to some extent is true. However, the struggle begins when you are working on a deep learning-based application. I have deployed multiple models on free hosting website (Heroku) but I had to pull an all-nighter to finally understand that it’s impossible to host a TensorFlow (not the lite ones) based model to these freely available hosting websites due to the limitation of slug size (500 MB - upload size/compressed file size). Hence, I have decided to write a post on how to host a Tensorflow based app to Google Cloud Platform using a few easy steps.

Link to the issue that I’ve faced with slug size and its possible solution can be found here.

This is one of the techniques (probably the simplest one) of hosting your Django application to GCP.

Step 1:

Creating a Virtual Machine on Google Cloud Platform

  1. Assuming that you don’t have a Google Cloud account, here is the link to create one.
  2. Once you’re ready with the account, click on “Go to Console”.
  3. Use the existing project (named - My First Project) or create a new project.
  4. Now you will see a left navigation bar with a lot of available options. Hover over “Compute Engine” to see all the available options, then click on “VM instances”.
  5. Click on “CREATE INSTANCE”.
  6. Provide a name to your instance in the Name field and keep the rest of the fields as it is (i.e. Region, Zone, Machine Configuration and Container).
  7. The next important step is to select Boot Disk. (Whenever you are working with a deep learning model, keep in mind there is a facility for Deep Learning support on Google Cloud Platform, one of the best things among others). Select Operating System as “Deep Learning on Linux” and Version as “Debian GNU/Linux 10 Buster + TF 2–1”, as I am working with Tensorflow version 2.1.0. You can increase the Disk Size if you want, but 10 GB is more than enough.
  8. Under Firewall section, allow both https and https traffic and finally click on “Create”.

Step 2:

Installing necessary packages (Python3, pip, wget, etc)

  1. Once you create an instance, the next step is to click on “SSH”.
  1. Now, we will install all our packages. A very important step to keep in mind is the version compatibility. When you are working with Tensorflow 2.1.0, it is preferred to have Python 3.6 (recommended version - Python 3.6.10). Or else there is a high possibility that you may encounter the tensor/graph/other unwanted issues.
  2. Install Python3.6 as follows
$ apt update && apt upgrade -y
  1. Installing fresh Python on a Linux/Ubuntu machine requires us to install a bunch of prerequisite libraries as a part of dependencies. I’m honestly not sure what half of these do but yes, I have faced a lot of issues because of the absence of these libraries. Hence, trust me, you won’t regret doing this.
$ sudo apt-get install build-essential checkinstall
$ sudo apt-get install libreadline-gplv2-dev libncursesw5-dev libssl-dev libsqlite3-dev tk-dev libgdbm-dev libc6-dev libbz2-dev libffi-dev zlib1g-dev
  1. Install your Python version by downloading and building the required version from source instead of installing it using “apt-get install”, as it’s easy to do it this way.
$ cd /opt
$ sudo wget https://www.python.org/ftp/python/3.6.10/Python-3.6.10.tgz
$ sudo tar xzf Python-3.6.10.tgz
$ cd Python-3.6.10
$ sudo ./configure --enable-optimizations
$ sudo make altinstall

“make altinstall” will help us install additional versions of Python on our OS.

2. Check the version if it’s properly installed or not.

$ python3 --version
Python 3.6.10

3. Set your Python version to Python 3.6.10 with priority as 1 as follows:

$ update-alternatives --install /usr/bin/python python /usr/bin/python3.6 1

Sometimes it might happen that your Python version is installed in a different folder (most of the times in your /usr/local/lib directory). To set the current python version to Python 3.6 from that directory, use the below command.

$ update-alternatives --install /usr/bin/python python /usr/local/lib/python3.6 1

4. Set Python 3.6.10 to auto mode to use this as default python version

$ update-alternatives --config pythonThere are 2 choices for the alternative python (providing /usr/bin/python).

Selection Path Priority Status
------------------------------------------------------------
* 0 /usr/local/bin/python3.7 2 auto mode
1 /usr/bin/python3.6 1 manual mode
2 /usr/local/bin/python3.7 2 manual mode

Press <enter> to keep the current choice[*], or type selection number:

Enter Selection as 1 to set 3.6 as your default Python version.

5. Now install pip and wget as follows:

$ sudo apt install python-pip
$ sudo apt install wget OR sudo apt-get install wget

Upgrade pip if required or else you can also use the default installed version.

Step 3:

Preparing your Django app for hosting

  1. Navigate to your Django project, find settings.py file and either add the external IP of your VM instance to your allowed hosts (ALLOWED_HOST = [xxx.xxx.xxx.xxx]) or add a ‘*’ to your allowed host (ALLOWED_HOST = [‘*’]) to allow all kinds of traffic.
  1. Add a requirements.txt (if not already present) to your Django Project with all the required libraries/dependencies to run your project. Make sure that your requirements.txt is under root directory and not sub-directories or else cloud won’t be able to read and install it.
  2. Now push your project to Github or create a new repository and copy-paste all your files to that project in Github.
  3. Once you are ready with your project on Github, next step is to clone that to your VM instance.
$ sudo git clone https://github.com/name_of_your_repository.git

4. Change your directory to the cloned project

$ cd name_of_your_repository

5. Now, install requirements.txt from your project

$ sudo pip install -r requirements.txt

It’s gonna take some time to install all the dependencies (mainly your Tensorflow==2.1.0 (421 MB) 😛 Patience is the key guys ✌️).

6. Finally, run your server on the instance as follows:

$ sudo nohup python manage.py runserver 0.0.0.0:80 |tee &

nohup command will help you keep your server running even if you close the terminal, whereas |tee & will keep the server running in the background while disconnecting from stdout. Hence, there won’t be any unnecessary output generated which will use your space. However, if you are experiencing any issues and would like to check the error then use the below command:

$ sudo python manage.py runserver 0.0.0.0:80

This will print all the errors to your terminal.

Congratulations ! You have successfully deployed your Deep Learning-based Django project to GCP.

If you want to access your website then that can be done using the external IP address of your VM instance. For example, if you are using Postman to test your website, put the following as url — http://xxx.xxx.xxxx.xxx (xxx is your external IP). If there is an extension to your url you can use — http://xxx.xxx.xxxx.xxx/extension.

Some additional FYI thing

  1. If you are facing issues like “port already in use”, try this:
$ netstat -tulnap
$ kill -9 portpid

portpid is a unique number (23/234/4567/12446) assigned to http (80) port, which you can figure out from netstat command.

2. If you face issues related to installing Tensorflow or any other large libraries due to size, as all the installation is done at cache level for the first time. Use this:

$ sudo pip install --no-cache-dir tensorflow==2.1.0

3. If you would like to check the size metrics for your VM machine, use this:

$ df -h

This will help you keep track of disk space (used and available), cache, and other memory-related stuffs.

This is my first ever post here. If there are any issues do let me know. Or if you are facing any issue pertaining to Deep Learning models / ML models. You can contact me via LinkedIn or Facebook. Or else comment here itself, feedbacks are always a good way to improve.

I will be posting on how to develop a Django application with easy steps soon. Till then Enjoy coding !! 👍

--

--

Akhil Haridasan
Analytics Vidhya

Tech-savvy individual with good leadership, management and technical skills looking for a platform to learn and showcase my work ethics and skills