How to install Python 3.9.1 on Raspberry Pi

This short guide explains how to install Python version 3.9.1 on a Raspberry Pi running Raspbian.

First install the dependencies needed to build

sudo apt-get update
sudo apt-get install -y build-essential tk-dev libncurses5-dev libncursesw5-dev libreadline6-dev libdb5.3-dev libgdbm-dev libsqlite3-dev libssl-dev libbz2-dev libexpat1-dev liblzma-dev zlib1g-dev libffi-dev

Compile (takes a while!)
wget https://www.python.org/ftp/python/3.9.1/Python-3.9.1.tar.xz
tar xf Python-3.9.1.tar.xz
cd Python-3.9.1
./configure --prefix=/usr/bin/python3.9.1
make -j 4

Install
sudo make altinstall

And remove the files

cd ..
sudo rm -r Python-3.9.1
rm Python-3.9.1.tar.xz
. ~/.bashrc

Make Python 3.9.1 the default version, make aliases
sudo update-alternatives --config python

And verify:
python -V

6 thoughts on “How to install Python 3.9.1 on Raspberry Pi

  1. followed this on a fresh install of Raspbain, after trying the other install of 3.9.0 on a different sd card but it is failing for the same reason

    sudo update-alternatives –config python
    update-alternatives: error: no alternatives for python

    no error messages other then this

    Installing collected packages: setuptools, pip
    WARNING: The script easy_install-3.9 is installed in ‘/usr/bin/python3.9.1/bin’ which is not on PATH.
    Consider adding this directory to PATH or, if you prefer to suppress this warning, use –no-warn-script-location.
    WARNING: The script pip3.9 is installed in ‘/usr/bin/python3.9.1/bin’ which is not on PATH.
    Consider adding this directory to PATH or, if you prefer to suppress this warning, use –no-warn-script-location.
    Successfully installed pip-20.2.3 setuptools-49.2.1

    Any advice ? This is in a Pi400 16gb sd and full update / upgrade / dist-upgrade was run first and rebooted.

      • rebooted and the PATH is back to the default. I changed /etc/.bashrc and local but $PATH is the same
        pi@raspberrypi:~ $ echo $PATH
        /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/local/games:/usr/games

        No matter what I do the PATH wont update

        pi@raspberrypi:~ $ python -V
        Python 2.7.16
        pi@raspberrypi:~ $ python2 -V
        Python 2.7.16
        pi@raspberrypi:~ $ python3 -V
        Python 3.7.3

        python -c ‘import sys; print(sys.path)’
        [”, ‘/home/pi’, ‘/usr/bin/python3.9.1/bin’, ‘/usr/lib/python2.7’, ‘/usr/lib/python2.7/plat-arm-linux-gnueabihf’, ‘/usr/lib/python2.7/lib-tk’, ‘/usr/lib/python2.7/lib-old’, ‘/usr/lib/python2.7/lib-dynload’, ‘/usr/local/lib/python2.7/dist-packages’, ‘/usr/lib/python2.7/dist-packages’, ‘/usr/lib/python2.7/dist-packages/gtk-2.0’]

        pi@raspberrypi:~ $ echo $PATH
        /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/local/games:/usr/games

        both /etc.bashrc and ~/.bashrc have the following as the last line
        export PYTHONPATH=”${PYTHONPATH}:/usr/bin/python3.9.1/bin”

        but when I reboot and type echo $PATH I get
        pi@raspberrypi:~ $ echo $PATH
        /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/local/games:/usr/games

        pi@raspberrypi:~ $ . ~/.bashrc
        pi@raspberrypi:~ $ sudo update-alternatives –config python
        update-alternatives: error: no alternatives for python

        Im guessing because the /python3.9.1/bin is not in the path ?

        but I cant get it to add to the path.

        Any idea why ?

        Sorry to keep bugging you with these but I ahve learnt a lot in just looking into the errors

  2. ok sorted it by following another install, which is similar to yours.

    sudo apt update
    sudo apt upgrade
    sudo apt-get install -y build-essential tk-dev libncurses5-dev libncursesw5-dev libreadline6-dev libdb5.3-dev libgdbm-dev libsqlite3-dev libssl-dev libbz2-dev libexpat1-dev liblzma-dev zlib1g-dev libffi-dev tar wget vim

    wget https://www.python.org/ftp/python/3.9.1/Python-3.9.1.tar.xz
    tar xf Python-3.9.1.tar.xz
    cd Python-3.9.1
    ./configure –enable-optimizations
    sudo make -j 4
    sudo make altinstall

    adding the alias to file .bash_aliases in your home directory
    alias python=’/usr/local/bin/python3.9′

    pi@raspberrypi:~ $ python -V
    Python 3.9.1
    pi@raspberrypi:~ $ python2 -V
    Python 2.7.16
    pi@raspberrypi:~ $ python3 -V
    Python 3.7.3
    pi@raspberrypi:~ $ python3.9 -V
    Python 3.9.1

    Thanks for your posting it was helpful

    • Hi Aaron This was really helpful. thanks for that. I managed to get it working with your instructions.

Leave a Reply

Your email address will not be published. Required fields are marked *