The CentOS 7 Linux distribution includes Python 2 by default. However, Python 2 is going to reach EOL on January 1, 2020. While some legacy applications might require access to Python 2 for various reasons, it’s vitally important to kick start new projects in Python 3.
In this tutorial, we are going to take a look at how to get up and running with Python 3 on a CentOS 7 server. Specifically, we will take a look at how to install Python 3 via the CentOS 7 package manager Yum as well as from source.
- These instructions are being performed on a Liquid Web Self-Managed Dedicated CentOS 7 server as the root user.
- These instructions assume that your server has CentOS release 7.7.
- We also have an awesome article on managing multiple versions of Python via Anaconda
In CentOS 7 releases prior to 7.7, it was necessary to make Python 3 available for installation by setting up third-party repositories, such as the IUS repository, because the CentOS base repository did not provide a Python 3 package. Thankfully, as of CentOS 7.7, Python 3 is available in the base package repository!
Step 1: Update the environment
In order to make sure that we are working with the most up to date environment possible in terms of our packages, we can run the following command.
[root@centos7 ~]# yum update -y
Step 2: Install Python 3
Now that the environment is up to date, all we need do to install Python 3 is run the following command.
[root@centos7 ~]# yum install -y python3
That’s it! Python 3 is now installed! Another helpful idea to consider is that PIP, the Python package manager for Python 3, is installed alongside the Python 3 package, so we don’t have to worry about that as an additional installation step.
In order to ensure that Python 3 is in fact installed and usable, we can drop into a Python 3 shell by running the following command.
[root@centos7 ~]# python3 Python 3.6.8 (default, Aug 7 2019, 17:28:10) [GCC 4.8.5 20150623 (Red Hat 4.8.5-39)] on linux Type "help", "copyright", "credits" or "license" for more information. >>>
You should see the version of Python 3 installed on your system as well as a change in the command prompt characters.
Installing Python 3 via the Yum package manager is by far the simplest way to get the job done. However, in some cases, you might want to have the most recent version of Python available and that’s where a source installation can come in handy.
Step 1: Setup the Environment
In order to install Python 3 from source, we are going to need to ensure that some prerequisite packages are installed on our system.
[root@centos7 ~]# yum install gcc openssl-devel bzip2-devel libffi-devel -y
Step 2: Download Python
Next, we need to grab the version of Python we want. The following command will pull down the latest stable version of Python 3.8 as of the writing of this article.
[root@centos7 ~]# curl -O https://www.python.org/ftp/python/3.8.1/Python-3.8.1.tgz
Now we need to extract the file.
[root@centos7 ~]# tar -xzf Python-3.8.1.tgz
Step 3: Install Python 3
Now that it’s extracted, let’s change into the resultant directory.
[root@centos7 ~]# cd Python-3.8.1/
Next, we need to prepare to compile Python from source.
[root@centos7 Python-3.8.1]# ./configure --enable-optimizations
Finally, we are going to use the following command to finish off the installation, without replacing the default system Python on our system.
[root@centos7 Python-3.8.1]# make altinstall
Compiling code from source takes a little while, but once that’s finished, we can test out our new Python 3 version by running the following command.
[root@centos7 Python-3.8.1]# python3.8
Much like before when we installed Python 3.6 via Yum, we are dropped into a Python shell that outputs the version we are currently using.
Python 3.8.1 (default, Dec 27 2019, 17:12:30) [GCC 4.8.5 20150623 (Red Hat 4.8.5-39)] on linux Type "help", "copyright", "credits" or "license" for more information. >>>
Python has seen a continuous upward growth trend in the 21st century due to its flexibility in addressing both frontend and backend development goals. It also continues to provide, a strong cross-platform functionality when utilizing server clustering, along with the vast amount of available tools and libraries available that reduces the effort needed to write code and functions. Additionally, it has one of the industries’ strongest support community structures which ensures its continued success.
Because Python provides for the latest technologies with its constant growth and development in the field of Machine Learning and AI, we expect to see these advances continue to increase in the coming years.
How Can We Help?
Liquid Web delivers many of the newest technology private cloud hosting options that can allow you to take advantage of the latest methods and processes to grow your business well into the 21st century and beyond!
Give us a call at 800.580.4985, or open a chat or ticket with us to speak with one of our knowledgeable Solutions or Experienced Hosting advisors to learn how you can apply these tools and techniques today!