How to Fix “Unable to Load memcache.so” Extension in PHP: Complete Troubleshooting Guide


4 views

When compiling PHP from source (specifically version 5.2.8 in this case), developers often encounter the frustrating situation where the memcache extension fails to load despite following standard installation procedures. The error typically manifests in one of these ways:


PHP Warning:  PHP Startup: Unable to load dynamic library '/usr/local/lib/php/extensions/no-debug-non-zts-20060613/memcache.so'
or simply not appearing in php -m output

First, let's confirm whether the extension was actually installed correctly. Run:


ls -la /usr/local/lib/php/extensions/no-debug-non-zts-20060613/

You should see memcache.so with proper permissions (typically -rwxr-xr-x). If it's missing, the installation failed.

1. PHP Version Mismatch

Memcache 2.2.5 might not be compatible with PHP 5.2.8. Try:


pecl install memcache-2.2.7

2. Incorrect Extension Directory

Check your extension_dir:


php -i | grep extension_dir

Ensure your php.ini points to the correct path:


extension_dir = "/usr/local/lib/php/extensions/no-debug-non-zts-20060613/"
extension=memcache.so

3. Dependency Issues

Memcache requires zlib. Verify it's installed:


ldd /usr/local/lib/php/extensions/no-debug-non-zts-20060613/memcache.so

Missing dependencies will appear as "not found".

Checking Apache Error Logs


tail -f /usr/local/httpd/logs/error_log

Look for specific loading errors when restarting Apache.

Alternative Installation Method

For CentOS/RHEL systems, consider:


yum install php-pecl-memcache

Or for Debian/Ubuntu:


apt-get install php5-memcache

Once installed, verify functionality with:


connect('localhost', 11211) or die ("Could not connect");
$version = $memcache->getVersion();
echo "Server's version: ".$version."
"; $memcache->set('test_key', 'Hello World', 0, 300); $get_result = $memcache->get('test_key'); echo "Data from the cache: ".$get_result; ?>

After making changes, always:


service httpd restart
php -m | grep memcache

When compiling PHP from source (like version 5.2.8 in this case), extension loading problems often stem from path mismatches or configuration inconsistencies. The memcache extension requires three critical components to work:

1. Proper compilation with matching PHP version
2. Correct extension_dir path in php.ini
3. Appropriate file permissions

1. Validate Your PHP Installation

First, check your PHP configuration:

php -i | grep "Configure Command"
php --ini

2. Confirm Extension Directory

The key to solving loading issues is verifying the exact extension path:

php -i | grep extension_dir
// Alternative for older PHP:
php -r "echo ini_get('extension_dir');"

3. Manual Installation of memcache

For most Linux systems, here's the proper compilation sequence:

wget https://pecl.php.net/get/memcache-3.0.8.tgz
tar -zxvf memcache-*.tgz
cd memcache-*
phpize
./configure --with-php-config=/path/to/php-config
make
sudo make install

php.ini Settings That Matter

Instead of just adding the extension line, ensure these directives are properly set:

; Set the correct extension directory
extension_dir = "/usr/local/lib/php/extensions/no-debug-non-zts-20060613/"

; Load memcache (use full path if needed)
extension=memcache.so

Checking Apache Error Logs

The most revealing information often comes from Apache's error log:

tail -f /var/log/apache2/error.log
# Or for custom installations:
tail -f /usr/local/httpd/logs/error_log

Common Error Patterns

Look for these specific errors in logs:

1. "Unable to load dynamic library" - path issue
2. "undefined symbol" - version mismatch
3. "permission denied" - SELinux or file permissions

For those using package managers:

# Debian/Ubuntu:
sudo apt-get install php-memcache

# RHEL/CentOS:
sudo yum install php-pecl-memcache

After making changes, always verify:

php -m | grep memcache
php -r "var_dump(extension_loaded('memcache'));"