Install Python 3 Linux Centos

admin13 April 2024Last Update :

Understanding Python 3 Installation on CentOS Linux

Python is a versatile and widely-used programming language that’s known for its ease of use and readability. It’s a popular choice for web development, data analysis, artificial intelligence, and more. CentOS, on the other hand, is a stable, predictable, manageable, and reproducible platform derived from the sources of Red Hat Enterprise Linux (RHEL). Installing Python 3 on CentOS Linux is a straightforward process, but it requires careful attention to ensure that the system’s integrity and other Python-dependent applications are maintained.

Prerequisites for Installing Python 3 on CentOS

Before proceeding with the installation of Python 3 on CentOS, it’s important to ensure that your system meets the necessary prerequisites. Here’s what you need to check:

  • A CentOS Linux system (CentOS 7 or 8)
  • Access to a user account with sudo privileges
  • An internet connection to download the necessary packages
  • Basic knowledge of Linux command line interface

Choosing the Right Python 3 Version

Python 3 has several versions, each with its own set of features and improvements. It’s crucial to choose the version that best fits your project’s requirements. You can check the official Python website for the latest releases and their respective features.

Installing Python 3 on CentOS Using Yum

The simplest way to install Python 3 on CentOS is by using the default package manager, yum. Here’s how you can do it:

Step 1: Update the System

Before installing any new software, it’s a good practice to update your system’s package index. This ensures that you have the latest updates and security patches.

sudo yum update

Step 2: Install Python 3

CentOS repositories contain Python 3 packages that can be installed directly using yum. To install Python 3, use the following command:

sudo yum install python3

This command will install the latest version of Python 3 available in the CentOS repositories along with its dependencies.

Step 3: Verify the Installation

After the installation is complete, you can verify it by checking the version of Python 3 that was installed:

python3 --version

Installing Python 3 from Source

Sometimes, the version of Python 3 that you need might not be available in the CentOS repositories. In such cases, you can compile and install Python from source.

Step 1: Install Required Build Dependencies

To compile Python from source, you need to install the build dependencies first:

sudo yum groupinstall "Development Tools"
sudo yum install openssl-devel bzip2-devel libffi-devel

Step 2: Download Python Source Code

Next, download the source code for the version of Python 3 you wish to install from the official Python website.

wget https://www.python.org/ftp/python/3.x.x/Python-3.x.x.tgz

Replace 3.x.x with the specific version number you’re installing.

Step 3: Extract the Source Code and Compile

After downloading, extract the tarball and compile the source code:

tar xzf Python-3.x.x.tgz
cd Python-3.x.x
./configure --enable-optimizations
make altinstall

The –enable-optimizations option will optimize the Python binary by running multiple tests, which might take a while. The altinstall command is used to prevent replacing the default python binary file in the system.

Step 4: Verify the Installation from Source

Once the compilation is complete, you can verify the installation by calling Python with its version number:

python3.x --version

Again, replace 3.x with the specific version number you installed.

Setting Up a Virtual Environment

Using virtual environments is a best practice when working with Python projects. It allows you to create isolated spaces on your system for different projects, preventing package conflicts.

Step 1: Install the Python 3 Virtual Environment Package

First, install the package that allows you to create virtual environments:

sudo yum install python3-venv

Step 2: Create a Virtual Environment

To create a virtual environment, navigate to your project directory and run:

python3 -m venv my_project_env

Replace my_project_env with the name you wish to give your virtual environment.

Step 3: Activate the Virtual Environment

To activate the virtual environment, use the following command:

source my_project_env/bin/activate

You’ll notice that the command prompt changes to indicate that you are now working inside the virtual environment.

Managing Python Packages with Pip

Pip is the package installer for Python. You can use it to install, update, and remove Python packages.

Step 1: Ensure Pip is Installed

Python 3.4 and above come with pip by default. You can ensure it’s installed with:

python3 -m ensurepip --upgrade

Step 2: Install Packages with Pip

To install a package using pip, simply run:

pip install package_name

Replace package_name with the name of the package you want to install.

Step 3: List Installed Packages

To list all installed packages in the current environment, use:

pip list

Step 4: Uninstall Packages

To remove a package, use the following command:

pip uninstall package_name

FAQ Section

What is the difference between make install and make altinstall?

make install will overwrite the default system python binary, while make altinstall prevents this by installing Python alongside the system’s default Python version.

How can I switch between multiple Python versions?

You can use the alternatives system or install a version management tool like pyenv to switch between multiple Python versions.

Is it safe to remove Python 2 from CentOS?

Removing Python 2 is not recommended as some system tools still rely on it. It’s better to install Python 3 alongside Python 2.

Can I use yum to install specific versions of Python 3?

Yum can only install packages that are available in its repositories. If a specific version is not available, you’ll need to compile it from source.

How do I ensure that my Python environment is secure?

Keep your Python environment secure by regularly updating Python and all installed packages, using virtual environments, and following best practices for code security.

References

Leave a Comment

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


Comments Rules :