in

How to Install Python 3 on CentOS, Ubuntu and Windows? The Definitive Guide

As a data analyst and machine learning engineer, Python has become an indispensable tool in my day-to-day work. Its versatility, readability and vast collection of scientific libraries like NumPy, SciPy, Pandas, scikit-learn, TensorFlow make Python the #1 programming language for data science and analytics.

In this comprehensive guide, I‘ll share how to properly install the latest Python 3 release on the three most popular operating systems – CentOS, Ubuntu and Windows.

I‘ll cover the step-by-step installation process on each OS, along with tips, best practices and recommendations based on my experience using Python for data analysis and machine learning projects over the last 5 years.

So whether you‘re starting your first data science project or setting up a new development environment, you‘ll have Python 3 ready to go after reading this guide!

Why Python 3 and What‘s New?

Before we jump into the installation steps, let me briefly explain what Python 3 is and what‘s new compared to earlier versions of Python.

Python 3, commonly stylized as Python3, is the current major release of the Python language. Python 3 introduced several breaking changes from Python 2 which made Python code forward-compatible.

Some of the key features and improvements in Python 3 are:

  • Speed and optimizations – Python 3 code runs significantly faster, often 30% to 100% faster based on benchmarks. The interpreters and libraries have been optimized.

  • Cleaner language design – Python 3 cleaned up many inconsistencies in Python 2 to make the language more consistent and logical.

  • Unicode support – Text strings are Unicode by default in Python 3 allowing working seamlessly with text data.

  • New syntax and functions – f-strings, matrix multiplication operators, type hints, dictionary views, exception chaining and async/await functions are some new additions.

  • Improved libraries – Libraries like asyncio, Enum, pathlib, typed dictionaries, dataclasses modernize Python and make it more consistent with TypeScript/C#.

  • Active development – Python 3 is under active development with yearly releases. Python 3.9 was released in October 2020.

So in summary, Python 3 comes with many enhancements while also breaking backward compatibility with Python 2. Going forward, Python 3 is the way to go for all new development and data analytics workloads.

Alright, now that you know what Python 3 offers, let‘s look at how to install the latest version on CentOS first.

Installing Python 3 on CentOS

CentOS is a stable, free and open source Linux distribution based on Red Hat Enterprise Linux (RHEL). It is widely used for hosting web servers, databases and enterprise apps.

There are two common approaches to install Python 3 on CentOS:

  1. Using the yum package manager
  2. Compiling from source code

Let‘s explore both these methods in detail:

Installing Python 3 using yum package manager

The easiest way to install Python 3 is by using yum, the default package manager on CentOS.

Yum allows installing any available Python 3 version along with all its dependencies like pip, setuptools, development files, etc from CentOS‘s software repositories with a single command:

sudo yum install python3

When I run this on my CentOS 7 cloud server, here is the output:

$ sudo yum install python3
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: centos.excellmedia.net
 * extras: centos.excellmedia.net
 * updates: centos.excellmedia.net
Resolving Dependencies
--> Running transaction check
---> Package python3.x86_64 0:3.6.8-18.el7 will be installed
--> Processing Dependency: python3-libs(x86-64) = 3.6.8-18.el7 for package: python3-3.6.8-18.el7.x86_64
--> Processing Dependency: python3-setuptools for package: python3-3.6.8-18.el7.x86_64
--> Processing Dependency: python3-pip for package: python3-3.6.8-18.el7.x86_64
--> Processing Dependency: libpython3.6m.so.1.0()(64bit) for package: python3-3.6.8-18.el7.x86_64
--> Running transaction check
---> Package python3-libs.x86_64 0:3.6.8-18.el7 will be installed
--> Processing Dependency: libtirpc.so.1()(64bit) for package: python3-libs-3.6.8-18.el7.x86_64
---> Package python3-pip.noarch 0:9.0.3-8.el7 will be installed
---> Package python3-setuptools.noarch 0:39.2.0-10.el7 will be installed
--> Running transaction check
---> Package libtirpc.x86_64 0:0.2.4-0.16.el7 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

=====================================================================================================================
 Package                   Arch   Version                       Repository                                Size
=====================================================================================================================
Installing:
 python3                   x86_64 3.6.8-18.el7                  updates                                   70 k
Installing for dependencies:
 libtirpc                  x86_64 0.2.4-0.16.el7                 base                                      89 k
 python3-libs              x86_64 3.6.8-18.el7                  updates                                  6.9 M
 python3-pip               noarch 9.0.3-8.el7                   base                                     1.6 M
 python3-setuptools        noarch 39.2.0-10.el7                 base                                     629 k

Transaction Summary
=====================================================================================================================
Install  1 Package (+4 Dependent packages)  

Total download size: 9.3 M
Installed size: 48 M
Is this ok [y/d/N]: y

As you can see, yum downloads all the required Python 3 packages and dependencies amounting to around 9.3 MB in total and installs them seamlessly.

Once done, Python 3 is installed and ready! We can verify this:

$ python3 --version
Python 3.6.8

I prefer yum over compiling Python manually since it handles all dependencies automatically and keeps Python up to date.

However, compiling from source is useful when you need specific Python versions or optimization flags. Let‘s look at that next.

Installing Python 3 from Source on CentOS

The alternative to yum is to compile Python 3 from the source code manually. Here are the steps:

  1. Install build dependencies – Python requires gcc, make and development files to compile:

     sudo yum install gcc openssl-devel bzip2-devel libffi-devel
  2. Download the Python 3 source code from python.org downloads. Let‘s take Python 3.6.8.

     cd /opt  
     wget https://www.python.org/ftp/python/3.6.8/Python-3.6.8.tgz
  3. Extract the gzipped tarball:

     tar xzf Python-3.6.8.tgz
  4. Change into the extracted Python-3.6.8 directory and run the configure script with desired options. Here I‘m enabling optimizations:

     cd Python-3.6.8
     ./configure --enable-optimizations
  5. Compile the Python source code. The -j flag runs make on multiple cores for faster compilation:

     make -j 8
  6. Finally, install the compiled Python by running make altinstall which keeps the system python intact:

     sudo make altinstall

The pros of compiling from source are:

  • Can install specific Python versions rather than yum‘s latest package
  • Can pass custom ./configure flags like --enable-optimizations for performance

The downsides are:

  • Managing dependencies like pip, setuptools yourself
  • Compilation takes time
  • Need to recompile to upgrade Python rather than yum update

Overall, I prefer using yum for simplicity and ease of maintenance. But compiling from source is a viable option as well.

And that summarizes the two main approaches for installing Python 3 on CentOS!

Next up, let‘s look at installing Python 3 on Ubuntu.

Setting up Python 3 on Ubuntu

Ubuntu is the most popular Linux distribution for desktops and development. Given Python‘s popularity, the latest Python 3 release is available in Ubuntu‘s APT repositories.

We can install Python 3 on Ubuntu using the apt command:

sudo apt update
sudo apt install python3

Here is the output log when running this on a fresh Ubuntu 18.04 server:

$ sudo apt update
Hit:1 http://us.archive.ubuntu.com/ubuntu bionic InRelease
Get:2 http://us.archive.ubuntu.com/ubuntu bionic-updates InRelease [88.7 kB]     
Get:3 http://security.ubuntu.com/ubuntu bionic-security InRelease [83.2 kB]
Fetched 171 kB in 1s (183 kB/s)   
Reading package lists... Done

$ sudo apt install python3
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following additional packages will be installed:
  libexpat1 libpython3-dev libpython3.6 libpython3.6-minimal libpython3.6-stdlib python3-minimal python3.6
  python3.6-minimal
Suggested packages:
  python3-doc python3-tk python3-venv binutils binfmt-support
The following NEW packages will be installed:
  libexpat1 libpython3-dev libpython3.6 libpython3.6-minimal libpython3.6-stdlib python3
  python3-minimal python3.6 python3.6-minimal
0 upgraded, 10 newly installed, 0 to remove and 0 not upgraded.
Need to get 7,357 kB of archives.
After this operation, 35.3 MB of additional disk space will be used.

...

Processing triggers for man-db (2.8.3-2ubuntu0.1) ...
Setting up python3-minimal (3.6.7-1~18.04) ...
Setting up python3.6-minimal (3.6.9-1~18.04ubuntu1) ...
Setting up python3 (3.6.7-1~18.04) ...

$ python3 --version  
Python 3.6.9

As we can see, apt downloads all Python 3 dependencies, installs them and sets up Python 3.6.9 properly.

Python 3 on Ubuntu works seamlessly out of the box, so I strongly recommend using the apt package manager.

Now finally, let‘s look at installing Python 3 on Windows.

Installing Python 3 on Windows

There are a few ways to install Python 3 on Windows:

1. Downloading the installer from python.org

The official Python installer from the python.org downloads page is the recommended approach for most Python users on Windows.

Here are the steps:

  1. Download the latest Python 3 installer from python.org. Choose 64-bit or 32-bit based on your Windows version.

  2. Run the downloaded .exe installer by double-clicking on it.

  3. Make sure you check the "Add Python 3.x to PATH" option during installation. This sets the PATH to include Python.

  4. Click Install Now and Python will be installed with the default options.

Once installed, open Command Prompt and verify Python 3 is available:

C:\Users\John>python --version
Python 3.8.5

This is the simplest way to get the latest Python 3 on Windows.

2. Installing from the Microsoft Store

Another option is to install Python 3 from the Microsoft Store on Windows 10 and Windows 11.

Simply search for "Python 3" in the store search box, select the app published by Python Software Foundation and click Install.

This sandboxed installation through the Microsoft Store has some security benefits but may limit writing to certain filesystem locations.

3. Using Chocolatey package manager

Chocolatey is a popular package manager for Windows that allows installing software in a manner similar to yum or apt.

To install Python 3 via Chocolatey:

choco install python

This installs the latest Python 3 release along with pip automatically.

Chocolatey is great for managing multiple Python versions and environments. But I prefer the official installer for simplicity.

And that summarizes the most common approaches for installing Python 3 on Windows!

Key Takeaways on Installing Python 3

We‘ve covered a lot of ground on how to properly install Python 3 on CentOS, Ubuntu and Windows.

Here are some key takeaways:

  • Use package managers whenever possible – yum on CentOS, apt on Ubuntu make installing Python 3 easy by handling dependencies automatically.

  • Add Python to PATH during Windows setup – Ensures Python is accessible from Command Prompt on Windows.

  • Python 3 is forward compatible – New Python code should be written for Python 3 so you can utilize the latest features and improvements.

  • Consider virtual environments – Create isolated Python environments using venv or Conda to avoid dependency conflicts when working on different Python projects.

  • Python 2 legacy support – Some enterprises still have legacy Python 2 code. In such cases, Python 2 and 3 can co-exist on the same system.

  • Learn packaging tools like pip – Use Python‘s pip and virtual environments to install and manage third party packages and dependencies.

And with that, you now have Python 3 installed on your operating system of choice!

The next step is to start writing Python code and scripts to build your data analysis and machine learning skills. I‘ll be covering more tips on learning Python and coding practices in future guides.

I highly recommend also checking out realpython.com‘s awesome Python tutorials when starting out. Their guides follow best practices and are perfectly paced for beginners.

I hope you found this guide helpful in your Python 3 installation journey! Let me know if you have any other questions.

Happy coding!

AlexisKestler

Written by Alexis Kestler

A female web designer and programmer - Now is a 36-year IT professional with over 15 years of experience living in NorCal. I enjoy keeping my feet wet in the world of technology through reading, working, and researching topics that pique my interest.