Skip to content

Instantly share code, notes, and snippets.

@bb1950328
Last active July 11, 2021 20:31
Show Gist options
  • Save bb1950328/fa1715fd200a12b14f5a6c16d46d18e1 to your computer and use it in GitHub Desktop.
Save bb1950328/fa1715fd200a12b14f5a6c16d46d18e1 to your computer and use it in GitHub Desktop.
Install Python 3.7.3 on Raspbian

How to install Python 3.7 on Raspbian

Reqirements

Of course, you need a Raspberry Pi with a running Raspbian. I have tested this instructions on a Raspi 1 B+ and Raspbian Stretch (Image from 2019-04-08). I think, any newer hard/software will work too.

Compiling

Because there isn't any package for Python 3.7 on armhf (=Architecture of the Raspi), we have to compile Python from sources. This takes a while, but you can leave it completely unattended.

Downloading the sources

  1. Go to https://www.python.org/downloads/source/ and take the newest Release (at the time of writing, its 3.7.3).
  2. Download it with wget:
    wget https://www.python.org/ftp/python/3.7.3/Python-3.7.3.tar.xz
  3. Uncompress it with tar:
    tar -xf Python-3.7.3.tar.xz
  4. Jump in that folder with cd:
    cd Python-3.7.3

Installing dependencies

To compile Python, we need some additional packages:
sudo apt-get install libncurses-dev libreadline-dev tk-dev libsqlite3-dev sqlite3 libgdbm-dev libssl-dev libbz2-dev libexpat1-dev liblzma-dev zlib1g-dev build-essential libncurses5-dev libgdbm-dev libffi-dev libncursesw5-dev libnss3-dev

Configuring

Now we have to decide where we want to install Python. I recommend to install it in /usr/local/opt/python3.7 so you don't touch the preinstalled Python3. This is important, because some administrative tools won't work with Python 3.7. So run:
./configure --prefix=/usr/local/opt/python3.7
This step generates the makefile based on the system configuration.

Build

Now we can run the make command. The option -j 4 tells the compiler that he should use all 4 cores. You can omitt that if your Raspi only has one core.
make -j 4

Installing

When the build has finished, we have to run:
sudo make altinstall
To copy all the files to the prefix we have specified.
To run Python everywhere, we need to add /usr/local/opt/python3.7/bin to the PATH environment variable. In order to do that, start
nano /etc/profile
and append the following line at the bottom:
PATH=$PATH:/usr/local/opt/python3.7/bin

Testing

If everything worked, you can run python3.7 and the interactive shell should start.
Have fun!

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