Change Python Default Version in DebianUbuntu

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 thepythoncommand, 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

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. Theupdate-alternativesand pyenv tools can help you in this regard.

Theupdate-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:

ls /usr/bin/python*

Output:

/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:

python --version

Output:

Python 2.7.8

Note: If the previous command did not display the default version of Python, run thepython3 --versioncommand.

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:

. ~/.bashrc

Now, by checking the default version of Python, you will see your changes applied:

python --version

Output:

Python 3.4.2

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

Output:

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:

python --version

Output:

Python 3.4.2

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

Output:

/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

Output:

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:

python --version

Output:

Python 2.7.8

If you ever need to switch to Python 3, repeat step 5 and type the Python3 number to switch to Python 3.

FAQ

By running the python -V command, the available version of Python will be displayed.

Python 3 is installed by default in 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.

Related Article : What is Debian OS

Leave a Reply

Your email address will not be published. Required fields are marked.


lim Publish in February 11, 2024 at 6:51 am

I can very sure to say that after making those changes, it will cause the gnome-terminal to stop working. You can revert it to the original version, and the terminal will come back up.

    Liosa F Publish in February 13, 2024 at 7:38 am

    To prevent gnome-terminal issues: Install all necessary Python packages and dependencies (specific Python libraries or modules)for the new default Python version. Update specific configuration files related to GNOME Terminal according to the new Python version requirement. Check the compatibility of Dependencies and Shebang Lines in your scripts with the newly set default Python version (for example, gnome-terminal is incompatible with 3.7) and change the shebang to explicitly point to a compatible version like 3.6. you can reinstall GNOME Terminal to ensure that it's properly configured with the new Python version.