Overview

When tasked with an R-based project, you might find yourself wanting to connect to an Oracle database. ROracle is one library you can use. This post is a guide on installing the library on CentOS 7.

Oracle Instant Client

First thing we need to do is install the right dependencies.

Install the yum repo and gpg key for Oracle Instant Client:

export ORACLE_INSTANT_CLIENT_VERSION=18.3
export ORACLE_YUM_URL=https://yum.oracle.com 
export ORACLE_HOME=/usr/lib/oracle/${ORACLE_INSTANT_CLIENT_VERSION}/client64
export ORACLE_YUM_REPO=public-yum-ol7.repo 
export ORACLE_YUM_GPG_KEY=RPM-GPG-KEY-oracle-ol7 

rpm --import ${ORACLE_YUM_URL}/${ORACLE_YUM_GPG_KEY};
curl -o /etc/yum.repos.d/${ORACLE_YUM_REPO} ${ORACLE_YUM_URL}/${ORACLE_YUM_REPO};
sed -i 's/enabled=1/enabled=0/g' /etc/yum.repos.d/${ORACLE_YUM_REPO}; 
yum-config-manager --enable ol7_oracle_instantclient;

This allows yum to see the Instant Client packages. We install those next:

ACCEPT_EULA=Y sudo yum install -y \
  libaio-devel \
  oracle-instantclient18.3-basic \
  oracle-instantclient18.3-sqlplus \
  oracle-instantclient18.3-tools \
  oracle-instantclient18.3-devel; 

libaio-devel is also a required package, per the documentation.

ROracle

Next, set the necessary environment variables for installing ROracle:

export OCI_LIB=${ORACLE_HOME}/lib 
export OCI_INC=/usr/include/oracle/${ORACLE_INSTANT_CLIENT_VERSION}/client64 

OCI_LIB points to a folder where the shared libraries will be. These shared libraries are dynamically linked shared object libraries that the installation program will need in order to run. OCI_INC points to a folder containing header files; these are collections of functions that other C programs can use through the include operator, e.g. include oci.h.

The installation program will need to know where to look when it attempts to import the shared libraries. To that end, we’ll leverage ldconfig:

echo "/usr/lib/oracle/18.3/client64/lib" | sudo tee /etc/ld.so.conf.d/oracle.conf
sudo ldconfig

Per the documentation, ldconfig creates the necessary links and cache to the libraries found in the *.conf files. So we create an oracle.conf file that tells ldconfig to create links for shared libraries in /usr/lib/oracle/18.3/client64/lib, a.k.a. the OCI_LIB folder.

Finally, we install the ROracle package:

R -e "install.packages('ROracle')"

Example

I also have a docker-based example here. It basically does everything enumerated in this guide, with the exception that the project uses packrat for package management, and that the ROracle package is already identified in the snapshot file. It still has to re-install the ROracle (and other) packages, since the lib folder (where the packages are installed) isn’t checked into version control.