[HowTo] Install Kivy and Buildozer on Ubuntu 15.04

Installing Kivy and building an apk file is the fundamental step of coding Android applications with Python. In this post, Buildozer is used for building apk, installing sdk, ndk and other dependencies automatically.

All commands on this post are applied on a fresh installed and newly updated (02/07/2015) Ubuntu 15.04. Use it at your own risk.

Firstly, we should install system-wide dependencies:

sudo apt-get install -y build-essential mercurial git python2.7 \
python-setuptools python-dev ffmpeg libsdl-image1.2-dev \
libsdl-mixer1.2-dev libsdl-ttf2.0-dev libsmpeg-dev libsdl1.2-dev \
libportmidi-dev libswscale-dev libavformat-dev libavcodec-dev zlib1g-dev

And other system-wide requirements which should be at current version. First command of each will remove existing versions on computer.

Warning: Running the remove commands below may remove necessary dependencies as well. As a result, some Linux distros may fail after login screen. In this case, for Linux Mint Cinnamon, reinstalling Cinnamon in tty is worked but not guaranteed. Use them at your own risk. You do not have to use remove commands, they are here just because of being in official documentation.

For Pip and virtualenv

sudo apt-get remove --purge -y python-virtualenv python-pip
sudo easy_install-2.7 -U pip
sudo pip2.7 install -U virtualenv

For Cython:

sudo apt-get remove --purge -y cython
sudo pip install cython==0.21

For Numpy:

sudo apt-get remove --purge -y python-numpy
sudo pip2.7 install -U numpy

And finally, for Pygame:

hg clone https://bitbucket.org/pygame/pygame
cd pygame
python2.7 setup.py build
sudo python2.7 setup.py install
cd ..
sudo rm -rf pygame/

After this step, a virtual environment is created for Kivy. If you want to use different Kivy versions at the same time, you can create new virtual environments as many as you want. It is possible to install different modules for each virtual environment as its the nature of it.

Firstly, this tutorial will install it to your home folder. You can change it to anywhere you can find easily if you want. First command removes the folder and everything in it if there is a folder named venv.

rm -rf venv

Afterwards, a virtual environment is created in folder venv:

virtualenv -p python2.7 --system-site-packages venv

Then, stable version of Kivy is installed on this virtual environment:

venv/bin/pip install kivy

Afterwards, Buildozer, which is necessary to make an apk file is installed as follows:

venv/bin/pip install git+https://github.com/kivy/buildozer.git@master

Plyer is another module for Kivy which provides many interfaces to different devices and operating systems in common structures:

venv/bin/pip install git+https://github.com/kivy/plyer.git@master

Finally, KivyCatalog requirements are installed:

venv/bin/pip install -U pygments docutils

After this step, you should be able to import Kivy in your virtual environment. To activate the virtual environment, you can use this command:

source venv/bin/activate

If everything goes on well, you should see (venv) before your username at console. Then you can open python as regular:

(venv)itugnu@gnu:~$ python
Python 2.7.9 (default, Apr  2 2015, 15:33:21) 
[GCC 4.9.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import kivy
[INFO   ] [Logger      ] Record log in /home/itugnu/.kivy/logs/kivy_15-07-02_2.txt
[INFO   ] [Kivy        ] v1.9.0
[INFO   ] [Python      ] v2.7.9 (default, Apr  2 2015, 15:33:21) 
[GCC 4.9.2]

At this step, you have been installed Kivy’s stable release. Now it is time to make apk files using Buildozer. Buildozer is not the only way to build apk. It just automatizes the process and provides you more time. It also supports deploying the application to the phone and run it on the phone. In one line you can compile your code, upload to your mobile phone/tablet and run it. Another post will cover how to see logcat results in order to provide debug data.

You can find the second part here.

Further Reading & Resource:


comments powered by Disqus