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
pythoncommand, 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.
- 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-alternativesand pyenv tools can help you in this regard.
update-alternativestool 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
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:
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
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.
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.
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.