Kivy - Installation



To build a Kivy application, you need to have Python installed in your computer. Kivy 2.2.0, the latest stable version, officially supports Python versions 3.7 to 3.11. If Python is not already installed, download the installer of latest Python version, appropriate for your operating system and architecture, from Python's official website − https://www.python.org/downloads/

Python Virtual Environment

Python recommends the use of virtual environment to avoid conflicts with other Python versions and packages.

A virtual environment allows us to create an isolated working copy of Python for a specific project without affecting the outside setup. We shall use the "venv" module in Python's standard library to create virtual environment. PIP is included by default in Python version 3.4 or later.

Creating a Virtual Environment

Use the following command to create a virtual environment in Windows −

C:\users\user\>python -m venv c:\kivyenv

On Ubuntu Linux, update the APT repo and install "venv", if required, before creating a virtual environment.

mvl@GNVBGL3:~ $ sudo apt update && sudo apt upgrade -y
mvl@GNVBGL3:~ $ sudo apt install python3-venv

Then, use the following command to create a virtual environment −

mvl@GNVBGL3:~ $ sudo python3 -m venv kivyenv

Activating a Virtual Environment

You need to activate the virtual environment. On Windows, use the following command −

C:\>cd kivyenv
C:\kivyenv>scripts\activate
(kivyenv) C:\kivyenv>

On Ubuntu Linux, use the following command to activate the virtual environment −

mvl@GNVBGL3:~$ cd kivyenv
mvl@GNVBGL3:~/kivyenv$ source bin/activate
(myenv) mvl@GNVBGL3:~/kivyenv$

Installing Kivy Using the pip Utility

The easiest way to install any Python package is with the use of "pip" utility. Python 3 installation comes with the "pip" installer. After activating the virtual environment, use the following command from the CMD terminal in Windows or Linux terminal −

pip3 install "kivy[base]" kivy_examples

This installs the Kivy package with minimal dependencies. The "kivy_examples" package is optional. Instead of "base", the "full" option enables audio/video support.

Installing the Dependency Libraries for Kivy

SDL2 (Simple DirectMedia Layer) is a major dependency for Kivy. On Windows OS, SDL2 is automatically installed when you use the "pip" utility. However, for Linux and macOS, you need to install SDL2 separately.

On macOS, you can install SDL2 using Homebrew by running the following command in your terminal −

brew install sdl2

If on Linux OS, use the corresponding package manager to install SDL2. For example, it is done with the following command on Ubuntu Linux machine −

sudo apt-get install libsdl2-dev

Additionally, you may have to install other dependencies such as "gstreamer" and "Pillow" for certain specific features of Kivy.

Verifying the Kivy Installation

To verify if Kivy has been properly installed, start the Python interactive shell and import the package. The console shows that the Kivy dependencies are also imported.

>>> import kivy
[INFO] [Logger] Record log in C:\Users\mlath\.kivy\logs\kivy_23-05-26_0.txt
[INFO] [deps] Successfully imported "kivy_deps.gstreamer" 0.3.3
[INFO] [deps] Successfully imported "kivy_deps.angle" 0.3.3
[INFO] [deps] Successfully imported "kivy_deps.glew" 0.3.1
[INFO] [deps] Successfully imported "kivy_deps.sdl2" 0.6.0
[INFO] [Kivy] v2.2.0
[INFO] [Kivy] Installed at "c:\kivyenv\Lib\site-packages\kivy\__init__.py"
[INFO] [Python] v3.11.2 (tags/v3.11.2:878ead1, Feb 7 2023, 16:38:35) [MSC v.1934 64 bit (AMD64)]
[INFO] [Python] Interpreter at "c:\kivyenv\Scripts\python.exe"
[INFO] [Logger] Purge log fired. Processing...
[INFO] [Logger] Purge finished!

You can also obtain the list of all the packages installed using the "pip freeze" command −

(kivyenv) C:\kivyenv>pip3 freeze
certifi==2023.5.7
charset-normalizer==3.1.0
docutils==0.20.1
idna==3.4
Kivy==2.2.0
kivy-deps.angle==0.3.3
kivy-deps.glew==0.3.1
kivy-deps.gstreamer==0.3.3
kivy-deps.sdl2==0.6.0
Kivy-examples==2.2.0
Kivy-Garden==0.1.5
Pillow==9.5.0
Pygments==2.15.1
pypiwin32==223
pywin32==306
requests==2.31.0
urllib3==2.0.2
Advertisements