Skip to content

Instantly share code, notes, and snippets.

@M4lF3s
Last active April 19, 2023 09:26
Show Gist options
  • Save M4lF3s/e8b9a691b9ace611370960cce893a0a5 to your computer and use it in GitHub Desktop.
Save M4lF3s/e8b9a691b9ace611370960cce893a0a5 to your computer and use it in GitHub Desktop.
Kivy 2.1.0 Setup on Raspbian Bullseye Lite (headless) w/ SDL2 Window and using official 7" Touchscreen Display on a Raspberry Pi 4B

Initial Setup

  • Flash SD Card (use official Raspbian Imager -> saves you the hassle to Setup Wifi, Pi User, etc. headless)
  • Insert and Boot
  • Run sudo raspi-config and expand the FileSystem
  • Update with
    sudo apt-get update
    sudo apt-get upgrade
  • If asked for what to do with modified sshd config, choose: keep the local version currently installed
  • Reboot

Python Setup with PyEnv and virtualenv

  • Install Git
    sudo apt-get install git
  • Run PyEnv Installer Script and Setup Environment
    curl https://pyenv.run | bash
    echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bashrc
    echo 'command -v pyenv >/dev/null || export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bashrc
    echo 'eval "$(pyenv init -)"' >> ~/.bashrc
    echo 'eval "$(pyenv virtualenv-init -)"' >> ~/.bashrc
    exec "$SHELL"
  • Install Prerequisites for Python 3.9 Installation through PyEnv
    sudo apt-get install build-essential zlib1g-dev libffi-dev libssl-dev libbz2-dev libreadline-dev libsqlite3-dev liblzma-dev
  • Install Python 3.9
    pyenv install 3.9.13
  • Create virtualenv for development
    pyenv virtualenv 3.9.13 venv39

Compiling SDL from Source for Kivy

  • Install Prerequisites for Kivy
    sudo apt install pkg-config libgl1-mesa-dev libgles2-mesa-dev \
    libgstreamer1.0-dev \
    gstreamer1.0-plugins-{bad,base,good,ugly} \
    gstreamer1.0-alsa libmtdev-dev \
    xclip xsel libjpeg-dev
  • Install Prerequisites for SDL2
    sudo apt-get install libfreetype6-dev libgl1-mesa-dev libgles2-mesa-dev libdrm-dev libgbm-dev libudev-dev libasound2-dev liblzma-dev libjpeg-dev libtiff-dev libwebp-dev git build-essential
    
    sudo apt-get install gir1.2-ibus-1.0 libdbus-1-dev libegl1-mesa-dev libibus-1.0-5 libibus-1.0-dev libice-dev libsm-dev libsndio-dev libwayland-bin libwayland-dev libxi-dev libxinerama-dev libxkbcommon-dev libxrandr-dev libxss-dev libxt-dev libxv-dev x11proto-randr-dev x11proto-scrnsaver-dev x11proto-video-dev x11proto-xinerama-dev
  • Install SDL2
    wget https://libsdl.org/release/SDL2-2.0.22.tar.gz
    tar -zxvf SDL2-2.0.22.tar.gz
    pushd SDL2-2.0.22
    ./configure --enable-video-kmsdrm --disable-video-opengl --disable-video-x11 --disable-video-rpi
    Output of ./configure ... should be someting like:
    ...
    Enabled modules : atomic audio video render events joystick haptic hidapi sensor power filesystem threads timers file misc locale loadso cpuinfo assembly
    Assembly Math   :
    Audio drivers   : disk dummy oss alsa(dynamic) sndio(dynamic)
    Video drivers   : dummy kmsdrm(dynamic) opengl_es1 opengl_es2 vulkan wayland(dynamic)
    Input drivers   : linuxev linuxkd
    Enable virtual joystick APIs : YES
    Using libsamplerate : NO
    Using libudev       : YES
    Using dbus          : YES
    Using ime           : YES
    Using ibus          : YES
    Using fcitx         : YES
    
    Now Compile and Install
    make -j4
    sudo make install
    popd
  • Install SDL2_image
    wget https://libsdl.org/projects/SDL_image/release/SDL2_image-2.0.5.tar.gz
    tar -zxvf SDL2_image-2.0.5.tar.gz
    pushd SDL2_image-2.0.5
    ./configure
    make -j4
    sudo make install
    popd
  • Install SDL2_mixer
    wget https://github.com/libsdl-org/SDL_mixer/releases/download/release-2.6.1/SDL2_mixer-2.6.1.tar.gz
    tar -zxvf SDL2_mixer-2.6.1.tar.gz
    pushd SDL2_mixer-2.6.1
    ./configure
    make -j4
    sudo make install
    popd
  • Install SDL2_ttf
    wget https://github.com/libsdl-org/SDL_ttf/releases/download/release-2.20.0/SDL2_ttf-2.20.0.tar.gz
    tar -zxvf SDL2_ttf-2.20.0.tar.gz
    pushd SDL2_ttf-2.20.0
    ./configure
    make -j4
    sudo make install
    popd
  • Update dynamic libraries cache
    sudo ldconfig -v

Compile Kivy from source

  • Change into a Project Directory

  • Set Python Env to Virtualenv created above

    pyenv local venv39
  • Upgrade Python libraries

    python -m pip install --upgrade pip setuptools
  • Install Kivy 2.1.0 from source

    python -m pip install "kivy[base] @ https://github.com/kivy/kivy/archive/023bd79b90f9831b45bb8eb449346648aa5fe5f8.zip"
  • Test Everything with a simple HelloWorld Program:

    main.py

    from kivy.app import App
    from kivy.uix.label import Label
    
    
    class MainApp(App):
        def build(self):
            return Label(text="Hello, World")
    
    
    MainApp().run()
  • Run it with

    python main.py
@M4lF3s
Copy link
Author

M4lF3s commented Apr 19, 2023

Some Notes

  • This works on Raspbian Buster Lite 32 and 64 bit Pi3 and Pi4 using official 7" Touchscreen or HDMI
  • RPi3 uses older dispmanx API for rendering image layers on screen: https://info-beamer.com/blog/raspberry-pi-hardware-video-scaler
  • This means Kivy can be used with Legacy OpenGL Driver + egl_rpi Kivy Backend or KMS Driver and sdl2 Backend
  • Problem with KMSDRM Driver is that it does not use dispmanx layers which means VNC connections are not showing Kivy App (only Console)
  • RPi4 need to enable fake KMS Driver if Kivy app should also be visible over vnc: edit /boot/config.txt and change vc4-kms-v3d to vc4-fkms-v3d
  • For Pi3 Kivy + VNC Setup use Legacy Driver (enabled in raspi-config) and egl_rpi Kivy Backend

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment