Install Python 3.9.5 on Raspberry Pi step by step

7
Jun 2021
By: Theo van der Sluijs

reading time: 3 min.

Category: Coding,Tech
Tags:
Python 3.9.5 on the Raspberry Pi installation. With a few steps you can install Python 3.9.5 on your own Raspberry Pi!

So I’ve bought a new Raspberry Pi, the 4+ to be exact. I’ve put 32GB micro SDcard in it, installed Raspberry Pi OS on it, did my ssh stuff and Yes! I’ve upgraded to Python 3.9.5!

I have written about installing Python 3.8 and Python 3.9 on a Raspberry, but you are looking at the latest version right now.

So, little recap! Raspberry Pi?

The Raspberry Pi is a little computer the size of a credit card. A Rasp is very cheap, for about $45 it’s yours. You can connect a monitor to it, with the 4+ even 2! And of course a keyboard and mouse. It has a network connection and 4 USB ports.

The small device is very powerful (for its size) and actually anyone can work with it. You can use it as a PC, server, firewall etc. With languages like Scratch and Python you can write very powerful programs.

Besides that it can do everything you expect from a PC, you surf around on the internet, you watch Netflix, YouTube or another HD video, you read your mail, make spreadsheets and work on your documents. And yes, you can play games on it too!

Raspberry Pi OS?

Raspberry Pi OS is a Linux distribution developed for educational purposes and optimized for the various models of the Raspberry Pi. Raspbian is a fork of Debian that can be installed as a live system on an SD card or a microSD card.

How ever!!! A Raspberry Pi and the Raspberry Pi OS are very powerfull! Yes it is great for educational purposes but it can also be used for various great business models. Like taking care of your Ubiquiti network, handling your email, be a great firewall, act like a NAS, get data from your smart energy meters, process data from your solar panel inverters, gard your house with AI camera footage… the possibilities are endless

Next to monitoring my network I also use it for a lot of my Python program’s.

Python 3.9.5

Python is a programming language designed and developed in the early 1990s by Guido van Rossum, at the time affiliated with the Center for Mathematics and Computer Science in Amsterdam. Python is a ‘general-purpose’ programming language, which means that it can be used for almost anything.

Python is a programming language that is generally easy to read. Python is also an HLL or High Level Programming language. This is why Python is so widely used. You can read it and others can also read it easily. This makes it easier to maintain and you think better / more logically while programming. Python is widely used for scripting but can also be compiled into executable programs.

Install Python 3.9.5 on a Raspberry Pi Using Source Code

First of all, update your Raspberry Pi. It’s always a good idea to keep it up to date. Due to fixed vulnerabilities, feature updates and general bug fixes

Open up your terminal on your raspberry pi and do a:

sudo apt update

Then install essential packages for compiling source code.

sudo apt install wget build-essential checkinstall

and

sudo apt install libreadline-gplv2-dev libncursesw5-dev libssl-dev libsqlite3-dev tk-dev libgdbm-dev libc6-dev libbz2-dev libffi-dev zlib1g-dev

Download the Python 3.9.5 source code from the official download site with this command!

wget https://www.python.org/ftp/python/3.9.5/Python-3.9.5.tar.xz

When fineshed downloading unpack the file with

tar -Jxf Python-3.9.5.tar.xz

Finished unpacking? Great, go to the folder the tar created by doing a

cd Python-3.9.5

Then prepare the source for the installation with

./configure --enable-optimizations

Now do a

make

to compile the source, it’s a good time to get a coffee and a good book. This will take a while!

Ready? Do a

sudo make install

this replaces your previous Python installation!!

It’s always good to cleanup after yourself by doing

cd ..
sudo rm -r Python-3.9.5
rm -rf Python-3.9.5.tar.xz

Now… The moment of truth!

Do a

Python --version

ahhhh bummerrrrrrr, you see this?

Python 2.7.16

Don’t worry, there are two options!

  1. you leave it like it is and in your Virtual environment (Venv) you point to Python 3.9.5
  2. we do some extra magic and make 3.9.5 the default!

Let’s do 2!

Finishing up Python 3.9.5 on a Raspberry Pi

Do this in your terminal

echo "alias python='/usr/local/bin/python3.9'" >> ~/.bashrc

Then do a

. ~/.bashrc

Or close your terminal and reopen the connection to your rasp in a new terminal

python -V

Python 3.9.5

All done!

11 Comments

  1. Reuben

    Worked perfect. Thanks for the write up!

    Reply
  2. Tai-Jie Yun

    Thank you.

    Nice and clear instructions.

    Reply
  3. Rouvim Avgoustakis

    Thank you for the explanation. This makes learning programming a lot easier. It stimulates to learn more

    Reply
  4. mudi

    installed flawlessly! much obliged

    Reply
  5. Sudhir

    This worked exactly as described in steps. I was able to install Python 3.9.5 on my Raspberry pi.
    Thanks a lot for this article.

    Reply
  6. Yang

    Thanks for the update. I got the latest python.
    When I do pip list, it does’t have the libraries associated with older python.
    Do I need to reinstall all the libraries?

    Reply
    • Theo van der Sluijs

      Yes you will need to reinstall all the libraries. Do you work with .venv (python environment)

      Reply
  7. Nikolaus Meier

    Worked perfect. Thanks for the write up!

    Reply

Submit a Comment

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