The objective of this tutorial is to provide the reader with an easy to follow way of switching between alternative Python versions on Ubuntu 20.04 Focal Fossa Desktop/Server.
In this tutorial you will learn:
- How to check installed Python versions
- How to switch between alternative Python versions
Software Requirements and Conventions Used
Category | Requirements, Conventions or Software Version Used |
---|---|
System | Installed Ubuntu 20.04 or upgraded Ubuntu 20.04 Focal Fossa |
Software | Python2/Python3 |
Other | Privileged access to your Linux system as root or via the sudo command. |
Conventions |
# – requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command$ – requires given linux commands to be executed as a regular non-privileged user |
How to switch between alternative Python Versions step by step instructions
- First step is to check what python versions are available on your Ubuntu system. To do so execute the following command:
$ ls /usr/bin/python* /usr/bin/python2 /usr/bin/python2.7 /usr/bin/python3 /usr/bin/python3.7 /usr/bin/python3.7m /usr/bin/python3.8 /usr/bin/python3-futurize /usr/bin/python3m /usr/bin/python3-pasteurize
- Next, check if you already have some python alternatives configured. To do so run:
sudo update-alternatives --list python update-alternatives: error: no alternatives for python
Currently the system has no python alternatives configured.
- In this step we are going to set two Python alternatives, namely it will by Python2 and Python3 alternative. Execute the following commands:
$ sudo update-alternatives --install /usr/bin/python python /usr/bin/python2 1 $ sudo update-alternatives --install /usr/bin/python python /usr/bin/python3 2
- Confirm that both alternatives are ready to be used:
$ sudo update-alternatives --list python /usr/bin/python2 /usr/bin/python3
- Change to alternative python version. For example to change to Python 2 execute the following command:
$ sudo update-alternatives --config python There are 2 choices for the alternative python (providing /usr/bin/python). Selection Path Priority Status ------------------------------------------------------------ * 0 /usr/bin/python3 2 auto mode 1 /usr/bin/python2 1 manual mode 2 /usr/bin/python3 2 manual mode Press
to keep the current choice[*], or type selection number: 1 Enter selection number. In this case to switch to Python version 2 we enter the
1
selection number. - Check your python version:
$ python -V Python 2.7.17
To switch to Python 3 alternative repeat
Step 5 and enter the selection number appropriate to your desired python version.