Search code examples
phpmemcachedphp-memcached

How to install php-memcache in xampp ubuntu?


I have installed xampp for linux from https://www.apachefriends.org/index.html in my Ubuntu 22.04 OS

After successfully installed, my project is running fine but when I install the php-memchached in my system with the below command but it is not showing in my phpinfo() page.

Command:

sudo apt-get install -y php-memcached

Also I tried with the code to check my Memcached Successfully installed or not. But I got the error message that Class 'Memcached' not found

Code:

$memcached = new Memcached();
$memcached->addServer("127.0.0.1", 8080);
$response = $memcached->get("sample_key");

if($response==true) {
    echo $response;
} else {
    echo "Cache is empty";
    $memcached->set("sample_key", "Sample data from cache") ;
}

Error Output in Yii console:

Class 'Memcached' not found

Please let me know how to install the php-memcached in Xappp for my ubuntu system?


Solution

  • php-memcached extension uses the libmemcached library to provide an API for communicating with memcached servers.

    So, This extension requires libmemcached client library.

    The steps. Tried successfully from a basic ubuntu:22.04 image:

    FROM ubuntu:22.04
    
    RUN apt-get update && apt-get upgrade -y && apt-get install -y net-tools sudo wget nano
    

    And xampp install:

    cd /home
    sudo wget https://downloadsapachefriends.global.ssl.fastly.net/8.1.6/xampp-linux-x64-8.1.6-0-installer.run
    sudo chmod 755 xampp-linux-x64-8.1.6-0-installer.run
    sudo ./xampp-linux-x64-8.1.6-0-installer.run
    

    Then, there are a lot of dependencies to install:

    sudo apt-get update
    sudo apt-get install -y memcached
    sudo apt-get install php-pear
    sudo apt-get install libcurl3-openssl-dev
    sudo apt-get install php-dev
    sudo apt-get install zlib1g-dev
    sudo apt-get install libmemcached-dev
    sudo pecl install memcached #note the path to memcached.so
    sudo nano /opt/lampp/etc/php.ini
    add extension=/usr/lib/php/20210902/memcached.so #path noted before
    sudo /opt/lampp/lampp restart
    

    Or another possible install:

    sudo apt-get update
    sudo apt-get install autoconf
    sudo apt-get install build-essential
    sudo apt-get install -y pkg-config
    sudo apt-get install zlib1g-dev
    sudo apt-get install libmemcached-dev
    sudo /opt/lampp/bin/pecl install memcached #note the path to memcached.so
    sudo nano /opt/lampp/etc/php.ini
    add extension=/opt/lampp/lib/php/extensions/no-debug-non-zts-20210902/memcached.so #path noted before
    sudo /opt/lampp/lampp restart
    

    With your code:

    $memcached = new Memcached();
    $memcached->addServer("127.0.0.1", 8080);
    $response = $memcached->get("sample_key");
    
    if($response==true) {
        echo $response;
    } else {
        echo "Cache is empty";
        $memcached->set("sample_key", "Sample data from cache") ;
    }
    

    Results:

    Cache is empty
    

    And phpinfo: enter image description here

    Reference for more details:

    https://www.php.net/manual/en/book.memcached.php