| by Arround The Web | No comments

How to Change from Default to Alternative Python Version on Debian

Python is an important programming language for all Linux-based distributions. This is the reason why you will find Python preinstalled in the system with the default version. The case may occur when you require switching from the default Python version to an alternative one if installed. The fact is some applications require different Python versions.

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.

ls /usr/bin/python*

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:

python --version

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:

  1. Through update-alternatives command
  2. Through pyenv Tool

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:

sudo update-alternatives --install /usr/bin/python python /usr/bin/python3.9 2

Next, create a symlink of Python version 2.7 18 by running the below command:

sudo update-alternatives --install /usr/bin/python python /usr/bin/python2.7 18

Once the symlinks are created, you can list the installed Python versions to switch them and for that, run the following command:

sudo update-alternatives --config python

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:

python --version

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:

sudo apt-get update; sudo apt-get install make build-essential libssl-dev zlib1g-dev libbz2-dev libreadline-dev libsqlite3-dev git wget curl llvm libncursesw5-dev xz-utils tk-dev libxml2-dev libxmlsec1-dev libffi-dev liblzma-dev

Step 2: Once done, run the pyenv installation script on Debian via the below-given command:

curl https://pyenv.run | bash

Step 3: Next, open the source file of an environmental variable through the below-mentioned command:

sudo nano ~/.profile

Add the following script at the bottom of the source file and save the file by pressing ctrl + X and then Y:

export PYENV_ROOT="$HOME/.pyenv"

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:

source ~/.profile

Step 5: Verify the installation of pyenv through the version command:

pyenv --version

Step 6: Run the list command to check the available Python versions:

pyenv install --list

Step 7: You can pick any version you want to install on Debian, I am choosing the 3.10.9

pyenv install 3.10.9

Step 8: After installing run the following command to make the installed version global for all users:

pyenv global 3.10.9

Run the following command to confirm the changes:

python --version

Bottom Line

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.

Share Button

Source: linuxhint.com

Leave a Reply