Introduction
I’ve developed a Bash script over two days, which greatly simplifies setting up Oracle Instant Client and OCI8 for Laravel projects using PHP 8.2 on Ubuntu. This script is a real-time-saver, making it much easier for PHP developers to get their environment ready without getting bored with complex setup details. It’s all about letting developers focus on building their applications more efficiently.
Prerequisites
- Ubuntu-based system
- PHP 8.2 installed
- Administrative privileges (sudo access)
The Script: A Hassle-Free Approach
#!/bin/bash
# Exit immediately if a command exits with a non-zero status.
set -e
# Define the Oracle Instant Client version and directory
ORACLE_VERSION="12.2.0.1.0"
ORACLE_DIR="/opt/oracle/instantclient"
# Function to download and verify a file
download_and_verify() {
local url=$1
local file=$2
echo "Downloading $file..."
wget --no-check-certificate -c --header "Cookie: oraclelicense=accept-securebackup-cookie" "$url"
if [ ! -f "$file" ]; then
echo "Error: Failed to download $file"
exit 1
fi
}
# Remove existing files if they exist
[ -f "instantclient-basic-linux.x64-$ORACLE_VERSION.zip" ] && rm "instantclient-basic-linux.x64-$ORACLE_VERSION.zip"
[ -f "instantclient-sdk-linux.x64-$ORACLE_VERSION.zip" ] && rm "instantclient-sdk-linux.x64-$ORACLE_VERSION.zip"
# Download Oracle Instant Client basic package
download_and_verify "https://download.oracle.com/otn/linux/instantclient/122010/instantclient-basic-linux.x64-$ORACLE_VERSION.zip" "instantclient-basic-linux.x64-$ORACLE_VERSION.zip"
# Download Oracle Instant Client SDK package
download_and_verify "https://download.oracle.com/otn/linux/instantclient/122010/instantclient-sdk-linux.x64-$ORACLE_VERSION.zip" "instantclient-sdk-linux.x64-$ORACLE_VERSION.zip"
# Unzip the downloaded files
echo "Unzipping downloaded files..."
unzip instantclient-basic-linux.x64-$ORACLE_VERSION.zip
unzip instantclient-sdk-linux.x64-$ORACLE_VERSION.zip
# Create Oracle directory
echo "Creating Oracle directory..."
sudo mkdir -p $ORACLE_DIR
# Move Instant Client to the Oracle directory
echo "Moving Instant Client to the Oracle directory..."
sudo mv instantclient_12_2/* $ORACLE_DIR
sudo rmdir instantclient_12_2
# Install necessary packages
echo "Installing necessary packages..."
sudo apt-get update
sudo apt install php-pear libaio1 build-essential php8.2-dev expect
# Create symbolic links
echo "Creating symbolic links..."
sudo ln -sf $ORACLE_DIR/libclntsh.so.12.1 $ORACLE_DIR/libclntsh.so
sudo ln -sf $ORACLE_DIR/libocci.so.12.1 $ORACLE_DIR/libocci.so
# Configure dynamic linker run-time bindings
echo "Configuring dynamic linker run-time bindings..."
echo $ORACLE_DIR | sudo tee /etc/ld.so.conf.d/oracle-instantclient.conf
sudo ldconfig
# Install OCI8 for PHP 8.2
echo "Installing OCI8 extension for PHP 8.2..."
/usr/bin/expect <<EOF
set timeout 60
spawn sudo pecl install oci8
expect "Please provide the path to the ORACLE_HOME directory"
send "instantclient,/opt/oracle/instantclient\r"
expect eof
EOF
echo "Oracle Instant Client and OCI8 installation completed."
Step-by-Step Execution
Here’s a step-by-step guide to executing the provided Bash script for setting up the Oracle Instant Client and OCI8 extension for PHP 8.2 on Ubuntu:
Step 1: Save the Script
- Create a new file on your Ubuntu system using a text editor.
- Copy the Bash script you have provided into this file.
- Save the file with a suitable name, such as
setup-oci8.sh
.
Step 2: Make the Script Executable
- Open a terminal.
- Navigate to the directory where you saved
setup-oci8.sh
. - Run the command
chmod +x setup-oci8.sh
to make the script executable.
Step 3: Run the Script
- In the terminal, execute the script by typing
./setup-oci8.sh
. - The script will start executing the steps automatically.
Post-Execution Steps
- After running the script, the Oracle Instant Client and OCI8 extension should be set up for PHP 8.2.
- You might need to adjust your PHP configuration to enable the OCI8 extension, by adding
extension=oci8.so
in yourphp.ini
file. - Restart any services that use PHP to ensure they recognize the new extension.
- execute the following command to verify the installation
php -m | grep oci8
Breakdown of the Script Execution Process
- Setting up Environment and Variables:
- Defines the Oracle Instant Client version and the directory where it will be installed.
- Downloading Oracle Instant Client:
- Downloads the Oracle Instant Client basic package and SDK from Oracle’s website.
- Unzipping and Moving Files:
- Unzip the downloaded files and move them to the specified directory (
/opt/oracle/instantclient
).
- Installing Required Packages:
- Updates package lists and installs necessary packages including
php-pear
,libaio1
,build-essential
,php8.2-dev
, andexpect
.
- Creating Symbolic Links:
- Creates symbolic links for the Oracle Instant Client libraries.
- Configuring Dynamic Linker:
- Updates the dynamic linker configuration to include the Oracle Instant Client libraries.
- Installing OCI8 Extension:
- Uses
expect
to automate the installation of the OCI8 extension for PHP 8.2, including providing the path to the Oracle Instant Client when prompted during the installation process.
- Completion Message:
- Displays a completion message indicating that the Oracle Instant Client and OCI8 installation is finished.
This script automates the manual process of setting up the Oracle Instant Client and OCI8, making it convenient for PHP developers working with Oracle databases.