How To Install Python On Linux 2.x/3.x latest version

How do I install python an interactive high-level object-oriented language on Linux operating system?

Tutorial details
Difficulty Easy (rss)
Root privileges Yes
Requirements Package manager
Time 2m
Python runs on many operating systems such as MS-Windows, MacOS/Mac OS X, Linux, FreeBSD, OpenBSD, Solaris, AIX, and many varieties of free UNIX like systems. The easiest way to install the Python is to use package manger such as apt-get command/apt command, yum command/dnf command, and so on. This page explains how to install Python version 2.x or 3.x on Linux.

A note about Debian / Ubuntu Linux user

Make sure we update our repos:
sudo apt update
sudo apt upgrade

Use the following command to search for available versions of Python under Debian and Ubuntu Linux:
$ apt-cache search python | egrep "^python..[0-9] " --color
Here is what we see:

python3.8 - Interactive high-level object-oriented language (version 3.8)
python2.7 - Interactive high-level object-oriented language (version 2.7)
python3.9 - Interactive high-level object-oriented language (version 3.9)

Run the following command to install python version 2.x:
$ sudo apt-get install python2.7
Type the following command to install python version 3.8 or 3.9:
$ sudo apt-get install python3.8
## or ##
$ sudo apt-get install python3.9

Output indicating that I am installing Python 3.9 on Ubuntu 20.04 LTS or Debian 10/11:

Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following extra packages will be installed:
  python3.1-minimal
Suggested packages:
  python3.1-doc python3.1-profiler
The following NEW packages will be installed:
  python3.1 python3.1-minimal
0 upgraded, 2 newly installed, 0 to remove and 13 not upgraded.
Need to get 5,444 kB of archives.
After this operation, 19.9 MB of additional disk space will be used.
Do you want to continue [Y/n]? y
Get:1 http://debian.osuosl.org/debian/ squeeze/main python3.1-minimal amd64 3.1.3-1 [1,669 kB]
Get:2 http://debian.osuosl.org/debian/ squeeze/main python3.1 amd64 3.1.3-1 [3,775 kB]                                                                       
Fetched 5,444 kB in 27s (201 kB/s)                                                                                                                           
Selecting previously deselected package python3.1-minimal.
(Reading database ... 280220 files and directories currently installed.)
Unpacking python3.1-minimal (from .../python3.1-minimal_3.1.3-1_amd64.deb) ...
Selecting previously deselected package python3.1.
Unpacking python3.1 (from .../python3.1_3.1.3-1_amd64.deb) ...
Processing triggers for man-db ...
Processing triggers for menu ...
Processing triggers for desktop-file-utils ...
Processing triggers for gnome-menus ...
Setting up python3.1-minimal (3.1.3-1) ...
Setting up python3.1 (3.1.3-1) ...
Processing triggers for menu ...

Installing Python on a Red Hat (RHEL) / CentOS Linux user

Type the following command:
$ sudo yum install python
OR
# yum install python
See “RHEL 8 install Python 3 or Python 2 using yum” for more info.

How to install Python 2.x/3.x on SUSE or OpenSUSE Linux

Use the zypper command to search Python:
$ sudo zypper ref
$ sudo zypper search python-base
$ sudo zypper search python3-base

It seems Python 3 is already installed on my OpenSUSE Linux v15.2 server:

Loading repository data...
Reading installed packages...

S | Name               | Summary                               | Type
--+--------------------+---------------------------------------+-----------
i | python3-base       | Python 3 Interpreter and Stdlib Core  | package
  | python3-base       | Python 3 Interpreter and Stdlib Core  | srcpackage
  | python3-base-32bit | Python 3 Interpreter and Stdlib Core  | package
  | python3-base58     | Base58 and Base58Check implementation | package

If not installed, try:
sudo zypper install python3-base
## get version 2.x ##
sudo zypper install python2-base

How do I find out installed python version?

Type the following command:
$ python --version
Default Python version

Python 2.7.18

To see default Python 3.x version, type:
$ python3 --version
$ python3.8 --version
$ python3.9 --version

Installing latest Python 3.9 on Debian or Ubuntu using source code method

First install development tools on Debian / Ubuntu Linux as follows:
$ sudo apt install build-essential \
curl libbz2-dev \
libffi-dev libgdbm-dev \
libncurses5-dev libnss3-dev \
libreadline-dev libsqlite3-dev \
libssl-dev zlib1g-dev

Next, get latest version of Python. We can use wget to download file. You can also use curl to download file as follows:
$ VERSION="3.9.1"
$ wget "https://www.python.org/ftp/python/${VERSION}/Python-${VERSION}.tgz"

Extract the tgz archive or tarball as follows using the tar command:
$ tar -xvf Python-${VERSION}.tgz
Change directory using the cd command and configure build:
$ cd Python-${VERSION}
$ ./configure --enable-optimizations

Compile it using all CPU cores. We can pass the as follows on Linux and the nproc command returns the number of CPU cores available on your system:
$ make -j $(nproc)
Once compilation finished, install the Python on your system, run:
$ sudo make altinstall
That is all. Python 3.9.1 has been installed on your Linux system. Print Python version, run:
$ python3.9 --version

Summing up

You learned how to install Python’s latest stable version on popular Linux distros such as Debian, Ubuntu, SUSE/OpenSUSE, and RHEL/CentOS Linux.


🐧 Get the latest tutorials on Linux, Open Source & DevOps via RSS feed or Weekly email newsletter.

🐧 2 comments so far... add one

CategoryList of Unix and Linux commands
File Managementcat
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network Utilitiesdig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04
2 comments… add one
  • YRS Sep 8, 2015 @ 6:33

    Thank You So much for article, it helped me .

  • george Jan 10, 2016 @ 11:45

    whay all linux users refers only to debian or ubuntu operating system? I use mageia or openSUSE. Nobody know how to use no matter programms on this?

Leave a Reply

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

Use HTML <pre>...</pre> for code samples. Problem posting comment? Email me @ webmaster@cyberciti.biz