4.2 Build and Install Python for Linux for On-Premises Databases

Instructions for installing Python for Linux for an on-premises oracle database.

The Python installation on the database server must be executed by the Oracle user and not sudo, root, or any other user. However, this is not a requirement for the OML4Py client.

Python 3.12.0 is required to install and use OML4Py.

Build and Install Python on Oracle Linux 7

These steps describe building and installing Python 3.12.0 on Oracle Linux 7.

  1. Python 3.12 requires OpenSSL 3.0.12, which needs to be installed from source on Linux 7. Download the OpenSSSL source tarball.
    wget https://www.openssl.org/source/old/3.0/openssl-3.0.12.tar.gz 
    1. Install the Perl modules perl-IPC-Cmd and perl-Digest-SHA if unavailable.
      1. Run the following commands to check if the modules are already installed on your system:
        rpm -qa perl-IPC-Cmd
        rpm -qa perl-Digest-SHA
        If the commands return output like perl-IPC-Cmd-1.02-1.el7.noarch or perl-Digest-SHA-6.02-1.el7.x86_64, it means the modules are installed.
      2. If no value is returned, the modules are not available on the system and need to be installed as sudo or root. Run the following command to install them::
        sudo yum install perl-IPC-Cmd perl-Digest-SHA
    2. Extract the OpenSSL archive. Create a subdirectory named dist inside openssl directory. Go to the openssl directory.
      tar -xvzf openssl-3.0.12.tar.gz
      cd openssl-3.0.12
      mkdir dist
      export OPENSSL_HOME=<full-path-to-openssl-3.0.12>
    3. Run the following command to configure and make.
      ./Configure enable-fips --prefix=$OPENSSL_HOME/dist/openssl --openssldir=$OPENSSL_HOME/dist/ssl
      make clean; make
      make install
    4. Check if the OpenSSL libraries have been successfully compiled and the openssl folder containing them has been created.
      ls dist/openssl/lib64
      engines-3/ libcrypto.so  libssl.a   libssl.so.3 pkgconfig/ 
      libcrypto.a libcrypto.so.3 libssl.so ossl-modules/
      
      ls dist/ssl
      certs/           ct_log_list.cnf.dist misc/            openssl.cnf.dist
      ct_log_list.cnf  fipsmodule.cnf   openssl.cnf      private/
  2. Download the Python 3.12.0 source tarball.
    wget https://www.python.org/ftp/python/3.12.0/Python-3.12.0.tgz 
    1. Before continuing, check if there’s already a Python installation.
      1. Look for a folder named python in this location: $ORACLE_HOME.
      2. If there’s no python folder found, proceed to step 2.2.
      3. If a folder is found, rename the existing folder to avoid conflicts with the new installation. For example:
        mv $ORACLE_HOME/python $ORACLE_HOME/python-old
    2. Create a directory $ORACLE_HOME/python and extract the contents of the tarball to this directory:
      mkdir -p $ORACLE_HOME/python 
      tar -xvzf Python-3.12.0.tgz --strip-components=1 -C $ORACLE_HOME/python
      The contents of the Gzipped source tarball will be copied and extracted directly to $ORACLE_HOME/python.
    3. Create PYTHONHOME environment variable and change the directory to:
      export PYTHONHOME=$ORACLE_HOME/python 
      cd $PYTHONHOME
    4. Copy the openssl libraries to python folder.
      mkdir $PYTHON_HOME/include $PYTHONHOME/ssl
      cp -r $OPENSSL_HOME/dist/openssl/lib64/* $PYTHONHOME/lib
      cp -r $OPENSSL_HOME/dist/ssl/* $PYTHONHOME/ssl
      cp -r $OPENSSL_HOME/include/openssl $PYTHONHOME/include
    5. Go to the new directory:
      cd $ORACLE_HOME/python
    6. OML4Py requires the presence of the perl-Env, libffi-devel, openssl, openssl-devel, tk-devel, xz-devel, zlib-devel, bzip2-devel, readline-devel, libuuid-devel and ncurses-devel libraries. Install these packages as sudo or root user:
      sudo yum install perl-Env libffi-devel openssl openssl-devel tk-devel xz-devel zlib-devel bzip2-devel readline-devel libuuid-devel ncurses-devel
    7. Compile and install Python.
      ./Configure --enable-shared OPT='-fwrapv -O3 -Wall' --prefix=$PYTHONHOME --with-openssl=$PYTHONHOME --with-openssl-rpath=$PYTHONHOME/lib
      make clean; make
      make altinstall
    8. Create a symbolic link in the $PYTHONHOME/bin directory to the python3.12 executable

      ln -s $PYTHONHOME/bin/python3.12 $PYTHONHOME/bin/python3

      Note:

      Be sure to use the --enable-shared flag if you are going to use Embedded Python Execution; otherwise, using an Embedded Python Execution function results in an extproc error.

      Be sure to invoke make altinstall instead of make install to avoid overwriting the system Python.

    9. Make Python libraries available and add Python3.12 bin folder to the path.
      export LD_LIBRARY_PATH=$PYTHONHOME/lib:$LD_LIBRARY_PATH 
      export PATH=$PYTHONHOME/bin:$PATH
    10. Verify that Python was compiled correctly.
      python3
      Python 3.12.0 (main, Apr  5 2024, 18:58:17) [GCC 4.8.5 20150623 (Red Hat 4.8.5-44.0.3)] on linux Type "help", "copyright", "credits" or "license" for more information.
      import ssl
      ssl.OPENSSL_VERSION
      'OpenSSL 3.0.12 24 Oct 2023'

Build and Install Python on Oracle Linux 8

These steps describe building and installing Python 3.12.0 on Linux 8.

  1. Download the Python 3.12.0 source tarball.
    wget https://www.python.org/ftp/python/3.12.0/Python-3.12.0.tgz
  2. Create a directory $ORACLE_HOME/python and extract the contents to this directory:
    mkdir -p $ORACLE_HOME/python
    tar -xvzf Python-3.12.0.tgz --strip-components=1 -C $ORACLE_HOME/python

    The contents of the Gzipped source tarball will be copied directly to $ORACLE_HOME/python

  3. Go to the new directory:
    cd $ORACLE_HOME/python
  4. OML4Py requires the presence of the perl-Env, libffi-devel, openssl, openssl-devel, tk-devel, xz-devel, zlib-devel, bzip2-devel, readline-devel, libuuid-devel and ncurses-devel libraries. Install these packages as sudo or root user:
    sudo yum install perl-Env libffi-devel openssl openssl-devel tk-devel xz-devel zlib-devel bzip2-devel readline-devel libuuid-devel ncurses-devel
  5. To build Python 3.12.0, enter the following commands, where PREFIX is the directory in which you installed Python-3.12.0. The command on the Oracle Machine Learning for Python server will be:
    cd $ORACLE_HOME/python
    ./Configure --enable-shared --prefix=$ORACLE_HOME/python
    
    make clean; make
    make altinstall

    Note:

    Be sure to use the --enable-shared flag if you are going to use Embedded Python Execution; otherwise, using an Embedded Python Execution function results in an extproc error.

    Be sure to invoke make altinstall instead of make install to avoid overwriting the system Python.

  6. Set environment variable PYTHONHOME and add it to your PATH, and set environment variable LD_LIBRARY_PATH:
    export PYTHONHOME=$ORACLE_HOME/python
    export PATH=$PYTHONHOME/bin:$PATH
    export LD_LIBRARY_PATH=$PYTHONHOME/lib:$LD_LIBRARY_PATH

    Note:

    To use Python for OML4Py, the variables must be set, and these variables must appear before the system Python in PATH and LD_LIBRARY_PATH.
  7. Create a symbolic link in your $ORACLE_HOME/python/bin directory to link to your python3.12 executable, which you can do with the following commands:
    cd $ORACLE_HOME/python/bin
    ln -s python3.12 python3

    pip will return warnings during package installation if the latest version is not installed. You can upgrade the version of pip to avoid these warnings:

    python3 -m pip install --upgrade pip

For both Oracle Linux 7 and Oracle Linux 8

You can now start Python by running the command python3. To verify the directory where Python is installed, use the sys.executable command from the sys package. For example:

python3
Python 3.12.0 (default, Feb 22 2022, 15:13:36)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-44.0.3)] on linux
Type "help", "copyright", "credits" or "license" for more information.

import sys
print(sys.executable)
/u01/app/oracle/product/19.3/dbhome_1/python/bin/python3

This returns the absolute path of the Python executable binary.

If you run the command python3 and you get the error command not found, then that means the system cannot find an executable named python3 in $PYTHONHOME/bin. A symlink is required for the OML4Py server installation components. So, in that case, you need to create a symbolic link in your PREFIX/bin directory to link to your python3.12 executable as described in Step 6.