Do it your self hobby code

How to Install the PHP Memcache and Memcached Extension

If you’re using WordPress and W3 Total Cache plugin, you may find memcached and memcache are not available. This tutorial will help you install PHP extension memcached and memcache on Ubuntu server. Here’s how:

How to install Memcache on Ubuntu server?

Go to your terminal and enter this command:

sudo apt-get update
sudo apt-get install php-memcache

Restart your apache2 service:

sudo service apache2 restart

How to install Memcached on Ubuntu server?

Go to your terminal and enter this command:

sudo apt-get update
sudo apt-get install memcached
sudo apt-get install php-memcached

Restart your apache2 service:

sudo service apache2 restart
memcached extension w3 total cache plugin wordpress

Install Memcache and Memcached Php Extension Manually

You need to install Memcache manually if you are using multiple PHP versions and want to install memcache to specific Php version.

I’m using 3 php versions:

ls /usr/bin/php*
 #/usr/bin/php7.2
 #/usr/bin/php7.4
 #/usr/bin/php8.0

Check what Php my system is using. There’s couples of way:

php -i | grep 'Configuration File'
 #Configuration File (php.ini) Path => /etc/php/7.4/cli
 #Loaded Configuration File => /etc/php/7.4/cli/php.ini

I’m using Php7.4. You can also check by writing a php file.

echo “<?php phpinfo();?>” > /var/www/html/phpinfo.php


You can now see your php info using your browser. Enter this address with the ip of your server:

http://[ip address]/phpinfo.php

Clearly, I’m using Php7.4 even though I have php8.0 installed

php -v
PHP 8.0.12 (cli) (built: Oct 22 2021 12:34:00) ( NTS )

INSTALL MEMCACHE MANUALLY

We can find and download the package here: https://pecl.php.net/package/memcache

cd /tmp
wget https://pecl.php.net/get/memcache-8.0.tgz
tar xf memcache-8.0.tgz
cd memcache-8.0

phpize

./configure --with-php-config=/usr/bin/php-config7.4
sudo make
sudo make install
 #Installing shared extensions:     /usr/lib/php/20190902/

Create ini file to enable the extension

cd /etc/php/7.4/mods-available
sudo nano memcache.ini

Paste this and save:

extension=memcache.so

Enable the extension

sudo phpenmod memcache

Restart Apache service:

sudo service apache2 restart

Check if it is installed correctly

phpquery -v 7.4 -s apache2 -m memcache
 #memcache (Enabled for apache2 by maintainer script)

INSTALL MEMCACHED MANUALLY

We can find and download the package here: https://pecl.php.net/package/memcached

cd /tmp
wget https://pecl.php.net/get/memcached-3.1.5.tgz
tar xf memcached-3.1.5.tgz
cd memcached-3.1.5

phpize

./configure --with-php-config=/usr/bin/php-config7.4
sudo make
sudo make install
 #Installing shared extensions:     /usr/lib/php/20190902/

Create ini file to enable the extension

cd /etc/php/7.4/mods-available
sudo nano memcached.ini

Paste this and save:

extension=memcached.so

Enable the extension

sudo phpenmod memcached

Restart Apache service:

sudo service apache2 restart

Check if it is installed correctly

phpquery -v 7.4 -s apache2 -m memcached
 #memcache (Enabled for apache2 by maintainer script)

Leave a Comment

Your email address will not be published. Required fields are marked *