You are on page 1of 7

 July 30, 2023  Python, Tutorials, Ubuntu

In this tutorial, we are going to explain in step-by-step detail how to install multiple Python versions on Ubuntu 22.04

Python is a high-level, general-purpose programming language compatible with di�erent operating systems such as MacOS,
Windows, Linux and etc.

In this blog post, we will focus on installing Python in the Linux distributions. Also, sometimes on one Linux server, it is necessary
to be installed multiple Python versions. This tutorial will teach you about installation and changing between di�erent versions.

Installing multiple Python versions on Ubuntu 22.04 is a very easy and straightforward process that may take up to 15 minutes.
Let’s get things done!

Prerequisites
• Fresh install of Ubuntu 20.04
• User privileges: root or non-root user with sudo privileges

Update the System


Every fresh installation of the operating system needs the packages to be updated to the latest versions available.

sudo apt update -y && sudo apt upgrade -y

Step 1. Install Python 3.10


We will start with the latest version available in the default repository of Ubuntu 22.04. To install execute the
following command:
sudo apt install python3.10 -y

To check the installed version, execute the command . You should receive the following output:

root@host:~# python3.10 -V
Python 3.10.6

Step 2. Install Python 3.8


The second Python version on our list will be Python 3.8. To install it, execute the following command:

sudo apt install python3.8 -y

To check the installed version, execute the command . You should receive the following output:

root@host:~# python3.8 -V
Python 3.8.16

Step 3. Install Python 3.6


The third Python version will be the Python 3.6 version. Let’s execute the following command:

sudo apt install python3.6 -y

As you noticed, the installation process failed with the following message:

root@host:~# sudo apt install python3.6 -y


Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
E: Unable to locate package python3.6
E: Couldn't find any package by glob 'python3.6'

This means that Python3.6 is not in the default repository of Ubuntu 22.04. We will need to download the build from the source.
To do that, execute the following command:

cd /opt

wget https://www.python.org/ftp/python/3.6.9/Python-3.6.9.tgz

tar xfv Python-3.6.9.tgz

cd /Python-3.6.9

Before we proceed with installation, we need to install the prerequisites for the older Python versions:

sudo apt-get install -y make build-essential libssl-dev zlib1g-dev libbz2-dev libreadline-dev libsqlite3-dev wget curl
llvm libncurses5-dev libncursesw5-dev xz-utils tk-dev libffi-dev liblzma-dev libgdbm-dev libnss3-dev libedit-dev libc6-
dev

After all these preparations, we can �nally build and install it with the following commands:

./configure --enable-optimizations

sudo make altinstall

To check the installed version, execute the command . You should receive the following output:

root@host:/opt/Python-3.6.9# python3.6 -V
Python 3.6.9

Step 4. Install Python 3.5


The last version of Python in this blog post will be the Python3.5 version. We will download and install the same way as the
Python3.6

cd /opt

wget https://www.python.org/ftp/python/3.5.10/Python-3.5.10.tgz

tar xfv Python-3.5.10.tgz

cd Python-3.5.10/

./configure --enable-optimizations

sudo make altinstall

To check the installed version, execute the command . You should receive the following output:

root@host:/opt/Python-3.5.10# python3.5 -V
Python 3.5.10

Step 5. Switch between Python versions


In the last step of this tutorial, we are going to explain how you can switch between di�erent Python versions. First, you need to
know that the newer Python versions installed from the default repository are located in the directory on the server,
while the older versions built from the source are located in directory.

We need to create symbolic links for every installed Python version, including the path of the installed Python version. Execute
the following commands one by one:

sudo update-alternatives --install /usr/bin/python python /usr/bin/python3.10 1


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

Your command line should look like this:

root@host:/# sudo update-alternatives --install /usr/bin/python python /usr/bin/python3.10 1


update-alternatives: using /usr/bin/python3.10 to provide /usr/bin/python (python) in auto mode
root@host:/opt/Python-3.6.9# sudo update-alternatives --install /usr/bin/python python /usr/bin/python3.8 2
update-alternatives: using /usr/bin/python3.8 to provide /usr/bin/python (python) in auto mode
root@host:/opt/Python-3.6.9# sudo update-alternatives --install /usr/local/bin/python python /usr/local/bin/python3.6 3
update-alternatives: renaming python link from /usr/bin/python to /usr/local/bin/python
update-alternatives: using /usr/local/bin/python3.6 to provide /usr/local/bin/python (python) in auto mode
root@host:/opt/Python-3.6.9# sudo update-alternatives --install /usr/local/bin/python python /usr/local/bin/python3.5 4
update-alternatives: using /usr/local/bin/python3.5 to provide /usr/local/bin/python (python) in auto mode

To list and choose which Python version should be active, execute the following command:

sudo update-alternatives --config python

The output should look like this:

root@host:/# sudo update-alternatives --config python


There are 4 choices for the alternative python (providing /usr/local/bin/python).

Selection Path Priority Status


------------------------------------------------------------
* 0 /usr/local/bin/python3.5 4 auto mode
1 /usr/bin/python3.10 1 manual mode
2 /usr/bin/python3.8 2 manual mode
3 /usr/local/bin/python3.5 4 manual mode
4 /usr/local/bin/python3.6 3 manual mode

You can enter any number and use that Python version. For example, we choose the Python3.10 by entering the number 1

Execute the command again, and you will notice the changed version:
root@host:/# sudo update-alternatives --config python
There are 4 choices for the alternative python (providing /usr/local/bin/python).

Selection Path Priority Status


------------------------------------------------------------
0 /usr/local/bin/python3.5 4 auto mode
* 1 /usr/bin/python3.10 1 manual mode
2 /usr/bin/python3.8 2 manual mode
3 /usr/local/bin/python3.5 4 manual mode
4 /usr/local/bin/python3.6 3 manual mode

Congratulations! You successfully installed multiple Python versions on your system and learned how to switch them. Of course,
you do not have to do this by yourself. You can contact our technical support, and the admins will help you with any aspect of the
installation and con�guration of Python versions. We are available 24/7.

If you liked this post on how to Install and Switch Python Versions in Ubuntu 22.04, please share it with your friends on social
networks or simply leave a reply below. Thanks.

python versions ubuntu

September 27, 2023 at 3:56 am

i would like to do something similar on ubuntu23.04


but unfortuately this is not working
REPLY

September 28, 2023 at 3:15 am

Are you facing any errors? The commands are the same, but repositories for certain versions of Python must be
added manually.
REPLY

January 15, 2024 at 6:10 am

Hi,

this is great – I searched for such a instruction quiet some time. Thank you!

It seems everything worked for me, but when I tried to install pip for python3.6 I got a segmentation fault. Do you have
any idea how I could install pip?

Thanky you,
best,
Nadine
REPLY

March 14, 2024 at 10:41 pm

Hi thanks for the tutorials, it works well. Thank you.

Is there any guide how to have di�erent pip versions ? I already installed python 3.10, 3.6 and 3.5 however the pip only
point out to python3.10.

Regards,
Iwan
REPLY

March 15, 2024 at 2:48 am

You can use the following command to install a package on a speci�c Python version: python3.6 -m pip install
“package”

Remember to change the version number on the Python command to install the package in the version you want.
REPLY

Your email address will not be published. Required �elds are marked *

Comment *

Name *

Email *

I'm not a robot


reCAPTCHA
Privacy - Terms

POST COMMENT

 How to Install Tar.gz on AlmaLinux 9 How to Install Anaconda on Ubuntu 22.04 

Search… 

(https://www.rosehosting.com/nvme-hosting/?
mtm_campaign=blogs&mtm_source=lhs&mtm_medium=blog&mtm_content=nvme-
vps&mtm_cid=1346&mtm_group=RH&mtm_placement=sidebar)

(https://www.linuxhostsupport.com)

RECENT POSTS

How to Fix Themes Not Installing on WordPress (https://linuxhostsupport.com/blog/how-to-�x-themes-not-installing-on-wordpress/)

The Best Self-Hosted Email Clients in 2024 (https://linuxhostsupport.com/blog/the-best-self-hosted-email-clients-in-2024/)

How to Check and Change the Time Zone on Linux (https://linuxhostsupport.com/blog/how-to-check-and-change-the-time-zone-on-linux/)

How to Install PHP 8 on Debian (https://linuxhostsupport.com/blog/how-to-install-php-8-on-debian/)

What to Consider When Choosing a Hosting Provider (https://linuxhostsupport.com/blog/what-to-consider-when-choosing-a-hosting-provider/)

CATEGORIES

 Analytics (https://linuxhostsupport.com/blog/category/analytics/)

 CMS, CRM, ERP (https://linuxhostsupport.com/blog/category/cms-crm-erp/)

 Databases (https://linuxhostsupport.com/blog/category/databases/)

 Debian (https://linuxhostsupport.com/blog/category/debian/)

 Guides (https://linuxhostsupport.com/blog/category/guides/)

 Laravel (https://linuxhostsupport.com/blog/category/laravel/)

 Linux (https://linuxhostsupport.com/blog/category/linux/)

 Miscellaneous (https://linuxhostsupport.com/blog/category/miscellaneous/)

 Open-source (https://linuxhostsupport.com/blog/category/open-source/)

 Python (https://linuxhostsupport.com/blog/category/python/)

 Security (https://linuxhostsupport.com/blog/category/security/)

 Tutorials (https://linuxhostsupport.com/blog/category/tutorials/)

 Ubuntu (https://linuxhostsupport.com/blog/category/ubuntu/)

 Ubuntu 18.04 (https://linuxhostsupport.com/blog/category/ubuntu-18-04/)

Company
About Us

Contact Us

Blog

Services
Monthly Server Management
Per Incident Server Support

Outsourced Hosting Support

Other Management & Support Services

Support
Client Area

Follow us
Facebook (https://www.facebook.com/linuxhostsupport)

Twitter (https://twitter.com/lnxhostsupport)

LinkedIn (https://www.linkedin.com/company/linuxhostsupport.com)

Terms of Service Privacy Policy

Copyright 2024 © Rose Web Services LLC.

You might also like