How to Change the Default Python Version on Debian
Introduction
Before we begin talking about how to change the default Python Version on Debian, let's briefly understand – What is Python?
Python is a popular and versatile programming language known for its simplicity and readability. With an extensive library ecosystem and cross-platform compatibility, Python enables developers to build a wide range of applications, from web development and data analysis to artificial intelligence and automation. Its clean syntax and beginner-friendly nature make it an excellent choice for both beginners and experienced programmers.
For all Linux-based distributions, Python is a crucial programming language. Python comes preinstalled in the system with the default version for this reason. The situation could arise if you need to move from the installed default Python version to another one. Certain applications actually call for specific Python versions.
In this tutorial, you will change the default Python Version on Debian. We will also address a few FAQs on how to change the default Python Version on Debian.
How to Determine the Current Version of the Default Python on Debian Linux
It's crucial for you to learn whatever versions of Python are currently installed on your system before making the decision to switch to a different default Python version. "/usr/bin/python" is the default location for Python on Debian or other Linux-based systems. The installed Python versions on your system can be found using the list command "ls".
ls /usr/bin/python*
Note: You may get a different output.
Run the following command to check the current Python version installed by default on Debian:
python --version
The default Python in our case is version 2.7.18. Depending on the system you use, it can differ in your instance.
How to Switch from Default to Alternative Python Version on Debian Linux
You can change from the default to an alternative Python version on Debian Linux in two different ways:
- Via update-alternatives command
- Via pyenv Tool
Method 1: Switch From Default to Alternative Python Version Through update-alternatives Command
First, the user needs to set up a symlink between the various Python version directories so that they can all be merged into a group called "python." The command listed below must be used to establish a symlink of Python version 3.9 since it is the alternative version we will use:
sudo update-alternatives --install /usr/bin/python python /usr/bin/python3.9 2
Next, run the following command to create a symlink to Python version 2.7 18:
sudo update-alternatives --install /usr/bin/python python /usr/bin/python2.7 18
Once the symlinks are generated, you can switch between the installed Python versions by running the following command:
sudo update-alternatives --config python
The Python version is displayed, and by default, 2.7 is selected. You can input whatever number you want to make the default version of your system. For instance, here we are selecting Python 3.9 by providing the number 2:
After switching to the Python version, use the Python version command to confirm it:
python --version
Method 2: Switch from Default Python to Alternative Python Through pyenv Tool
You may also use pyenv to change the default Python to an alternate by following these steps:
Step 1: First, update the system and run the following command to confirm that no system dependencies are missing:
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: After that, use the following command to run the pyenv installation script on Debian:
curl https://pyenv.run | bash
Step 3: Next, use the following command to open the source file of an environmental variable:
sudo nano ~/.profile
Insert the following script at the bottom of the source file, then save it by clicking 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: Using the following command, reload the modifications to an environment variable:
source ~/.profile
Step 5: Use the version command to confirm the installation of pyenv:
pyenv --version
Step 6: To see which Python versions are available, use the list command:
pyenv install --list
Step 7: You can install any version of Debian; we are using 3.10.9.
pyenv install 3.10.9
Step 8: Run the subsequent command after installation to make the installed version global for all users:
pyenv global 3.10.9
To confirm the modifications, run the following command:
python --version
FAQs to Change the Default Python Version on Debian
Which Python versions are available on Debian?
Debian typically includes multiple Python versions. You can check the available versions using the command ls /usr/bin/python*
.
Does switching the default Python version affect existing projects?
Yes, changing the default Python version may impact existing projects relying on specific Python features or modules. It's recommended to test projects with the new Python version before switching permanently.
How can I install an alternative Python version on Debian?
You can install alternative Python versions on Debian using the package manager, such as apt-get
, with the appropriate package name. For example, apt-get install python3.8
installs Python 3.8.
How can I change the default Python version using update-alternatives
?
Execute the command update-alternatives --config python
and select the desired Python version from the provided options.
Are there any compatibility issues when switching Python versions?
Compatibility issues may arise when switching between major Python versions since syntax and library changes can affect code behavior. Testing and updating code may be necessary.
Can I have multiple Python versions installed simultaneously?
Yes, you can have multiple Python versions installed side-by-side on Debian. Each version will be accessible through its respective command, such as python3.7, python3.8, or python3.9.
Conclusion
On Debian, you can install various Python versions and swiftly switch between them. There is no need to uninstall one Python version before installing another because they can coexist. You must first build a symlink for the Python version in a group, and then you may use the "-update alternative" command to switch to whichever Python version you wish.
If you have any queries or doubts, please leave them in the comment below. We'll be happy to address them.