How to upgrade Python with Pyenv

Published : Feb 2, 2024

Updating to the latest version of Python using pyenv involves a few steps. Here's a guide to help you through the process:

1. Update pyenv itself

First, ensure that pyenv is updated to the latest version, as this will ensure it has the latest Python versions available for installation.

If you installed pyenv using Homebrew (on macOS), run:

If you installed pyenv using git, you can update it by navigating to the pyenv installation directory (usually ~/.pyenv) and pulling the latest changes:

2. Install the Latest Python Version

After updating pyenv, you can install the latest Python version. First, check the available versions:

Look for the latest stable version in the list (avoid alpha, beta, and release candidates unless you specifically need them).

To install the latest version, for example, Python 3.10.4, run:

Replace 3.10.4 with the latest version you find in the list.

3. Set the Newly Installed Version as Global (Default)

After installing the new version, you can set it as the global default:

Again, replace 3.10.4 with the version number you installed.

4. Verify the Installation

Verify that the new version is being used by default:

This command should output the Python version you just set with pyenv.

5. Update the Environment

After changing the Python version, it's a good practice to restart your terminal or run the following command to update the environment:

6. Update or Reinstall Global Python Packages

If you had any globally installed Python packages (installed with pip), you might need to reinstall them for the new Python version, as each Python version managed by pyenv maintains its own set of packages.

Troubleshooting

If you encounter any issues with pyenv not being recognized or the new Python version not being used, ensure that pyenv is correctly initialized in your shell's configuration file (like .bashrc, .bash_profile, .zshrc, etc.).

Remember that changing the global Python version can affect Python applications and scripts on your system. Ensure that your projects are compatible with the new version.