How to install and switch between different python versions in ubuntu 16.04.

Md Mahbubur Rahman
Analytics Vidhya
Published in
3 min readFeb 18, 2020

--

When I work with python, sometimes I face some issues with python versions. Though I know which command will work on that case but during that parti- cular time I just forgot the exact commands and I have to search on google or stackoverflow for the exact commands.

Here I will keep the notes of installing python of different versions and chan-ging the version on per use.

How to install different versions of python?

python 2.7:

Run the following command:

sudo apt install python-minimal

Now if you run the python -V command, you will get the 2.7.12 as output. Normally installation path will be /usr/bin/python.

python 3.5:

Run the following command:

sudo apt install python3

Now if you run the python -V command, you will get the 3.5.2 as output. Normally installation path will be /usr/bin/python3.5.

python 3.6:

Run the following commands:

sudo add-apt-repository ppa:deadsnakes/ppa
sudo apt update
sudo apt install python3.6

Now if you run the python -V command, you will get the 3.6.10 as output. Normally installation path will be /usr/bin/python3.6.

python 3.7:

Run the following commands:

sudo add-apt-repository ppa:deadsnakes/ppa
sudo apt update
sudo apt install python3.7

Now if you run the python -V command, you will get the 3.7.6 as output. Normally installation path will be /usr/bin/python3.7.

python 3.8:

Run the following commands:

sudo add-apt-repository ppa:deadsnakes/ppa
sudo apt update
sudo apt install python3.8

Now if you run the python -V command, you will get the 3.8.1 as output. Normally installation path will be /usr/bin/python3.8.

How to switch between different python version?

If we have multiple python versions installed in our system, then python will use only one executable. Normally if we run a program called program.py by ‘python program.py’, then this program will be run by python2.7. If we check python version by ‘python -V’, it will return 2.7.12. But if we want 3.5 as the result of ‘python -V’ and the program will be run by python 3.5 executable, then we can change the default python executable in two ways.

Creating alias in bashrc:

We can create an alias within user’s home directory and add it to the bashrc. But it will only work for the current user.

Open the bashrc file by using the following command:

nano ~/.bashrc

Add the following line to the below of the bashrc file .

alias python=‘/usr/bin/python3.5’

Now if we use check python version using ‘python -V’, it will return 3.5.2. And if we run the program.py file using ‘python program.py’, then it will be run by python3.5 executable.

Using update-alternatives:

To switch between python version over the all users, we can use update-alternatives command.

We will set priority of each version using update-alternatives. Python executable with the highest priority will be used as default python version.

We will set the priority by the following commands:


sudo update-alternatives --install /usr/bin/python python /usr/bin/python2.7 1
sudo update-alternatives --install /usr/bin/python python /usr/bin/python3.5 2sudo update-alternatives --install /usr/bin/python python /usr/bin/python3.6 3sudo update-alternatives --install /usr/bin/python python /usr/bin/python3.7 4sudo update-alternatives --install /usr/bin/python python /usr/bin/python3.8 5

Here I set the priority of python 2.7, 3.5, 3.6, 3.7, 3.8 as 1, 2, 3, 4, 5. As the python3.8 has the highest priority, the default python executable is python3.8 now.

To switch between any versions, we can use the following command:

sudo update-alternatives --config python

It will give a response as following:

Now default python executable can be changed by setting its corresponding selection number. For example, if we enter 4 , python will use python3.7 as default executable.

Note:

This story is just for my documentation purpose. Nothing serious :D :D

--

--