How to fix: cx_Oracle.DatabaseError: DPI-1047: Cannot locate a 64-bit Oracle Client library - Python

After some more research i got the solution from Ubuntu community , after you have installed oracle instant-client you will have to integrate oracle libraries as follows:

export LD_LIBRARY_PATH=/usr/lib/oracle/<version>/client(64)/lib/${LD_LIBRARY_PATH:+:$LD_LIBRARY_PATH}

An example for 12.1 version for Linux x86_64 can be:

export LD_LIBRARY_PATH=/usr/lib/oracle/12.1/client64/lib/${LD_LIBRARY_PATH:+:$LD_LIBRARY_PATH}

where <version> indicates the version of your of your oracle instant-client e.g 11.2, 12.2
The connection parameter should be as follows

connection = cx_Oracle.connect("username/password@host/service_name e.g orcl")

to get the listener/service_name type the following in the oracle sqlplus

SQL> show parameter local_listener

literal under VALUE is your listener/service_name.


For Ubuntu Linux 20.04 LTS server what worked for me (which may be obvious but wasn't to me!) is 1) when you perform the export, you need to be in the folder you intend to run the app/ command connecting to Oracle from, and although this worked, after closing the SSH terminal to the EC2 server, was then not available again which was resolved by 2) Add it to ~/.bashrc Steps in full:

With the Oracle instant client unzipped in for example: /opt/oracle/instantclient_19_9

sudo apt-get install libaio1
cd ~/your-project-folder
export LD_LIBRARY_PATH=/opt/oracle/instantclient_19_9

I then added to ~/.bashrc with:

sudo nano ~/.bashrc

And add this line:

export LD_LIBRARY_PATH=/opt/oracle/instantclient_19_9

And in the terminal run:

source ~/.bashrc

Mine worked as expected installed on an EC2 server under 'ubuntu' user with requisite nvm/ nodeJs installed

In nodeJs an example connection might look something like:

const testOracleConnection = async () => {
    let conn;

    try {
        conn = await oracledb.getConnection(oracleConfig);

        const query1 = 'select ID, anotherColumn from someTable where ID = 1111';

        const result = await conn.execute(query1);

        console.log(result);
    } catch (err) {
        console.error(err);
    } finally {
        if (conn) {
            try {
                await conn.close();
            } catch (err) {
                console.error(err);
            }
        }
    }
};

I was facing the exact same problem. This is what worked for me:

  • First, I downloaded the Oracle Basic zip file. In my case, I got the 64-bit version.
  • After that, I unzipped it in an opt directory. I had to use sudo in my system
    $ sudo mkdir -p /opt/oracle  

    $ cd /opt/oracle  

    $ sudo unzip /opt/oracle/instantclient-basic-linux.x64-19.8.0.0.0dbru.zip  
  • Then I installed libaio1. Note that I am working with Ubuntu
    $ sudo apt-get install libaio1
  • Finally, I added the path to the external variable LD_LIBRARY_PATH
    $ vim ~/.bashrc  
  • And added this line to the .bashrc file
    export LD_LIBRARY_PATH=/opt/oracle/instantclient_19_8:$LD_LIBRARY_PATH  
  • After saving the .bashrc file, I sourced it:
    $ source ~/.bashrc

Then my Python script worked nicely again.

See also the cx_oracle documentation