Set up Python 3.11 on Rocky Linux 8: Popular Programming Language

Posted on

Set up Python 3.11 on Rocky Linux 8: Popular Programming Language

Set up Python 3.11 on Rocky Linux 8: Popular Programming Language

This guide, brought to you by Orcacore, will provide a step-by-step walkthrough on how to Set up Python 3.11 on Rocky Linux 8. Python is a versatile, object-oriented, and high-level programming language renowned for its readability and ease of use.

Python’s popularity continues to soar, currently holding the position of the third most popular programming language globally, trailing only Java and C. Its widespread adoption can be attributed to several factors:

  • Beginner-Friendly Syntax: Python’s syntax is designed to be clear and intuitive, making it an excellent choice for novice programmers.
  • Extensive Libraries: A vast collection of libraries and modules allows developers to quickly implement complex functionalities without writing code from scratch.
  • Cross-Platform Compatibility: Python runs seamlessly on various operating systems, including Windows, macOS, and Linux.
  • Large Community Support: A vibrant and active community provides ample resources, documentation, and assistance for developers.
  • Versatile Applications: Python is used in a wide range of domains, including web development, data science, machine learning, scripting, and automation.
  • Set up Python 3.11 on Rocky Linux 8

Note: If you’re interested in setting up a different version, we also have a guide available for Python 3.12 setup on Rocky Linux 8.

Before proceeding, ensure you have a non-root user account with sudo privileges on your Rocky Linux 8 server. If needed, refer to our guide on Initial Server Setup with Rocky Linux 8 to configure this.

Install Python 3.11 on Rocky Linux 8

1. Install Python 3.11 on Rocky Linux 8

Let’s dive into the installation process.

Update Package Index

Begin by updating your local package index to ensure you have the latest information about available packages:

sudo dnf update -y

Install Dependencies

Next, install the necessary packages and dependencies required for compiling and installing Python 3.11:

sudo dnf install gcc openssl-devel bzip2-devel libffi-devel zlib-devel wget make tar -y

Download Python 3.11

Visit the Python Downloads page to find the latest stable version of Python 3.11. Download the source code using the wget command:

sudo wget https://www.python.org/ftp/python/3.11.2/Python-3.11.2.tgz

Extract the Archive

Extract the downloaded archive using the tar command:

sudo tar -xf Python-3.11.2.tgz

Navigate to Source Directory

Change your current directory to the extracted source directory:

cd Python-3.11.2

Run Configuration Script

Execute the configuration script to check for dependencies and prepare the build environment:

./configure --enable-optimizations

The --enable-optimizations option enables optimizations for improved performance after debugging is complete.

Build and Install Python 3.11

Start the build process using the make command:

make -j 2

The -j option specifies the number of parallel jobs to use during the build process. Set this to the number of cores in your system for faster build times. You can determine the number of cores with the nproc command:

nproc

The build process may take some time to complete, depending on your system’s resources.

Once the build is finished, install Python 3.11 using the altinstall target:

sudo make altinstall

Using altinstall prevents overwriting the system’s default Python installation.

Verify Installation

After the installation is complete, verify that Python 3.11 is installed correctly by checking its version:

python3.11 --version
Python 3.11 Rocky Linux 8

To further confirm that Python 3.11 is functioning correctly, create a test Python project.

2. Create a Test Python Project on Rocky Linux 8

Create Project Directory

Create a directory for your test project and navigate into it:

mkdir ~/test_app && cd ~/test_app

Create Virtual Environment

Inside the project directory, create a virtual environment using Python 3.11. This isolates the project’s dependencies.

python3.11 -m venv test_app_venv

Activate Virtual Environment

Activate the virtual environment:

source test_app_venv/bin/activate

Your shell prompt will change to indicate that the virtual environment is active.

Install a Package (Example)

As an example, install the apache-airflow package using pip:

pip install apache-airflow

Deactivate Virtual Environment

To exit the virtual environment, use the deactivate command:

deactivate

Conclusion

This guide demonstrated how to Set up Python 3.11 on Rocky Linux 8 and create a basic test project. Stay connected with us on Facebook, Twitter, and YouTube for more tutorials and updates.

You might also find these articles helpful:

Alternative Installation Methods for Python 3.11 on Rocky Linux 8

While the above method of compiling from source provides granular control and optimization, it can be time-consuming. Here are two alternative methods for installing Python 3.11 on Rocky Linux 8, which are often simpler and faster:

1. Using conda (Miniconda or Anaconda)

Conda is a package, dependency, and environment management system. It’s particularly useful for data science and machine learning projects, but it can also be used to manage different Python versions.

Explanation: Conda allows you to create isolated environments with specific Python versions and packages. This prevents conflicts between different projects that require different versions of Python or its dependencies.

Steps:

  1. Download and Install Miniconda or Anaconda: Download the appropriate installer for Linux from the Anaconda or Miniconda website. Miniconda is the smaller, minimal version that includes only conda and its dependencies. Anaconda includes a larger set of pre-installed packages.

    wget https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh
    bash Miniconda3-latest-Linux-x86_64.sh

    Follow the on-screen prompts during the installation.

  2. Create a Conda Environment: Create a new Conda environment specifying Python 3.11:

    conda create -n py311 python=3.11
  3. Activate the Environment: Activate the newly created environment:

    conda activate py311
  4. Verify Installation: Check the Python version within the environment:

    python --version

    It should output Python 3.11.x.

Code Example (Creating and Activating the Environment):

# Create conda environment named py311 with python 3.11
conda create -n py311 python=3.11

# Activate the py311 conda environment
conda activate py311

# Verify the python version
python --version

2. Using Software Collections (SCL)

Software Collections (SCL) allow you to install multiple versions of software packages, including Python, on the same system without interfering with the system’s default packages.

Explanation: SCL provides a mechanism to install and use newer versions of software without replacing the base system’s components, which is crucial for maintaining system stability.

Steps:

  1. Enable the SCL repository: Rocky Linux 8 requires enabling the SCL repository.

    sudo dnf install centos-release-scl -y
  2. Install the desired Python version from SCL: Unfortunately, Python 3.11 isn’t typically available directly through the standard SCL repository for Rocky Linux 8. However, you can potentially explore alternative SCL repositories or build your own SCL package. This is a more advanced approach and requires familiarity with SCL packaging.

    Because directly installing Python 3.11 via SCL on Rocky Linux 8 might not be straightforward due to repository availability, I’ll demonstrate installing a different, readily available Python version (e.g., Python 3.8) to illustrate the general SCL process. Replace rh-python38 with the appropriate SCL package name if you find one for Python 3.11.

    sudo dnf install rh-python38 -y
  3. Enable the Software Collection: To use the installed Python version, enable the corresponding software collection:

    scl enable rh-python38 bash

    This command starts a new shell session with the SCL environment enabled.

  4. Verify Installation: Check the Python version:

    python --version

    It should output Python 3.8.x (or the version you installed). Remember to replace rh-python38 with the correct SCL package if you manage to find or build one for Python 3.11.

Code Example (Illustrative – Using rh-python38):

# Install Python 3.8 from SCL (Illustrative)
sudo dnf install rh-python38 -y

# Enable the SCL environment
scl enable rh-python38 bash

# Verify Python version
python --version

Important Considerations for SCL:

  • The availability of specific Python versions through SCL depends on the repositories configured on your system.
  • Using SCL requires enabling the specific software collection each time you want to use the installed version.
  • Building your own SCL package is a more advanced task.

While compiling from source provides maximum control, using Conda offers a straightforward way to manage multiple Python versions and dependencies, and SCL can be a viable option if the desired Python version is available within the SCL repositories. The best method depends on your specific requirements and the availability of packages for your system. I have provided a way to Set up Python 3.11 on Rocky Linux 8.

Leave a Reply

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