Install Python 3.11 on Rocky Linux 9 with Easy Steps

Posted on

Install Python 3.11 on Rocky Linux 9 with Easy Steps

Install Python 3.11 on Rocky Linux 9 with Easy Steps

Python has become a cornerstone in modern software development, renowned for its versatility and readability. Its applications span a wide range of fields, from web development and data science to scripting and automation. Unlike markup languages like HTML or CSS, Python is a true programming language, empowering developers to build complex applications and solve intricate problems. This guide will walk you through the process to Install Python 3.11 on Rocky Linux 9, ensuring you have the latest version ready for your projects.

This comprehensive guide provides step-by-step instructions to Install Python 3.11 on Rocky Linux 9. Let’s get started!

Before diving into the installation process, ensure you have the necessary prerequisites in place. You’ll need a Rocky Linux 9 server and a non-root user with sudo privileges. If you haven’t already set up a user with sudo access, you can refer to a guide like the Initial Server Setup with Rocky Linux 9.

Installing Python 3.11 on Rocky Linux 9: Step-by-Step

The following steps will guide you through compiling and installing Python 3.11 from source on your Rocky Linux 9 system.

Step 1: Update Package Index

Begin by updating your local package index to ensure you have the latest package information. This is crucial for resolving dependencies correctly.

sudo dnf update -y

Step 2: Install Required Packages and Dependencies

Python 3.11 relies on several libraries and tools for compilation and execution. Install these dependencies using the following command:

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

This command installs the GNU Compiler Collection (gcc), OpenSSL development headers (openssl-devel), bzip2 development libraries (bzip2-devel), Foreign Function Interface library (libffi-devel), zlib compression library (zlib-devel), wget for downloading files, make for building software, and tar for extracting archives.

Step 3: Download Python 3.11

Visit the Python Downloads page to find the latest stable version of Python 3.11. Once you have identified the correct version, download the source code using wget:

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

Note: Ensure that you replace 3.11.1 with the actual version number if a newer version is available.

Step 4: Extract the Downloaded File

Extract the downloaded tarball using the tar command:

sudo tar -xf Python-3.11.1.tgz

Step 5: Navigate to the Source Directory

Change your current directory to the extracted source directory:

cd Python-3.11.1

Step 6: Run Configuration Script

The configuration script prepares the build environment by checking for dependencies and configuring the build process. Run the script with the --enable-optimizations flag:

./configure --enable-optimizations

The --enable-optimizations option enables Profile Guided Optimization (PGO), which can improve Python’s performance. It is recommended for production environments where performance is critical.

Step 7: Build Python 3.11

Start the build process using the make command. The -j option specifies the number of parallel jobs to use, which can significantly reduce build time.

make -j 2

The -j option should be set to the number of cores in your system. You can determine the number of cores using the nproc command:

nproc

Adjust the -j value accordingly.

Step 8: Install Python 3.11

Install Python 3.11 using the make altinstall command. This command installs Python 3.11 alongside the existing system Python installation, preventing conflicts.

sudo make altinstall

Important: Use make altinstall instead of make install to avoid overwriting the system’s default Python version.

Step 9: Verify the Installation

Verify that Python 3.11 is installed correctly by checking its version:

python3.11 --version

You should see the Python 3.11 version number printed in the output.

Create a Test App with Python 3.11

To further ensure the correct installation and functionality of Python 3.11, create a simple test application.

Step 1: Create Project Directory

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

mkdir ~/test_app && cd ~/test_app

Step 2: Create a Virtual Environment

Create a virtual environment for your project to isolate its dependencies. This is best practice for Python development.

python3.11 -m venv test_app_venv

This command creates a virtual environment named test_app_venv.

Step 3: Activate the Virtual Environment

Activate the virtual environment using the following command:

source test_app_venv/bin/activate

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

Step 4: Install a Package (Example: Apache Airflow)

As an example, install the Apache Airflow package using pip:

pip install apache-airflow

This command installs Apache Airflow and its dependencies within the virtual environment.

Step 5: Deactivate the Virtual Environment

Deactivate the virtual environment when you are finished working on the project:

deactivate

This will return your shell prompt to its normal state.

Alternative Solutions to Install Python 3.11 on Rocky Linux 9

While compiling from source is a reliable method, alternative approaches can simplify the installation process. Here are two different ways to install Python 3.11 on Rocky Linux 9:

1. Using conda package manager:

Anaconda or Miniconda provides a powerful package and environment management system. This method is especially useful if you plan to use Python for data science or machine learning.

  • Explanation: Conda creates isolated environments, preventing conflicts between different Python versions and packages. It also simplifies the installation of binary packages, often providing pre-built versions of libraries that can be difficult to compile from source.

  • Steps:

    1. Install Miniconda: Download the Miniconda installer for Linux from the official Anaconda website.

    2. Run the installer:

      bash Miniconda3-latest-Linux-x86_64.sh

      Follow the on-screen instructions.

    3. Create a Conda environment with Python 3.11:

      conda create -n py311 python=3.11
    4. Activate the environment:

      conda activate py311
    5. Verify the installation:

      python --version

      This should display Python 3.11.

2. Using pyenv version manager:

Pyenv is a tool that allows you to manage multiple Python versions side-by-side. It’s a good option if you need to switch between different Python versions for various projects.

  • Explanation: Pyenv manages different Python versions in a non-intrusive way, allowing you to set a global Python version or a version specific to a directory. It uses shims to intercept Python commands and redirect them to the correct version.

  • Steps:

    1. Install pyenv and its dependencies:

      sudo dnf install -y git
      curl https://pyenv.run | bash
    2. Configure your shell: Add the following lines to your ~/.bashrc or ~/.zshrc file:

      export PYENV_ROOT="$HOME/.pyenv"
      export PATH="$PYENV_ROOT/bin:$PATH"
      eval "$(pyenv init -)"
      eval "$(pyenv virtualenv-init -)"
    3. Reload your shell:

      source ~/.bashrc

      or

      source ~/.zshrc
    4. Install Python 3.11:

      pyenv install 3.11.1

      Replace 3.11.1 with the desired version.

    5. Set the global Python version:

      pyenv global 3.11.1
    6. Verify the installation:

      python --version

      This should display Python 3.11.

Conclusion

You have now successfully learned how to Install Python 3.11 on Rocky Linux 9 using compilation from source and two alternative methods using package and version managers. Whether you choose to compile from source or leverage tools like Conda or Pyenv, you are now equipped to start building and deploying Python applications on your Rocky Linux 9 server. This guide has provided you with a solid foundation to Install Python 3.11 on Rocky Linux 9, so you can now begin your Python journey. Remember to choose the method that best suits your workflow and project requirements. This detailed guide on how to Install Python 3.11 on Rocky Linux 9 will help you in your future projects.

You may also find these articles helpful: