How to Install and Use Memcached in Laravel?

By Hardik Savani November 5, 2023 Category : Laravel

Hi Artisan,

In this tute, we will discuss how to use memcached in laravel. i would like to share with you laravel cache memcached. it's simple example of laravel cache driver memcached. We will look at example of laravel use memcached. Follow bellow tutorial step of install memcached php laravel.

Laravel provide several driver for cache your app with database query, view etc. here i will give you step by step instruction how to install and use memcached as cache driver in laravel application. you can easily use memcached with laravel 6, laravel 7, laravel 8, laravel 9 and laravel 10 version.

let's see step by step configuration memcached driver.

Install Memcached in Server:

in this step we need to install memcached in ubuntu server and php extension for it. so let's run both command:

sudo apt-get install memcached

next you need to install php extension for memcached. it's version specify so it will install with your php version like "sudo apt-get install php7.3-memcached", sudo apt-get install php7.4-memcached etc. but for default you can use as like bellow command:

sudo apt-get install php-memcached

Configure Memcached Driver:

now, we need to configure cache driver as memcached on env file as like bellow:



you can see bellow cache config file it will as bellow:



use Illuminate\Support\Str;

return [


'stores' => [


'memcached' => [

'driver' => 'memcached',

'persistent_id' => env('MEMCACHED_PERSISTENT_ID'),

'sasl' => [




'options' => [

/* Memcached::OPT_CONNECT_TIMEOUT => 2000, */


'servers' => [


'host' => env('MEMCACHED_HOST', ''),

'port' => env('MEMCACHED_PORT', 11211),

'weight' => 100,







Use Memcached Driver:

now, we will create simple example route to cache our database records. let's see bellow code:


Route::get('cache-data', function () {

$user = \Cache::remember('user', 60, function() {

return \App\Models\User::first();



Check Memcached Cache:

in this step, we will check memcached driver cache properly or not. you need to run bellow command and get all keys of cached:

php -r '$c = new Memcached(); $c->addServer("localhost", 11211); var_dump( $c->getAllKeys() );'

now you can use key and get that content as like bellow:

telnet 11211

get laravel_cache:user

now it will works great.

i hope it can help you...

Tags :