- How to Switch Python Version in Ubuntu & Debian
- Switch Python Version on Ubuntu & Debian
- Conclusion
- How to Change Python Default Version in Debian/Ubuntu
- prerequisite
- How can we change the default Python version in Debian/Ubuntu?
- Step 1: Checking the available versions of Python in Debian/Ubuntu
- Step 2: Checking the default version of Python in the system
- Step 3: Changing the default version of Python based on each user
- Step 4: Switching the version of Python with its alternative versions
- Step 5: Confirming configuration and availability of Python alternatives
- Step 6: changing the Python versions
- FAQ
- What is the fastest way to manage different versions of Python?
- How to know the available version of Python in Ubuntu/Debian?
- Which version of Python is installed by default on Ubuntu?
- What is the most stable version of Python to use in Debian/Ubuntu?
- Conclusion
- How to Change the Default Python Version to Python 3.9
- Setting the Latest Python Version
- Conclusion
How to Switch Python Version in Ubuntu & Debian
Python is a high-level programming language, widely used for system programming. It is available for all popular operating systems. You can install more than one Python version on a single system. Once you have installed multiple Python versions, you can switch the default Python with the update-alternatives tool.
All the Python developers are recommended to use a virtual environment for the applications. Which provides an isolated environment for the application with a defined Python version.
Switch Python Version on Ubuntu & Debian
The update-alternatives command-line tool is to create and maintain symbolic links for the default commands. With the help of this, we can easily switch commands to different versions. For, this tutorial, Python 3.10, and Python 2.7 are pre-installed on a Debian system. We will create a group for both commands and set symbolic links.
- Create a symlink from /usr/bin/python2.7 to /usr/bin/python and set the group name as “python”. Later, group name will be used to switch links.
sudo update-alternatives --install /usr/bin/python python /usr/bin/python2.7 1
Outputupdate-alternatives: using /usr/bin/python2.7 to provide /usr/bin/python (python) in auto mode
sudo update-alternatives --install /usr/bin/python python /usr/bin/python3.10 2
Outputupdate-alternatives: using /usr/bin/python3.10 to provide /usr/bin/python (python) in auto mode
sudo update-alternatives --config python
Output: [Select on option]There are 2 choices for the alternative python (providing /usr/bin/python). Selection Path Priority Status ------------------------------------------------------------ 0 /usr/bin/python3.10 2 auto mode * 1 /usr/bin/python2.7 1 manual mode 2 /usr/bin/python3.10 2 manual mode Press to keep the current choice[*], or type selection number:
Conclusion
In this tutorial, you have learned about switching the default Python versions on your Ubuntu and Debian Linux systems. Instead of switching version you can also configure Python virtual environment for your applications.
How to Change Python Default Version in Debian/Ubuntu
Python is an object-oriented, high-level, general-purpose programming language. Python is a powerful and flexible language that has become one of the best programming languages due to its features such as simplicity and ease of use, and its popularity is increasing day by day. This programming language is widely used in the world because it can effectively respond to the various needs of programmers in creating web applications, desktop programming, artificial intelligence programming, and machine learning. Python will not leave you alone in other areas of programming.
One of the valuable advantages of Python is its compatibility with most operating systems. Also Python provides the possibility of installing different versions simultaneously in one operating system so that users can benefit from the features of different versions of Python and switch between different versions of Python. It Should be mentioned that besides this feature of Python, one of the common problems of server administrators is changing the default version of Python in the system. For example, if versions 2 and 3 of Python are installed on your system, by entering the python command, the default version of Python will be executed, which may not be the version you need; to use the version you want, you must type the corresponding version in front of the Python command every time, Developers know how annoying this can be.
Users often do not know how to change the default version of Python in their system, so in this article, we will teach how to change the default version of Python and how to switch between Python versions.
It has always been recommended that developers consider an isolated environment such as a virtual environment for the optimal use of Python in the development of programs and other purposes; Since Debian and Ubuntu are the most popular and widely used operating systems, buy Linux VPS is one of the best options for using Python features in program development. Therefore, in this article, we will explain how to change the default version of Python in Debian/Ubuntu. Stay with us until the end of the article.
prerequisite
- Using Ubuntu/Debian Linux VPS
- Installing Python on the VPS
- Access Linux VPS as root user with sudo privileges
How can we change the default Python version in Debian/Ubuntu?
Due to the possibility of installing several versions of Python in one operating system, solutions are also considered for Switching between different versions of Python. The update-alternatives and pyenv tools can help you in this regard.
The update-alternatives tool is effective in setting symbolic links for default commands, and the pyenv tool is a Python management tool that facilitates changing the default version of Python.
In this tutorial, we are looking for the easiest and fastest way to change the default version of Python in Debian/Ubuntu, so we will use the update-alternatives command line tool.
Step 1: Checking the available versions of Python in Debian/Ubuntu
The first step is to find out what versions of Python are available on your Debian/Ubuntu system. The ls command helps you check binary Python executable files in Debian and Ubuntu. So run the following command with this goal:
/usr/bin/python /usr/bin/python2 /usr/bin/python2.7 /usr/bin/python3 /usr/bin/python3.4 /usr/bin/python3.4m /usr/bin/python3m
Step 2: Checking the default version of Python in the system
To find out the default version of Python in Debian/Ubuntu, use the following command:
Note: If the previous command did not display the default version of Python, run the python3 —version command.
Step 3: Changing the default version of Python based on each user
You can change the Python executable file based on each user. For this purpose, you must create an alias in the user’s home directory and add the alias to the contents of the ~/.bashrc file:
alias python='/usr/bin/python3.4'
After making the changes and logging back in, source the .bashrc file:
Now, by checking the default version of Python, you will see your changes applied:
As a result, you could easily change the default version of Python in Debian/Ubuntu.
Step 4: Switching the version of Python with its alternative versions
To change the version of Python with its alternatives on the system, you need to ensure that Python alternatives are configured in Debian/Ubuntu. Using the update-alternatives Python command line tool is helpful in this regard. Therefore, with Root user privileges, enter the following command to access a list of available Python options in the system:
sudo update-alternatives --list python
update-alternatives: error: no alternatives for python
When you receive the output above, you’ll notice that the Python alternative is not configured on the system. So, to configure two Python Alternatives, for example, python2.7 and python3.4, you need to update the Alternatives table and then add the desired Alternatives to it:
update-alternatives --install /usr/bin/python python /usr/bin/python2.7 1 update-alternatives --install /usr/bin/python python /usr/bin/python3.4 2
By using the update-alternatives tool and –install command, you can create and set symbolic link and set symlink from /usr/bin/python2.7 to /usr/bin/python and /usr/bin/python3.10 for /usr/bin/python. The number argument at the end of the previous command determines the priority. The priority is set manually or by selecting the highest priority number, in this example, we specified priority 2 for /usr/bin/python3.4 to set /usr/bin/python3.4 as the default Python version.
To make sure /usr/bin/python3.4 is set as the default Python version, run the following command:
Step 5: Confirming configuration and availability of Python alternatives
To check the Python alternatives and ensure that Python alternatives are configured and available, enter the following command:
update-alternatives --list python
/usr/bin/python2.7 /usr/bin/python3.4
Step 6: changing the Python versions
Finally, you can switch between alternative versions of Python whenever you like. For this purpose, switch to the desired version by running the following command and selecting the desired number:
update-alternatives --config python
There are 2 choices for the alternative python (providing /usr/bin/python). Selection Path Priority Status ------------------------------------------------------------ * 0 /usr/bin/python3.4 2 auto mode 1 /usr/bin/python2.7 1 manual mode 2 /usr/bin/python3.4 2 manual mode Press to keep the current choice[*], or type selection number: 1
In this output, python3.4 is set as the current version. By selecting the desired number, you can switch to your favorite Python version. In this example, we select number 1 (python2.7) and enter the following command to check the changes:
If you ever need to switch to Python 3, repeat step 5 and type the Python3 number to switch to Python 3.
FAQ
What is the fastest way to manage different versions of Python?
If you use multiple versions of Python Simultaneously, the update-alternatives command line tool is the fastest and easiest way to manage and change different versions of Python. By using the update-alternatives command, you can prioritize the executable Python, and the version that has a high priority is set as the default version.
How to know the available version of Python in Ubuntu/Debian?
By running the python -V command, the available version of Python will be displayed.
Which version of Python is installed by default on Ubuntu?
Python 3 is installed by default in Ubuntu.
What is the most stable version of Python to use in Debian/Ubuntu?
Python 3.10 is the best and most stable version to use on Ubuntu and Debian.
Conclusion
One of the valuable features of the Python programming language is the possibility of installing and using several different versions of Python simultaneously on the operating system. This feature, along with its advantages, brings challenges regarding switching between different versions and changing the default version of Python in the system.
In this tutorial, you learned how to change the default version of Python and switch between different versions of Python in Debian and Ubuntu in the simplest way. Learning this topic is effective in the progress of developers’ projects. It also offers programmers the possibility of using an advanced platform for coding without interfering with old programs for various purposes.
We hope that reading this article has brought you closer to your goals.
How to Change the Default Python Version to Python 3.9
After installing the latest version of Python 3 on your system, you will notice that an older version is still used when executing Python VIA the python and python3 commands.
In this article, we will learn how to change the default Python version so that it can be used without explicitly typing a version number.
Setting the Latest Python Version
If you have followed my article on how to install Python 3.9 on Ubuntu, you will notice that 3.9 is not the version used when running the following command:
To update Python to the latest version installed on your system, we can use update-alternatives to change the python shortcut command to point to Python 3.9.
Before we do this, we need to add Python 3.9 as an option in update-alternatives . Start by listing all the installed versions of Python 3 so you can choose the version options to add.
You may also want to list Python 2.* versions if you want to add one of them as an option:
Now run the following commands changing the version numbers to suit what you have.
sudo update-alternatives --install /usr/bin/python python /usr/bin/python3.6 1
sudo update-alternatives --install /usr/bin/python python /usr/bin/python3.9 2
sudo update-alternatives --install /usr/bin/python python /usr/bin/python2.7 3
update-alternatives: using /usr/bin/python3.6 to provide /usr/bin/python (python) in auto mode update-alternatives: using /usr/bin/python3.9 to provide /usr/bin/python (python) in auto mode update-alternatives: using /usr/bin/python2.7 to provide /usr/bin/python (python) in auto mode
Note — the number at the end of each command is the priority number.
Now you can change the default version of Python using the following command:
sudo update-alternatives --config python
There are 3 choices for the alternative python (providing /usr/bin/python). Selection Path Priority Status ------------------------------------------------------------ * 0 /usr/bin/python2.7 3 auto mode 1 /usr/bin/python2.7 3 manual mode 2 /usr/bin/python3.6 1 manual mode 3 /usr/bin/python3.9 2 manual mode
Type the number of the version you wish to set as the default and press ENTER .
Now check the default Python version has changed:
Conclusion
In this tutorial, you have learned how to create a list of alternate Python versions for update-alternatives and set the default.