15%

Save 15% on All Hosting Services

Test your skills and get Discount on any hosting plan

Use code:

Skills
Get Started
07.10.2024

Comprehensive Guide to Installing Python 3 on CentOS 7

CentOS 7, renowned for its stability and reliability, is a preferred choice for server environments. However, it ships with Python 2.x by default, which is outdated for many modern applications that require Python 3. This guide provides a step-by-step process to install Python 3 on CentOS 7, ensuring your system is equipped to handle contemporary software requirements.

Quick Start: Python 3 Installation on CentOS 7

To install Python 3 on CentOS 7, you must first update your system, enable the EPEL repository, and then install the desired Python version. This guide will walk you through these steps, providing detailed instructions and expert insights to optimize your setup.

Prerequisites

Before proceeding, ensure you have:

  • Root or sudo access to your CentOS 7 system.
  • An updated system to ensure compatibility with the latest packages.

Step 1: System Update

Updating your system is crucial to ensure all existing packages are current. Execute the following command:

“`bash

sudo yum update -y

“`

This command updates all packages to their latest versions available in the CentOS repositories.

Step 2: Install Development Tools

To compile Python or install certain packages, you need essential development tools:

“`bash

sudo yum groupinstall -y "Development Tools"

sudo yum install -y yum-utils

“`

These tools include `gcc`, `make`, and other libraries necessary for building Python and its extensions.

Step 3: Enable the EPEL Repository

Python 3 is not available in the default CentOS 7 repositories. To access it, enable the EPEL (Extra Packages for Enterprise Linux) repository:

“`bash

sudo yum install -y epel-release

“`

The EPEL repository contains additional packages, including Python 3, that are not found in standard CentOS repositories.

Step 4: Install Python 3

With the EPEL repository enabled, choose your Python version:

Install Python 3.6

For a stable version, install Python 3.6:

“`bash

sudo yum install -y python3

“`

This command installs Python 3.6 along with `pip3`, the package manager for Python.

Install a Newer Python Version (e.g., Python 3.9)

If you require a newer version, such as Python 3.9, utilize Software Collections (SCL):

“`bash

sudo yum install -y centos-release-scl

sudo yum install -y rh-python39

“`

Enable Python 3.9:

“`bash

scl enable rh-python39 bash

“`

This command allows you to use Python 3.9 within the current terminal session.

Step 5: Verify the Installation

Confirm the installation by checking the Python version:

“`bash

python3 –version

“`

You should see output like `Python 3.6.8` or `Python 3.9.x`, depending on your installation.

Step 6: Set Python 3 as Default (Optional)

To make Python 3 the default version, create an alias:

“`bash

echo 'alias python=python3' >> ~/.bashrc

source ~/.bashrc

“`

This alias sets Python 3 as the default for `python` commands in your shell sessions.

Step 7: Install pip for Python 3

`pip` is typically installed with Python 3. If not, install it with:

“`bash

sudo yum install -y python3-pip

“`

Verify the installation:

“`bash

pip3 –version

“`

This should display the installed version of `pip`, confirming it is ready for use.

Step 8: Create a Python Virtual Environment

Using a virtual environment is recommended for isolating project dependencies:

“`bash

python3 -m venv myprojectenv

“`

Activate the virtual environment:

“`bash

source myprojectenv/bin/activate

“`

The prompt change indicates the virtual environment is active. To deactivate, run:

“`bash

deactivate

“`

Key Takeaways for Python Installation on CentOS 7

  • Always update your system before installing new software to ensure compatibility.
  • Enable the EPEL repository to access Python 3 and other essential packages.
  • Utilize virtual environments to manage dependencies effectively.

Internal Linking to Enhance Your Hosting Experience

For those looking to expand their hosting capabilities, consider exploring VPS Hosting, Dedicated Servers, and SSL Certificates to enhance security and performance.

FAQ

Q1: Why is Python 3 preferred over Python 2?

Python 3 offers modern features, improved syntax, and better performance, making it essential for current applications.

Q2: Can I run both Python 2 and Python 3 on CentOS 7?

Yes, both versions can coexist. Use `python` for Python 2 and `python3` for Python 3.

Q3: What is the EPEL repository?

EPEL (Extra Packages for Enterprise Linux) provides additional packages not included in the standard CentOS repositories, essential for modern software requirements.

Q4: How do I switch between Python versions?

Use the `scl enable` command to switch between installed Python versions within terminal sessions.

Q5: Is it necessary to use a virtual environment?

While not mandatory, virtual environments are highly recommended to manage dependencies and avoid conflicts between projects.

15%

Save 15% on All Hosting Services

Test your skills and get Discount on any hosting plan

Use code:

Skills
Get Started