Sunday, September 7, 2025

Managing Python Installations using PyEnv - On Linux

I've been using pyenv on the Mac unwittingly since you're not really supposed to use the Python that ships with the operating system.

So you're forced to install Python from python.org, or as many Mac users who are software developers do, install Homebrew and use that package management system to manage all the software development tools.

Linux and Raspberry Pi

The process is the same.
  1. Update the system.
  2. Install required dependencies for pyenv.
  3. Install pyenv.
  4. Update some environment variables.
  5. Reset the shell
  6. Install a Python version using pyenv.
  7. Set the installed Python version as the default.
  8. Start programming!
In addition to that, I'll also show you how to create a Python virtual environment.

Update the system

sudo apt update

You may need to upgrade your system based on the results of the above command. If so, type the following

sudo apt upgrade

Install required dependencies for pyenv

sudo apt install make build-essential libssl-dev zlib1g-dev \
libbz2-dev libreadline-dev libsqlite3-dev curl git libncursesw5-dev \
xz-utils tk-dev libxml2-dev libxmlsec1-dev libffi-dev liblzma-dev

Install pyenv

curl -fsSL https://pyenv.run | bash

Update some environment variables

echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bashrc
echo '[[ -d $PYENV_ROOT/bin ]] && export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bashrc
echo 'eval "$(pyenv init - bash)"' >> ~/.bashrc

If you also have a .profile file in your home directory, update that file as well. As well as any .bash_profile or .bash_login files.

Reset the shell

exec "$SHELL"

Or you can logout, and login again.

Install a Python version using pyenv

To see all the commands available in pyenv, type:

pyenv commands

To get help on a specific command (such as install), type:

pyenv install --help

To see ALL the versions of Python that pyenv has available for installation, type:

pyenv install --list

But if you're only interested in the CPython versions starting from 3.0, type:

pyenv install --list | grep -E ' 3\.([1-9][0-9]+)'

Finally, to install a Python version (e.g., 3.13.7), type:

pyenv install 3.13.7

Set the installed Python version as the default

To see all the Python versions that pyenv can manage, type:

pyenv versions

To see the current Python version that's in use, type:

pyenv version

Finally, the easiest way to set the Python version to use (e.g., 3.13.7), type:

pyenv global 3.13.7

pyenv has a couple of other interesting commands for setting the Python version to use depending on whether you just want to use it temporarily in the shell:

pyenv shell 3.13.7

or if you want to use a specific version each time you are in a specific folder (e.g., ~/src/proj1):

cd ~/src/proj1
pyenv local 3.13.7

Now when you switch into that folder, pyenv will make sure to call the correct python version that you specified.

Now all that's left is to...

Start programming!











No comments:

Post a Comment