How to install Python 3.7 on Raspberry Pi

This short guide explains how to install Python version 3.7.0 on a Raspberry Pi running Raspbian aswell as other Debian based Linux distributions including Ubuntu.

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!)
tar xf Python-3.7.0.tar.xz
cd Python-3.7.0
./configure --prefix=/usr/local/opt/python-3.7.0
make -j 4

sudo make altinstall

Make Python 3.7 the default version, make aliases
sudo ln -s /usr/local/opt/python-3.7.0/bin/pydoc3.7 /usr/bin/pydoc3.7
sudo ln -s /usr/local/opt/python-3.7.0/bin/python3.7 /usr/bin/python3.7
sudo ln -s /usr/local/opt/python-3.7.0/bin/python3.7m /usr/bin/python3.7m
sudo ln -s /usr/local/opt/python-3.7.0/bin/pyvenv-3.7 /usr/bin/pyvenv-3.7
sudo ln -s /usr/local/opt/python-3.7.0/bin/pip3.7 /usr/bin/pip3.7
alias python='/usr/bin/python3.7'
alias python3='/usr/bin/python3.7'
ls /usr/bin/python*
cd ..
sudo rm -r Python-3.7.0
rm Python-3.7.0.tar.xz
. ~/.bashrc

And verify:
python -V

And if you want to revert:
update-alternatives --config python

23 thoughts on “How to install Python 3.7 on Raspberry Pi

  1. Following these directions I’m getting errors with pip

    “pip is configured with locations that require TLS/SSL, however the ssl module in Python is not available.”

    I made sure to install the dependencies listed, could there be something else that is missing?


  2. When I try to make py 3 my default I put I get the following error message: ln: failed to create symbolic link ‘/usr/bin/pydoc3.7’: File exists

    Everything worked fine up until this point… I can’t seem to make py 3 my default.

  3. Thanks for the instructions.
    The worked fine except for
    sudo rm -r Python-3.7.0
    rm Python-3.7.0.tar.xz
    because at that point of the sequence, the current location is Python-3.7.0, so a simple
    cd ..
    should probably be added.

    Making the aliases works, too, but they don’t “stick”, i.e. after rebooting
    $ python3
    will invoke python 3.5.4 again.
    I have added
    @reboot alias python3=’/usr/bin/python3.7′
    to crontab, but to now avail (although the line is executed)
    Any ideas? (sorry., I’m pretty much a newbie to Linux/Raspian)
    TIA, Beat

      • and so? I just spent 2 days getting 3.7.0 installed from your great instructions – thank you. Your last line (added?) has typo? you say verify it with python -V I think you mean python3 -V? I did, it came back 3.7. reboot and now it comes back with 3.5 again.

        I am super newbie and no clue. does this need fixed?

        thank you.

    • Adding a file in /etc/profile.d/ called and putting your alias commands in there is the way to make aliases stick. These files are sourced when you start a shell, so the aliases will be set then.

  4. Can you simply do:
    alias python3=’/usr/local/opt/python-3.7.0/bin/python3.7′

    since that’s where you installed to? Then when you do “python3 -V” it outputs 3.7.0 properly.

  5. raspbian stretch doesn’t know where to get libffi_dev so python install failes:
    Failed to build these modules:
    `E: Unable to locate package libffi_dev`

  6. worked fine on my pi3. Thanks for the great write up! also, the output from python -V echos python 3.7.0 .

  7. I just want to to install Python 3.7 but not to make it default. I don’t want to change de default Python. For that, which steps do I ignore?

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.