Offline installation of cx_oracle for Python 3.x

Introduction

cx_Oracle for python is needed for connecting Oracle databases like 11g,12c,18c and 19c. This also works with a higher version of Python like 2.7 or 3.5 and higher. Older versions of cx_Oracle may work with older versions of Python. It requires Oracle Client libraries to be installed, we will be following below guide to install with dependencies and few of packages.

Once the cx_oracle in place it will allow to connect with Oracle database instance.

Python cx_oracle module
Python cx_oracle module

Online Installation

If you have internet connection its super easy to install the module cx_oracle for python.

# python -m pip install cx_Oracle --upgrade

Else, if you have pip package management installer it can be accomplished by running.

# pip install cx-Oracle

This requires an internet connection. If your case is similar to mine which needs to be installed in an isolated environment continue reading.

Downloading for Offline Installation

To perform an offline installation, first of all, we need to get all the required installation RPM. Those can be downloaded from Oracle developer repo. As per our requirement, we are downloading with cx_oracle for 12c.

Let’s download Oracle Instant client (Instant Client Package (ZIP)) from below URL. To download the instant client we need to authenticate with oracle credentials.

https://www.oracle.com/ae/database/technologies/instant-client/linux-x86-64-downloads.html

Local Installation

Install the downloaded RPM using yum command. If you are keen to read about yum command follow this.

# yum localinstall cx_Oracle-12c-py27-5.3-1.el7.x86_64.rpm oracle-instantclient12.2-basic-12.2.0.1.0-1.x86_64.rpm

Once installed the cx_oracle package let prepare to install the python module.

Offline installation of Python module

Download all the python modules from below URL. It contains many versions of modules for Linux and Windows.

https://pypi.org/project/cx-Oracle/#files

Download all wheel files required for Linux under anyone of the directory. And copy to the destination servers.

If you are lazy to “SCP” or using a “for” loop better use with below ansible copy module.

# ansible -b -m copy -a "src=/home/ansible/python_modules dest=/home/ansible owner=ansible group=ansible mode=0644" my_group

Either use your playbook as follows.

- name: Copy file with owner and permissions
  copy:
    src: /home/ansible/python_modules
    dest: /home/ansible 
    owner: ansible
    group: ansible
    mode: '0644' 

Files are copied under remote servers.

root:prod-srv-1 ~ $ ls -lthr
 total 4.3M
 -rw-r--r--. 1 root root 712K Jan 27 15:43 cx_Oracle-7.3.0-cp27-cp27m-manylinux1_x86_64.whl
 -rw-r--r--. 1 root root 712K Jan 27 15:43 cx_Oracle-7.3.0-cp27-cp27mu-manylinux1_x86_64.whl
 -rw-r--r--. 1 root root 720K Jan 27 15:43 cx_Oracle-7.3.0-cp35-cp35m-manylinux1_x86_64.whl
 -rw-r--r--. 1 root root 720K Jan 27 15:43 cx_Oracle-7.3.0-cp36-cp36m-manylinux1_x86_64.whl
 -rw-r--r--. 1 root root 725K Jan 27 15:43 cx_Oracle-7.3.0-cp37-cp37m-manylinux1_x86_64.whl
 -rw-r--r--. 1 root root 742K Jan 27 15:43 cx_Oracle-7.3.0-cp38-cp38-manylinux1_x86_64.whl
root:prod-srv-1  ~ $

Install the python modules from local directory

$ python3.6 -m pip install cx_Oracle --no-index --find-links file:///root/pymodules
root:prod-srv-1  ~ $ python3.6 -m pip install cx_Oracle --no-index --find-links file:///root/pymodules
 Collecting cx_Oracle
 Installing collected packages: cx-Oracle
 Successfully installed cx-Oracle-7.3.0
root:prod-srv-1  ~ $

List and verify the cx_oracle for Python

We can list and verify the installed version. While listing there are two options available those are legacy or columns. These options can be used to print the output in a decent layout.

$ pip3 list installed --format=columns

Output for reference

root:prod-srv-1  ~ $ pip3 list installed --format=columns
 Package    Version
 cx-Oracle  7.3.0
 pip        9.0.3
 setuptools 39.2.0
root:prod-srv-1  ~ $

That’s it we have completed with installing cx_Orace for python version 2.7 or higher. If you found any bug or interested to contribute look from GitHub repo.

Conclusion

For future self-reference above steps are documented, Hope someone can come across with the same requirement to install this module in an isolated production environment. Subscribe to our newsletter and stay tuned for more real-time experienced guides.