Python is one of the most widely used programming languages, known for its simplicity and versatility. Whether you’re developing web applications, data science projects, or automation scripts, Python is an essential tool for developers. While CentOS 7 server comes with Python 2.x by default, many modern applications and frameworks now require Python 3. In this article, we will walk you through the steps to install Python 3 on CentOS 7, ensuring you can work with the latest version of Python.

Prerequisites

Before installing Python 3, you need to ensure your system is updated and that you have root or sudo privileges. This will help avoid any permission issues during installation.

  1. Access to a CentOS 7 server

  2. Root or sudo privileges

  3. Basic knowledge of using the command line

Step 1: Update the System

It’s always a good idea to update your system packages before installing new software. Open a terminal and run the following command to update your CentOS 7 system:

sudo yum update -y

This will ensure that all existing packages are up to date and any security patches are applied.

Step 2: Enable EPEL Repository

The Extra Packages for Enterprise Linux (EPEL) repository contains additional packages that are not included in the default CentOS repositories. To install Python 3 on CentOS 7, you will need to enable the EPEL repository first.

To enable the EPEL repository, run the following command:

sudo yum install epel-release -y

Once the repository is enabled, your system will have access to a wide range of additional packages, including Python 3.

Step 3: Install Python 3

Now that the EPEL repository is enabled, you can proceed to install Python 3. CentOS 7 uses the yum package manager, so you can use the following command to install Python 3:

sudo yum install python3 -y

This command will install Python 3 and the associated tools, including pip (Python’s package manager).

Step 4: Verify the Installation

After the installation is complete, verify that Python 3 has been installed correctly by checking the version. Run the following command to check the Python version:

python3 --version

You should see an output similar to:

Python 3.x.x

This confirms that Python 3 has been successfully installed on your CentOS 7 system.

Step 5: Install pip for Python 3 (Optional)

pip is the package manager for Python that allows you to install additional Python packages. While pip is often installed automatically along with Python 3, you can verify if it was installed by running:

pip3 --version

If pip is not installed, you can install it manually with the following command:

sudo yum install python3-pip -y

Once installed, you can use pip to install additional Python packages:

pip3 install <package_name>

Step 6: Set Python 3 as the Default (Optional)

By default, CentOS 7 uses Python 2.x as the system Python. If you’d like to make Python 3 the default version when running the python command, you can create an alias.

To make this change, open the .bash_profile file for your user:

nano ~/.bash_profile

Then, add the following line at the end of the file:

alias python=python3

Save and exit the file (CTRL+X, then Y, and press Enter). To apply the changes, reload your profile:

source ~/.bash_profile

Now, when you run the python command, it will invoke Python 3.

Step 7: Installing Additional Libraries (Optional)

If you need additional libraries or development tools, you can install them using yum. For example, if you want to install the development tools needed for compiling Python extensions, you can run:

sudo yum groupinstall "Development Tools" -y

This will install a set of tools that can help you build and manage Python packages and extensions.

Conclusion

Installing Python 3 on CentOS 7 is a straightforward process. By following the steps outlined in this article, you’ll be able to set up Python 3 on your CentOS 7 system and start developing with the latest version of Python.

Remember, Python 3 is now the standard for most modern applications, and it’s important to ensure you are working with the latest version. If you ever need to update Python 3, you can do so by running:

sudo yum update python3 -y

Now you’re ready to take full advantage of Python 3’s powerful features, libraries, and frameworks on your CentOS 7 server!