The default Python version that comes with Raspberry Pi OS (Raspbian) Bookworm aka Debian 12 is 3.11.2, and is not outdated by any means. I still prefer to build and use the latest Python on python.org. You can manually install the latest version of Python using these steps.
Python 3.12.3 was released on April 9, 2024. This version is the third maintenance release, with more than 300 bugfixes since 3.12.2. This article helps you install Python 3.12.3 on your Raspberry Pi.
Why use Python on the Raspberry Pi?
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.
Download Python 3.12.3 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.
wget https://www.python.org/ftp/python/3.12.3/Python-3.12.3.tgz
$ wget https://www.python.org/ftp/python/3.12.3/Python-3.12.3.tgz
--2024-05-21 14:23:57-- https://www.python.org/ftp/python/3.12.3/Python-3.12.3.tgz
Resolving www.python.org (www.python.org)... 151.101.40.223, 2a04:4e42:7000::223, 2a04:4e42:3000::223, ...
Connecting to www.python.org (www.python.org)|151.101.40.223|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 27159482 (26M) [application/octet-stream]
Saving to: โPython-3.12.3.tgzโ
Python-3.12.3.tgz 100%[=====================================>] 25.90M 6.07MB/s in 4.3s
2024-05-21 17:24:02 (6.02 MB/s) - โPython-3.12.3.tgzโ saved [27159482/27159482]
Install the build tools
Run these to install the build tools.
sudo apt update sudo apt install build-essential zlib1g-dev libncurses5-dev libgdbm-dev libnss3-dev libssl-dev libreadline-dev libffi-dev libsqlite3-dev
If you want, you can install git as well.
sudo apt install git
Configure, make and make install
Untar the .tgz file and change to the Python source directory.
tar -xzvf Python-3.12.3.tgz
cd Python-3.12.3/
Next, we run configure and make. The configure command may take several minutes depending on your Raspberry Pi. On my Raspberry Pi v3, this took about 45 minutes.
./configure --enable-optimizations
sudo make altinstall
If you want it to build faster, you can replace ./configure --enable-optimizations
with ./configure
. In that case, you will be doing this.
./configure
sudo make altinstall
This will install Python at /usr/local/bin/python3.12
. To test the version, run this:
/usr/local/bin/python3.12 -V
You will get this output:
Python 3.12.2
Make Python 3.12 the default version
When you run python
, you will still get the default 3.11.2.
/usr/bin/python3 -V Python 3.11.2
To make the default version of Python 3.12.3, create a softlink. Run this:
sudo rm /usr/bin/python
sudo ln -s /usr/local/bin/python3.12 /usr/bin/python
If that does not work, repeat the process for python3
instead of python
.
sudo rm /usr/bin/python3
sudo ln -s /usr/local/bin/python3.12 /usr/bin/python3
You can also create a softlink /usr/local/bin/python
.
sudo rm /usr/local/bin/python
sudo ln -s /usr/local/bin/python3.12 /usr/local/bin/python
Test Python version
Test whether Python 3.12.3 is the default version:
python -VV
Python 3.12.3
If you want to know which version of Python has precedence, you can use the which
command.
This is the command on my Raspberry Pi 3.
$ which python
/usr/local/bin/python
$ which python3
/usr/local/bin/python3
You can also check your Python versions using the whereis
command.
$ whereis python
python: /usr/bin/python /usr/share/man/man1/python.1.gz
$ whereis python3
python3: /usr/bin/python3 /usr/lib/python3 /etc/python3 /usr/local/bin/python3 /usr/share/python3 /usr/share/man/man1/python3.1.gz
Delete the installation directory
At this point, you can safely delete the installation directory.
cd /tmp/
rm -rf rm -rf Python-3.12.3.tgz Python-3.12.3/
How to upgrade from Python 3.12.x to 3.12.3
The process is exactly the same, except that Python 3.12.3 will automatically become the default after you complete installation. You may not have to change the softlinks.
Conclusion
Let me know whether you have any problems installing Python 3.12.3 in your Raspberry Pi.
This blog post will be regularly updated, usually once in two months.
Related Posts
If you have any questions, please contact me at arulbOsutkNiqlzziyties@gNqmaizl.bkcom. You can also post questions in our Facebook group. Thank you.