Ubuntu16.04下Anaconda3 Python3.5.4环境安装Kivy失败求助
Hey there, I’ve dealt with this exact headache before when setting up Kivy in a Conda environment on Ubuntu! The issue here is that even though you installed the system-level dependencies as per the Kivy docs, your isolated Conda environment isn’t picking up those system libraries during the pip compilation process. Let’s walk through the fixes step by step:
Step 1: Verify & Install SDL2 Development Libraries
First, double-check that you have all the required SDL2 development packages installed (the linker needs these, not just the runtime versions). Run this command to install or confirm them:
sudo apt-get update && sudo apt-get install libsdl2-dev libsdl2-ttf-dev libsdl2-image-dev libsdl2-mixer-dev
This ensures the linker can find the -lSDL2 family of libraries it’s complaining about.
Step 2: Tell Conda Where to Find System Libraries
Conda environments are designed to be isolated, so they don’t automatically use system-wide library paths by default. We need to set environment variables to point the compiler to the right locations:
Activate your Conda environment first:
source activate buildingExport the required flags to let pip’s compilation process locate the SDL2 headers and libraries:
export CFLAGS="-I/usr/include/SDL2" export LDFLAGS="-L/usr/lib/x86_64-linux-gnu"Note: If you’re on a 32-bit system, replace
x86_64-linux-gnuwithi386-linux-gnu.To make this permanent (so you don’t have to export these every time), create an environment variable script in your Conda environment:
- Navigate to your environment’s activate.d directory:
mkdir -p $CONDA_PREFIX/etc/conda/activate.d - Create a file called
env_vars.shwith the export commands:echo 'export CFLAGS="-I/usr/include/SDL2"' >> $CONDA_PREFIX/etc/conda/activate.d/env_vars.sh echo 'export LDFLAGS="-L/usr/lib/x86_64-linux-gnu"' >> $CONDA_PREFIX/etc/conda/activate.d/env_vars.sh
Next time you activate the environment, these variables will load automatically.
- Navigate to your environment’s activate.d directory:
Step 3: Reinstall Cython & Kivy
First, clean up any partial installations from your previous attempts:
pip uninstall -y cython kivy
Then reinstall in the correct order:
pip install cython==0.25.2 pip install kivy
Alternative: Use Conda-Forge for Kivy (No Compilation Needed)
If the above steps still give you trouble, skip the pip compilation entirely and use Conda’s pre-built packages from conda-forge. This handles all dependencies automatically:
source activate building conda install -c conda-forge kivy
Why This Happens
When you install Kivy via pip, it compiles the library from source, which requires linking against the SDL2 development libraries. Conda’s isolation means it doesn’t look in system paths by default, so we have to explicitly tell the compiler where to find those files. Using conda-forge avoids this because it packages all required dependencies within the Conda ecosystem.
内容的提问来源于stack exchange,提问作者Akash Jobanputra

