askvity

How to Install Python Custom?

Published in Python Installation 4 mins read

Installing Python with custom options gives you more control over the installation process and allows you to tailor it to your specific needs. Here's how to do it:

Step 1: Download the Python Source Code

First, you'll need to download the source code for the Python version you want to install. This is typically available from the official Python website.

  1. Go to Python's official download page.
  2. Download the Gzipped source tarball or XZ compressed source tarball for your desired Python version.

Step 2: Prepare Your System for Building Python

Before building Python from source, you need to ensure your system has the necessary build tools and dependencies. The specific requirements vary depending on your operating system.

Linux (Debian/Ubuntu):

sudo apt update
sudo apt install build-essential zlib1g-dev libncurses5-dev libgdbm-dev libnss3-dev libssl-dev libreadline-dev libffi-dev

Linux (Fedora/CentOS/RHEL):

sudo dnf install gcc zlib-devel ncurses-devel gdbm-devel openssl-devel readline-devel libffi-devel

macOS:

  • Install Xcode Command Line Tools:
xcode-select --install
  • You may also need to install brew if you don't have it: /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
  • Then install required packages via brew: brew install openssl readline

Step 3: Build and Install Python on Your System

Now that your system is ready, you can build and install Python. This process involves extracting the source code, configuring the build, compiling the code, and installing Python to a custom location.

  1. Extract the source code:
tar -xf Python-x.y.z.tgz  # Replace x.y.z with the version you downloaded
cd Python-x.y.z
  1. Configure the build: The configure script prepares the build environment. The --prefix option specifies the installation directory. This is where your custom installation will reside.
./configure --enable-optimizations --prefix=/opt/python-x.y.z # Replace x.y.z
*   `--enable-optimizations`: Enables Profile Guided Optimization (PGO) and Link Time Optimization (LTO) for better performance. This can significantly improve Python's execution speed.  Building with optimizations takes considerably longer.
*   `--prefix=/opt/python-x.y.z`:  Sets the installation directory.  It's recommended to install in `/opt/` or your home directory to avoid conflicts with the system Python. Choose a directory based on the python version for clarity.
  1. Build Python:
make -j $(nproc)
*   `make -j $(nproc)`:  Compiles the Python source code.  The `-j $(nproc)` option uses multiple cores for faster compilation. Replace `$(nproc)` with the number of cores you want to use.
  1. Install Python:
sudo make altinstall
*   `sudo make altinstall`: Installs Python. Using `altinstall` prevents overwriting the system Python installation.  It creates executables like `python3.x` and `pip3.x`.

Step 4: Verify Your Python Installation

After installation, verify that Python is installed correctly and accessible.

  1. Check the Python version:
/opt/python-x.y.z/bin/python3.x --version # Replace x.y.z
  1. Add Python to your PATH (Optional but Recommended): To easily use the custom Python installation, add its directory to your PATH environment variable.

    • Edit your .bashrc, .zshrc, or similar shell configuration file:
    nano ~/.bashrc  # Or the appropriate file for your shell
    • Add the following line to the end of the file:
    export PATH="/opt/python-x.y.z/bin:$PATH" # Replace x.y.z
    • Save the file and reload your shell:
    source ~/.bashrc  # Or the appropriate file for your shell
  2. Verify PATH Configuration: After modifying your path, confirm that your custom python version is being used:

python3.x --version #replace x.y.z
pip3.x --version #replace x.y.z

By following these steps, you can successfully install Python with custom options, tailored to your specific environment and needs.

Related Articles