Skip to content

Instantly share code, notes, and snippets.

@gwpl
Last active February 22, 2025 16:58
Show Gist options
  • Save gwpl/0977618927d93bfe7b8fecc7cd2d28e5 to your computer and use it in GitHub Desktop.
Save gwpl/0977618927d93bfe7b8fecc7cd2d28e5 to your computer and use it in GitHub Desktop.
Ubuntu/Debian and python3.8 vs python3.9 versioning hell? Use update-alternatives to switch seamlessly and resolve problems/conflicts.

Debian_Ubuntu_Alternative_Versions_System_101.md

Based on https://chatgpt.com/share/67b9ff44-bd6c-8007-84f4-a51d8dddfd95

You can switch between the registered Python versions using the update-alternatives interactive selection tool. Here’s how to do it:

  1. Run the Configuration Command:

    sudo update-alternatives --config python3
  2. Select the Desired Version:

    After running the command, you’ll see a list similar to this:

    There are 2 choices for the alternative python3 (providing /usr/bin/python3).
    
      Selection    Path                Priority   Status
    ------------------------------------------------------------
      0            /usr/bin/python3.9   2         auto mode
      1            /usr/bin/python3.8   1         manual mode
      2            /usr/bin/python3.9   2         manual mode
    
    Press <enter> to keep the current choice[*], or type selection number: 
    
  3. Enter the Number:

    Type the number corresponding to Python 3.8 (in this example, it might be 1) and press Enter. This action will change the default python3 to Python 3.8.

  4. Verify the Change:

    Check the currently active Python version with:

    python3 --version

This process tells the alternatives system to update the symbolic link for /usr/bin/python3 to point to your chosen version.

E.g. How to Setup python3.8 and python3.9 and switch betwen them (Debian, Ubuntu)

Steup python3.8 and python3.9 as alternatives

# sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.8 1
# sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.9 2

(last digits are priorities - to be adjusted based on your needs, higher value = higher priority , so in above case "pythion 3.9" has priority 2 and has higher prioryt then python 3.8)

Examplle of sudo add-apt-repository universe not working with pyehon3.9

By default it's python 3.9 in this setup but let's switch to showcase how to do switchgin:

switch to "(auto)" pointing to python3.9

# sudo update-alternatives --config python3
There are 2 choices for the alternative python3 (providing /usr/bin/python3).

  Selection    Path                Priority   Status
------------------------------------------------------------
  0            /usr/bin/python3.9   2         auto mode
* 1            /usr/bin/python3.8   1         manual mode
  2            /usr/bin/python3.9   2         manual mode

Press <enter> to keep the current choice[*], or type selection number: 0
update-alternatives: using /usr/bin/python3.9 to provide /usr/bin/python3 (python3) in auto mode

Example command not workign with python3.9 (at least as of 2025-02-22):

# sudo add-apt-repository universe
Traceback (most recent call last):
  File "/usr/bin/add-apt-repository", line 12, in <module>
    from softwareproperties.SoftwareProperties import SoftwareProperties, shortcut_handler
  File "/usr/lib/python3/dist-packages/softwareproperties/SoftwareProperties.py", line 68, in <module>
    from gi.repository import Gio
  File "/usr/lib/python3/dist-packages/gi/__init__.py", line 42, in <module>
    from . import _gi
ImportError: cannot import name '_gi' from partially initialized module 'gi' (most likely due to a circular import) (/usr/lib/python3/dist-packages/gi/__init__.py)

Swtiching to python3.8 and working command

Now let's switch to python3.8 :

# sudo update-alternatives --config python3
There are 2 choices for the alternative python3 (providing /usr/bin/python3).

  Selection    Path                Priority   Status
------------------------------------------------------------
* 0            /usr/bin/python3.9   2         auto mode
  1            /usr/bin/python3.8   1         manual mode
  2            /usr/bin/python3.9   2         manual mode

Press <enter> to keep the current choice[*], or type selection number: 1
update-alternatives: using /usr/bin/python3.8 to provide /usr/bin/python3 (python3) in manual mode

and command works:

# sudo add-apt-repository universe
'universe' distribution component is already enabled for all sources.

Cleanup: Now let's switch back to "(auto)" mode;

# sudo update-alternatives --config python3
There are 2 choices for the alternative python3 (providing /usr/bin/python3).

  Selection    Path                Priority   Status
------------------------------------------------------------
  0            /usr/bin/python3.9   2         auto mode
* 1            /usr/bin/python3.8   1         manual mode
  2            /usr/bin/python3.9   2         manual mode

Press <enter> to keep the current choice[*], or type selection number: 0
update-alternatives: using /usr/bin/python3.9 to provide /usr/bin/python3 (python3) in auto mode
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment