How to Install Python 3.9 on the Raspberry Pi
What and why?
I use Python for various purposes all my Raspberry Pi boards. I run automated scripts, web applications that help in home automation, web controllable Christmas lights and more recently AI/machine learning.
The default Python that comes with Raspberry Pi OS is sometimes outdated, and I prefer to use the latest Python on python.org. You can manually install the latest version of Python using these steps.
Download Python 3.9 source
First, setup Raspberry Pi OS on your microSD card. You can read my previous blog post on how to do it.
Run this command to download the latest Python source code.
Install the build tools
Run these to install the build tools.
sudo apt install build-essential zlib1g-dev libncurses5-dev libgdbm-dev libnss3-dev
If you want, you can install git as well.
sudo apt install git
Configure, make and make install
Run these commands. The configure command may take several minutes depending on your Raspberry Pi.
tar -xzvf Python-3.9.6.tgz cd Python-3.9.6/ ./configure --enable-optimizations sudo make altinstall
This will install Python at
/usr/local/bin/python3.9. To test the version, run this:
You will get this output:
Make Python 3.9 the default version
When you run
python, you will still get the default 3.7.3.
$ /usr/bin/python3 -V Python 3.7.3
To make the default version of Python 3.9.6, run this:
sudo rm /usr/bin/python sudo ln -s /usr/local/bin/python3.9 /usr/bin/python
Test whether Python 3.9.6 is the default version:
$ python -V Python 3.9.6
Let me know whether you have any problems installing Python 3.9 in your Raspberry Pi.
Created on 10 August 2021
Affiliate Disclosure: Some of the links to products on this blog are affiliate links. It simply means, at no additional cost to you, we’ll earn a commission if you click through and buy any product.