Sam Westby

Install Multiple Versions of Python on your Raspberry Pi

Managing multiple Python versions is a pain. This tutorial shows you how to install and use the easiest Python version manager out there: pyenv

Samuel Westby

June 6th, 2022

raspberry pi

python

Supplies

Step 1 - Set up your Pi

If you haven't set up your Raspberry Pi yet, you can follow my tutorial for that here.

Step 2 - Install pyenv

Open a terminal to your Pi. You can do this with SSH or directly on your Pi as a desktop. In the terminal, run the command:

curl https://pyenv.run | bash

Step 3 - Add pyenv to .bashrc

Next you'll add pyenv to .bashrc. This lets your terminal know where to look for the pyenv versions of Python. Run the following to open the Nano editor:

sudo nano ~/.bashrc

You are now in the nano editor. Go to the end of the file and add the following three lines:
export PATH="$HOME/.pyenv/bin:$PATH"
eval "$(pyenv init --path)"
eval "$(pyenv virtualenv-init -)"

Finally, save and exit
CTRL + S
CTRL + X

You should now be back to the terminal. Restart the terminal by closing it and reopening it. Alternatively you can run:
exec $SHELL

Step 4 - Install the system packages

There's a big list of packages to install. I suggest just copying and pasting this command:

sudo apt-get install --yes libssl-dev zlib1g-dev libbz2-dev libreadline-dev libsqlite3-dev llvm libncurses5-dev libncursesw5-dev xz-utils tk-dev libgdbm-dev lzma lzma-dev tcl-dev libxml2-dev libxmlsec1-dev libffi-dev liblzma-dev wget curl make build-essential openssl

Step 5: Setup pyenv

First, update pyenv with the command

pyenv update

Second, install the versions of Python that you want. You can see all your options with
pyenv install --list

In the video above, I install version 3.7.12 with the command
pyenv install 3.7.12

Step 6: Set your Python version

You have three options how to set your Python version:

For example, the following command will set my Python version to 3.7.12 for the terminal where I ran the command.
pyenv shell 3.7.12

NOTE: you can unset any Python version using the --unset flag. This code will reset the Python version for my current terminal directory.
pyenv local --unset

NOTE: you can uninstall pyenv with the following command
rm -fr ~/.pyenv
You also have to remove the lines we added to the .bashrc file in Step 3.

Lorem ipsum, dolor sit amet consectetur adipisicing elit. Libero tenetur non laborum dolorem laboriosam quo quibusdam assumenda dolores eveniet. Ipsum?