This tutorial will demonstrate the procedure of changing the default to an alternative Python version on Debian Linux.
How to Check the Current Version of the Default Python on Debian Linux
Before moving toward changing the default Python version to an alternative one, it’s important for you to find out the versions of Python installed on your system. The default directory for Python in Debian or another Linux-based system is “/usr/bin/python”. Using the list command “ls”, you can find the installed Python versions on your system.
Note: The output may be different in your case.
To check the current version of the default Python on the Debian, execute the following command:
In our case, the current version of the default Python is 2.7.18. It could be different in your case depending on the system you are using.
How to Change from Default to Alternative Python Version on Debian Linux
There are two ways to change from the default to alternative Python version on Debian Linux:
Method 1: Change From Default to Alternative Python Version Through update-alternatives Command
First, the user must create a symlink between the different python version directories separately so that they all can be merged in a group named “python”. Since Python version 3.9 is the alternative version, we are going to use, therefore, we must create a symlink of Python version 3.9 via the below-given command:
Next, create a symlink of Python version 2.7 18 by running the below command:
Once the symlinks are created, you can list the installed Python versions to switch them and for that, run the following command:
There you will see the Python version and by default 2.7 is selected. You can enter any number of your choice, to set it as the default version of your system. For example, I am entering the 2 to choose python3.9:
Once you switch to the Python version, run the Python version command to verify it:
Method 2: Change from Default Python to Alternative Python Through pyenv Tool
You can also change the default Python to alternative using pyenv using the following steps:
Step 1: First update the system and run the following command to ensure there are no unmet system dependencies:
Step 2: Once done, run the pyenv installation script on Debian via the below-given command:
Step 3: Next, open the source file of an environmental variable through the below-mentioned command:
Add the following script at the bottom of the source file and save the file by pressing ctrl + X and then Y:
command -v pyenv >/dev/null || export PATH="$PYENV_ROOT/bin:$PATH"
eval "$(pyenv init -)"
Step 4: Reload the changes to an environment variable through the following command:
Step 5: Verify the installation of pyenv through the version command:
Step 6: Run the list command to check the available Python versions:
Step 7: You can pick any version you want to install on Debian, I am choosing the 3.10.9
Step 8: After installing run the following command to make the installed version global for all users:
Run the following command to confirm the changes:
You can install multiple Python versions on Debian and easily switch between them. There is no need to remove one Python version to install another because you can use them concurrently. You must create a symlink for the Python version in a group and after that, you can switch to any Python version you want using the “–update alternative” command.