Install python 3.9 on Raspberry PI

28
Dec 2020
By: Theo van der Sluijs

reading time: 2 min.

Category: Coding
Installing python 3.9 on a Raspberry Pi in a few easy to follow steps in a few lines of code.

[Update: 07 June 2021]: Looking for Python 3.9.5 install?

On the first of October 2020 I have written the blog post “Install Python 3.8 on a Raspberry Pi“. That article about how to install Python 3.8 on a Raspberry Pi got a lot of attention. A lot of people read my article an started using Python 3.8. But now there is Python 3.9 so I thought why not write an article on how to install Python 3.9 on a Rasperry Pi!

Raspberry PI What?

The Raspberry Pi is a low cost, credit-card sized computer that you can plug into a computer monitor, mouse and keyboard. It is a capable little device that enables people of all ages to explore computing, and to learn how to program in languages like Scratch and Python. The Raspberry runs on linus and has it’s own distro called Raspberry PI OS.

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. And as you want to develop in Python 3, you need to specify the version of Python you are using each time and that is annoying! So, lets install the 3.9 version and make it your default!

Install Python 3.9 on a 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

Updating…. this takes a while, grab a coffee and get me one to!!

Next download the latest python version, untar it and compile it.

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

Building and compiling python can take a while (depends on the Raspberry Pi you have and the amount of memory). And when done, lets install what was build!

sudo make altinstall

Lets do a cleanup and remove the files we don’t need anymore

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

Make Python 3.9 default on Raspberry Pi

Finally! Let’s make Python 3.9 the default version, make aliases by:

sudo update-alternatives --config python

And verify:

python -V

If you still have a 2.x or 3.x version and not the 3.9 version, or when you get an error message do as following.

nano ~/.bashrc

Go to the last line (arrow down) and find something like:

alias python=/usr/bin/python-xxxxxx

And change this to

alias python=/usr/local/opt/python-3.9.0/bin/python3.9

If you cannot find any python related alias in your bashrc file just add the above line to your file.

Do a CTRL+X and CTRL+Y

Then in the terminal do:

. ~/.bashrc

This refreshes the bashrc in your terminal

Verify the Python version by:

python -V

and hopefully you see:

Python 3.9.0

Your are ready to go!

Happy programming!! Questions? Let me know in the comments below!

28 Comments

  1. rick

    >”You cannot login to your Raspberry Pi with SSH? Read this!” It’s not clear what “this” is that i should read.

    I’m reading this page with Chrome on Win10. The commands to run to install libs and to download, untar, and build are run together instead on showing on separate lines.
    For example: “sudo apt-get updatesudo apt-get install -y build-essential…”
    results in ” Invalid operation updatesudo”

    Reply
  2. Michael Graf

    Thanks for this detailed guide! I’ve used it to install Python 3.9.2 on my Raspberry Pi (running Raspbian Buster), and it mostly worked. The only things that didn’t were:

    – /usr/local/opt/python-3.9.2/bin was not added to the path. Fixed this by adding it manually to /etc/profile
    – update-alternatives didn’t know about Python, so I manually changed the symlinks for python and python3 in /usr/bin

    Hope that helps anyone who runs into this as well!

    Reply
    • emjei

      Hi Michael, I did the steps above but unfortunately still showing 3.7 version. When I performed this sudo update-alternatives –config python, I can see 2.7 and 3.9.

      I’ve also got this error after doing the steps above.

      WARNING: The script easy_install-3.9 is installed in ‘/usr/local/opt/python-3.9.0/bin’ which is not on PATH.
      Consider adding this directory to PATH or, if you prefer to suppress this warning, use –no-warn-script-location.
      WARNING: The script pip3.9 is installed in ‘/usr/local/opt/python-3.9.0/bin’ which is not on PATH.
      Consider adding this directory to PATH or, if you prefer to suppress this warning, use –no-warn-script-location.

      Reply
  3. Randy

    1
    nano ~/.bashrc
    Go to the last line (arrow down) and find something like:

    1
    alias python=/usr/bin/python-3.8

    I see nothing like the line above?

    Reply
    • geek emeritus

      I just did that. pasted the line at the bottom despite the fact there was nothing to replace. did the . ~/.bashrc and checked the version and it’s 3.09

      Reply
  4. Malcolm

    HI Theo,
    Tried this on an old Pi3b+ and, after taking it’s time (50 minutes or so), it works really well. Excellent instructions and easy to follow.
    Thank you,
    Malcolm

    Reply
  5. Harry C Ridpath

    Where is the executable? The I cannot find any python-3.9.0 folder. I found the 3.8 folder and added that to the .bashrc. There was no aliases for python at all.
    This gave me python 3.7.3 version.

    I would like to have the 3.9. I did everything that this workflow outlined and was able to successfully create a make and install. The only issue was that the “sudo update-alternatives –config python returned no alternative for python.

    any enlightenment is greatly appreciated.

    Reply
    • Theo van der Sluijs

      Python 3.7.3? Okay.. that is strange. Maybe you are still looking at the already installed version?

      Reply
  6. Elout

    Hi,

    Thanks for the detailed explanation. It worked nicely. Two points.
    1) although I have python2 and 3 (3.7) on my pi I couldn’t find the alias lines in bashrc. But when I added the line, it worked. This isn’t an issue, correct?

    2) I tried to run a script and it couldn’t find a library (sqlite).
    pip3 install sqlite gave an error message that environmental variable couldn’t be found.
    Do I need to do additional steps to upgrade pip as well?

    Reply
    • Theo van der Sluijs

      Elout, you are correct, it’s not an issue (1.) Yes you can update pip by running

      python -m pip install –upgrade pip

      Reply
  7. Kurtwerbrouck

    How to add requests lib for python3.9. When running a python program it always failes with the error that requests is missing. When using pip3 install requests is tells me that it is already installed. Any suggestions

    Reply
    • Theo van der Sluijs

      Are you using a virtual environment? Can you do a pip install (so without the 3)

      Reply
  8. SANDEEP PAREEK

    when i tried to upgrade the pip version using command
    python -m pip install –upgrade pip
    i get this error:
    /usr/local/bin/python3.9: No module named pip
    will you please help me out.

    Reply
  9. mark

    it is a great start but does not exactly work. I am using Raspberry pi OS created by the imager on May 31, 2021.

    it seems drastic to me to replace python, which should be python2. I can put the new location on path with .bashrc but the alias lines don’t exist in there so I cannot change them. running the command “sudo python3” will still bring the old python3.7 interpreter. It is complicated to have a mixture of the two.

    would it be better to either 1) use a virtual environment or 2) install as a user and not attempt to replace the system Python or finally 3)
    change the links in /usr/bin to all point to the new python3.9 installation?

    Reply
    • Theo van der Sluijs

      Hi Mark,

      Yes you are quite right. You can create a .venv (virtual environment) to use Python 3.x and not replace python 2.x at all.
      However, python 2,x is not removed you can still use it you just have to pinpoint your .venv to it.

      Myself rather always use the latest stable version, but I do however always use a .venv for all my projects.

      Thank you for your comment, it’s greatly appreciated!

      Reply
  10. Ron Seligmann

    Dear Theo, did all as above and encountered few issues.

    There was no alias line for python so I added the suggested.
    Did a config refresh: . ~/.bashrc
    sudo update-alternatives –config python
    update-alternatives: error: no alternatives for python

    Checked what version I have
    Type: python -V
    Get: Python 2.7.16
    Type: python3
    Get: Python 3.7.3 (default, Jan 22 2021, 20:04:44)

    Excluded the alias line and updated bash; did a version check
    -bash: /usr/local/opt/python-3.9.0/bin/python3.9: No such file or directory

    Came back fine after reboot with still the same answers as above
    Would appreciate your support on this.

    Reply
    • Theo van der Sluijs

      can you do a

      which python

      It might be possible that it can be found /usr/bin/python

      Reply
      • Dale

        Hi Theo, had a similar issue to Ron, got everything installed okay but update-alternatives gives an error:

        “update-alternatives: error: no alternatives for python”

        I figured this was the same issue as Michael Graf ran into, to fix he says he:

        “…manually changed the symlinks for python and python3 in /usr/bin”

        I know what symlinks are (I think), but I don’t understand what I need to do here, can you help?

        Reply
        • Theo van der Sluijs

          You can create symlinks but I would not know why as you can better change the .bashrc file have you tried that?

          So do a
          nano ~/.bashrc
          and find any alias related lines for python and comment it with a # infront of the line, or remove it.

          and then add

          alias python=/usr/local/opt/python-3.9.0/bin/python3.9

          It is possible that there is no alias in your file, then just add the above code and don’t remove a thing.

          If you are unsure that python is in /usr/local/opt/, do

          cd /
          find . -name python-3.9.0

          Mind that this is only searching for the 3.9.0 version. If you have any other version give it that search option.

          Reply
  11. BenoitG

    Great guide!!!! Update done !!!

    Reply

Trackbacks/Pingbacks

  1. Python 3.9.0 set as default on rasberry pi 4 but my IDE cant see it – Ask python questions - […] I am using cannot find the python 3.9.0 but can see 3.7.3 which came with the pi. I followed…

Leave a Reply to Theo van der Sluijs Cancel reply

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