Install Python 3.8 on a Raspberry Pi

1
Oct 2020
By: Theo van der Sluijs

reading time: 2 min.

Category: Coding
Install python 3.8 on a Raspberry Pi is very easy. You need just a few lines of code!

[Update: 28 December 2020]: Looking for Python 3.9 install?
[Update: 07 June 2021]: Looking for Python 3.9.5 install?

So this will be a very short post on how to install Python 3.8 on a Raspberry Pi. You can actually build great things with python on a Raspberry, like my Smart Meter reader with P1 cable and Python.

The Raspberry Pi OS is the Foundation’s official supported operating system and comes pre-installed with 2 versions of Python. Last time I checked 2.7.x and 3.5.x. If you want to develop in Python 3 (and YES you want to develop in version 3), you need to specify the version of Python you are using each time and that is annoying! So, lets install the 3.8 version and make it your default!

Python 3.8

Python is a very powerful but easy to learn programming language. Its language constructs and object-oriented approach aim to help programmers write clear, logical code for small and large-scale projects.

Python has very efficient high-level data structures and a simple but yet effective approach to (OOP) object-oriented programming.

Python’s design philosophy is based on code readability with its notable use of significant indentation, trust me… that works great!. 

Python’s elegant syntax and dynamic typing, together with its interpreted nature, make it an ideal language for scripting and rapid application development in many areas on most platforms like windows, macOS, linux.

Install Python 3.8 on Raspberry PI

First make sure you can ssh to your Raspberry Pi. The default SSH user and password on Raspberry Pi OS are:
– login: pi
– password: raspberry

You cannot login to your Raspberry Pi with SSH? Read this!

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 (yes… it takes a while, grab a coffee and get me one to!!)

wget https://www.python.org/ftp/python/3.8.0/Python-3.8.0.tar.xz
tar xf Python-3.8.0.tar.xz
cd Python-3.8.0
./configure --prefix=/usr/local/opt/python-3.8.0
make -j 4

Let’s install what was compiled!

sudo make altinstall

And remove the files you don’t need anymore

cd ..
sudo rm -r Python-3.8.0
rm Python-3.8.0.tar.xz
. ~/.bashrc

And yes!!! Let’s make Python 3.8 the default version, make aliases

sudo update-alternatives --config python

And verify:

python -V

Your are ready to go!

Happy programming!!

20 Comments

  1. Gerry Roston

    Thank you for posting this! I did everything as specified, but when I got to the last two commands, things went awry.

    When I tried ‘sudo update-alternatives –config python’, I was told “update-alternatives: error: no alternatives for python”.

    When i ran ‘python -V’, it responded with “Python 2.7.16”

    Any ideas why?

    Reply
    • Theo van der Sluijs

      well some linux systems seem to place it just a bit different. I upgraded from 3.8 to 3.9 so I guess that is where is differs.

      if you do:

      PATH=$PATH:/usr/local/opt/python-3.9.0/bin

      in your terminal, you should be okay!

      Reply
      • Marko

        Hi, I have the same issue, tried a few of these solutions, but it does not work. Btw. hope you are enjoying the coffee!

        Reply
        • Theo van der Sluijs

          Marko, Thank you for your coffee. How can I assist you on this matter? Do you get an error message? Or….

          Reply
      • Cyrus G.

        Hello Theo.
        This didn’t work
        sudo update-alternatives –config python brought me “update-alternatives: error: no alternatives for python” and python -V – “Python 2.7.16” a much further version than what I had (3.7), why is this happening and how can I fix it.

        Reply
        • Theo van der Sluijs

          Cyrus you can try

          Sorry you are right. Can you try

          PATH=$PATH:/usr/local/opt/python-3.7.0/bin

          But I’m not sure if that will work for your. I did not do a 3.7 update yet only higher I guess.

          Reply
  2. Joost

    same errors here.
    shouldn’t the update for the PATH variable not point to 3.8.0 iso 3.9.0 ?

    Even with the path update the response for the python -V still is 2.7.16

    Reply
    • Theo van der Sluijs

      Sorry you are right. Can you try

      PATH=$PATH:/usr/local/opt/python-3.8.0/bin

      Reply
      • Jimi

        -j 4 ! Hahaha. I’m on a Zero, so no extra j for me!
        I hope this works…

        Reply
  3. Nayana

    Actually after the . ~/.bashrc command
    I got error saying
    Line116: unexpected EOF while looking for matching
    Line:118 syntax error: unexpected end of file
    What to do? Please help

    Reply
    • Theo van der Sluijs

      Nayana, it could be you have a .bash_profile instead of an .bashrc file. Can you check?

      Reply
      • Nayana

        Can you tell me how to check that? Please

        Reply
          • Nayana

            I got an empty terminal
            with many options in the bottom

          • Nayana

            I actually cleared that error, but when I check the python version it shows 3.5 not 3.8 can you help me with that?

  4. Gianluca

    Hello from 2 house I can try to install but when I put the command:

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

    the terminal write this:

    –2021-04-30 19:17:12 – https://www.python.org/ftp/python/3.8.0/Python-3.8.0.tar.xz
    Resolution of http://www.python.org (www.python.org) … 151.101.112.223, 2a04: 4e42: 1b :: 223
    Connect to http://www.python.org (www.python.org) | 151.101.112.223 |: 443 … connected.
    HTTP request sent, awaiting response … 200 OK
    Length: 17829824 (17M) [application / octet-stream]
    Python-3.8.0.tar.xz: Permission denied

    Could not write in “Python-3.8.0.tar.xz” (Permission denied).

    Why? Thanks a lot

    Reply
    • Theo van der Sluijs

      So, the 200 is correct, it can get the file but it seems it cannot place it currently in the folder you are.

      Can you do a :

      cd ~/

      and then try the wget again?

      Reply
  5. Jan Leliwa

    amazing tutorial, had no issues with it and it worked flawlessly, on my way to get you a coffee

    Reply
  6. Hans

    The last 2 commands doesn’t work for me. I read the comments and did all as recommended.

    pi@RPiTWO:~ $ sudo update-alternatives –config python
    update-alternatives: Fehler: keine Alternativen für python
    pi@RPiTWO:~ $ PATH=$PATH:/usr/local/opt/python-3.8.0/bin
    pi@RPiTWO:~ $ sudo update-alternatives –config python
    update-alternatives: Fehler: keine Alternativen für python
    pi@RPiTWO:~ $ python -V
    Python 2.7.16

    Reply

Leave a Reply to Theo van der Sluijs Cancel reply

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