How to Upgrade to a Newer Version of Python#

Most Linux installations include a version of both Python 2 and 3.

You can view your current default version of Python (version 2) using the command:

  % python -V
This will respond with something akin to:
Python 2.7.16

You can also check your version of Python 3:

  % python3 -V
with the response being akin to:
Python 3.7.3

To prepare to upgrade to a newer version of Python, first make sure your Pi is up to date, as well as install the required development packages:

sudo apt update
sudo apt upgrade
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

Then, to install a specific version of Python, e.g., 3.8.5, download, configure, make and install the package:

wget https://www.python.org/ftp/python/3.8.5/Python-3.8.5.tgz

tar zxf Python-3.8.5.tgz
cd Python-3.8.5
./configure --enable-optimizations
make -j4
sudo make install
or alternatively:
sudo make altinstall
if you don't want the newly-installed version to be the default version on your Pi. The latter is an absolute requirement on Ubuntu (for example), as if you replace the default version of Python you will disable/break your OS (e.g., see How to Install Python 3.8 on Ubuntu). On a Raspberry Pi this isn't so far as I have seen a problem.

In the above directions, replace "3.8.5" above with whatever version you want to install. You can browse the directory of available downloads at: https://www.python.org/ftp/python

You must be patient. On a Pi 4, this can take a long time; on a 3 B+ a very long time. On a Pi Zero basically overnight.

If you're logging into your Pi remotely to do the upgrade, it's interesting to run a second ssh session with top or htop running, to watch your Pi's processors working very hard.


Tags:  none