Compile/install Python 3 on Raspberry Pi (2024)

Raspberry Pi OS includes Python 3.Here is how to compile the latest Python on the Raspberry Pi.

If you need to use apt installed Python modules that access hardware like GPIO, you can always access system Python 3 via /usr/bin/python3.In general, one of the few times you should ever use sudo on a Linux computer is apt install.

Get prereqs on your ARM device:

apt install libffi-dev libbz2-dev liblzma-dev libsqlite3-dev libncurses5-dev libgdbm-dev zlib1g-dev libreadline-dev libssl-dev tk-dev build-essential libncursesw5-dev libc6-dev openssl git

Extract the latest Pythonsource code

Configure (3 minutes on Raspberry Pi 2):

cd cpython-3.*./configure --prefix=$HOME/.local --enable-optimizations

Build and install–this step takes 10-40 minutes, depending on Raspberry Pi model.Do not use sudo!

make -j -l 4make install

Note: don’t omit -l 4 or Pi will be quickly overwhelmed and error build.This limits load average to 4. Without it, load average will soar to 100+ (bad).

Add to ~/.profile:

export PATH=$HOME/.local/bin/:$PATH

then open a new Terminal.Check that which python3 and which pip3 etc. refer to ~/.local/bin/ instead of /usr/bin.Don’t uninstall system Python 3 /usr/bin/python3 because system packages depend on it.The PATH you set in Step 5 above makes Linux prefer the new Python.

reference

Compile/install Python 3 on Raspberry Pi (2024)
Top Articles
Latest Posts
Article information

Author: Dong Thiel

Last Updated:

Views: 5511

Rating: 4.9 / 5 (79 voted)

Reviews: 86% of readers found this page helpful

Author information

Name: Dong Thiel

Birthday: 2001-07-14

Address: 2865 Kasha Unions, West Corrinne, AK 05708-1071

Phone: +3512198379449

Job: Design Planner

Hobby: Graffiti, Foreign language learning, Gambling, Metalworking, Rowing, Sculling, Sewing

Introduction: My name is Dong Thiel, I am a brainy, happy, tasty, lively, splendid, talented, cooperative person who loves writing and wants to share my knowledge and understanding with you.