24

I need python newest version in raspberry pi.
I tried apt install python3 3.8
apt install python3 but this didnot work.
And I also needed to update my raspberry pi python IDLE

2
  • ramoonus.nl/2020/10/06/… Commented Nov 6, 2020 at 16:28
  • 2
    I would recommend using a python version manager such as pyenv. Don't try to change default python's as that can break the OS. Commented Nov 6, 2020 at 16:29

5 Answers 5

68

First update the Raspbian.

sudo apt-get update    

Then install the prerequisites that will make any further installation of Python and/or packages much smoother.

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

And then install Python, maybe by downloading a compressed file?

example 1 :

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

Extract the folder :

sudo tar zxf Python-3.8.0.tgz

Move into the folder :

cd Python-3.8.0

Initial configuration :

sudo ./configure --enable-optimizations

Run the makefile inside the folder with the mentioned parameters :

sudo make -j 4

Run again the makefile this time installing directly the package :

sudo make altinstall

Maybe You already did it but You don't know how to setup the new version as a default version of the system?

Check first that it has been installed :

python3.8 -V

Send a strong command to .bashrc telling him who (which version) is in charge of Python

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

Again! Tell him because .bashrc has to understand! I am joking - You have to source the file so the changes can be applied immediately :

source ~/.bashrc

And then check that Your system changed the default version of Python to Python 3.8

python -V

The failure depends on many factors : what dependencies are installed, what are the packages added to the source_list.d, some inconvenient coming up during the installation. All may give you more information than you think, just read carefully. Hope it helped.

Sign up to request clarification or add additional context in comments.

6 Comments

I guess this is old. But isn't there a more... normal... way to update a required program? I'm new to Pi and by extension linux but this is more like a final year project. Is there really no decent/normal way for linux to update a python version? It feels like it's overcomplicated just for fun. I guess I'm more used to there being 1-3 commands to update a library not a whole essay that I need to rewrite from memory on a different machine (pi doesn't do well with browsers)(ram) I don't know what the constraints are but this totally feel waaaaaay out of necessary bounds. But hey, this is linux.
Thanks Xerozz, amazing answer with detailed / kind explanation, which are very valuable for newbies like myself. Cheers.
Linux is painful.
Ah, of course. The everyone's favorite "easy, beginner friendly" programming language. Just compile it from source to update, no big deal... Is this truly the simplest solution?
This is because the latest version of python isn't on the raspberrypi repo's (I just checked and it's still on 3.7?!?!?). I would poke them about this. Python is a very common tool on rasppi's so the fact they do not keep the repos up to date is mind blowing.
|
5

To all of you who got a problem with your RPi 3 freezing during this step:

sudo make -j 4

just change it to:

sudo make -j 2

or simply:

sudo make 

Best regards

2 Comments

If you want to suggest an answer like this, please also give your reasoning. Is this a workaround to a temporary bug? Or is it always necessary to do this? What does the -j flag do?
@AlexSpurling It specifies the number of CPU threads to use. This thread will explain: stackoverflow.com/questions/15289250/using-make-with-j4-or-j8/…
3

To anyone looking at this answer in 2024, you can upgrade to bullseye and install Python 3.9 directly from apt

  1. Backup Your Sources List:
$ sudo cp /etc/apt/sources.list /etc/apt/sources.list.bak
$ sudo cp /etc/apt/sources.list.d/raspi.list /etc/apt/sources.list.d/raspi.list.bak
  1. Edit the Sources List: Open the sources list file in a text editor:
$ sudo nano /etc/apt/sources.list

Replace all instances of stretch with bullseye. Your file might look something like this after the change:

deb http://raspbian.raspberrypi.org/raspbian/ bullseye main contrib non-free rpi
  1. Update, and install Python 3.9
$ sudo apt update
$ sudo apt install -y python3.9

Comments

2

Follow below commands to install the version which you want:

tar xf Python-3.x.x.tar.xz
cd Python-3.x.x
./configure --enable-optimizations
make
sudo make install

once completed run python -V

Comments

2

You can painlessly create virtual environments of any recent Python version (3.9 up to 3.13 worked fine for me) using the uv tool.

First make sure to install uv, e.g. via

curl -LsSf https://astral.sh/uv/install.sh | sh
# avoid need to exit/restart shell
source $HOME/.local/bin/env   

Then create the virtual environment

uv venv mycustomvenv --python 3.13

Finally, activate via e.g. (uv also supports other ways)

. mycustomvenv/bin/activate
python -V                      # gives Python 3.13.X

1 Comment

Great answer, worked for me on rpi 3b+. In case you also need newer pip with the python version add --seed flag to the command, e.g: uv venv --seed --python 3.10 mycustomvenv

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.