Categories
Computers

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

45 replies on “How to install Python 3.7 on Raspberry Pi”

tested on rpi2b+ and installs ok, but does not become default python3
what do i need to do to make python3.7 default
using your amended script, rpi still thinks python is version 3.5.4

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

I am getting the same error… but haven’t been able to fix it yet. I followed the instructions in the stackoverflow page too…

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.

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.

Those aliases need to be added in the user’s .bashrc file. These files exists in the home directory of the user that you currently logged in as.

1) If you know how to edit files using ‘vi’, then open file using command – “vi ~/.bashrc”, add the following aliases at the end of the file, save and exit.

alias python=’/usr/bin/python3.7′
alias python3=’/usr/bin/python3.7′

Then,
~/.bashrc (run this command) or alternatively close and open your terminal window.

2) if you don’t know how to edit file using ‘vi’, then simply run the following commands:
a) cp ~/.bashrc ~/.bashrc.old (take a copy of this file before making any changes. You can revert to this copy if things go bad)
b) echo “alias python3=’/usr/bin/python3.7′”>>~/.bashrc (make sure there are 2 of these – ‘>’)
c) echo “alias python=’/usr/bin/python3.7′”>>~/.bashrc
d) ~/.bashrc (run this command) or alternatively close and open your terminal window.

From now on, your “python” command always refers to python3.7 version.

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`

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

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?

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.

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.

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.

I’m trying to figure out how to install python3.7-dev. I need to install some packages with pip that use gcc etc and I think this is what’s giving me the exit status 1 error.
sudo apt-get install python3.7-dev tells me that it can’t find any packages.

Also, to address a comment earlier about not being able to install packages because of permissions, the recommended way is to add –user to the lines. Sudo pip install is not recommended (based on reading I’ve done).
So an example would be:
pip3.7 install –user pyusb

Thank for your instructions. Upgrading Python ok. However I am getting issues with pip.

after all linkings and setting aliases I typed ls /usr/bin/pip*
pip3.7 link is in red color. It means archived.

pi@raspberrypi:~ $ pip -V
pip 9.0.1 from /usr/lib/python2.7/dist-packages (python 2.7)
pi@raspberrypi:~ $ pip3 -V
Traceback (most recent call last):
File “/usr/bin/pip3”, line 9, in
from pip import main
ImportError: cannot import name ‘main’
pi@raspberrypi:~ $ pip3.7 -V
-bash: pip3.7: command not found
pi@raspberrypi:~ $

Got the following file already exists errors from following the script, maybe those steps are no longer required.

pi@raspberrypi:~/Python-3.7.0 $ sudo ln -s /usr/local/opt/python-3.7.0/bin/pydoc3.7 /usr/bin/pydoc3.7
ln: failed to create symbolic link ‘/usr/bin/pydoc3.7’: File exists
pi@raspberrypi:~/Python-3.7.0 $ sudo ln -s /usr/local/opt/python-3.7.0/bin/python3.7 /usr/bin/python3.7
ln: failed to create symbolic link ‘/usr/bin/python3.7’: File exists
pi@raspberrypi:~/Python-3.7.0 $ sudo ln -s /usr/local/opt/python-3.7.0/bin/python3.7m /usr/bin/python3.7m
ln: failed to create symbolic link ‘/usr/bin/python3.7m’: File exists

Installed on Pi 4

I am very new to all this so please bear withme. I have gone through this and some other procedures to get python3 installed. got that done, now I need to make sure python 3 (3.7.3) is the default. The alias works, but I am doing this to get polyglot running. currently when I install a node server, it says invalid type python 3. I started out with python 2.7. I have polyglot running at system startup, so I need to figure out how to make sure python 3.7 is the default at that time. I tried to do the update-alternative steps in the link above, but no joy there either. thanks for any help!

I just went through this on a Pi zero W and it worked fine. Python 3.7.3 is up and running.
i@raspberrypi:~ $ python
Python 3.7.3 (default, Apr 3 2019, 05:39:12)
[GCC 8.2.0] on linux
Type “help”, “copyright”, “credits” or “license” for more information.
>>> exit()
pi@raspberrypi:~ $ python -V
Python 3.7.3

Leave a Reply

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