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!)
wget https://www.python.org/ftp/python/3.7.0/Python-3.7.0.tar.xz
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

Install
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

30 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?

    Thanks

  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 aliases.sh 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:
    _ctypes
    “`
    `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?

  8. I’m a total noob with the Raspberry and was wondering if you could tell me why I am getting an error that tells me that my permission was denied each time I try to install a package. If it helps here is an example of the error: Could not install packages due to an EnvironmentError: [Errno 13] Permission denied: ‘/usr/local/opt/python-3.7.0/lib/python3.7/site-packages/speech_recognition’
    Consider using the `–user` option or check the permissions.

  9. Thanks for this guide.
    The alias didn’t seem to be working for me, so looking further afield I found this site:
    https://linuxconfig.org/change-default-python-version-on-raspbian-gnu-linux
    …which gave me inspiration to try:
    sudo update-alternatives –install /usr/bin/python python /usr/bin/python2.7 1
    sudo update-alternatives –install /usr/bin/python python /usr/bin/python3.5 2
    sudo update-alternatives –install /usr/bin/python python /usr/bin/python3.7 3

    So far, this approach seems to be working. Perhaps it will help someone else too. The caveat being that I don’t really know what I’m doing, so please correct me if this is wrong.

  10. I followed this guide but I’m having trouble when trying to install modules for some reason. Every time I try to install one using pip install I get told that I don’t have the permissions to do so.

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.