Ad · Host your site on AWS, Google Cloud, DigitalOcean, Linode, or Vultr without headaches! Start a free trial on Cloudways & get $15 on sign up!

How to Install PhpRedis for Laravel on Ubuntu

Caching is very important, so I have to learn how to work with it in Laravel as part of my practice.

Since I’m always trying to use best practices and tools whenever possible, I’m going to use Redis.

If you want to use Redis in Laravel, you have two options:

  1. Install Predis;
  2. Install PhpRedis;

Predis is easier to install, but if you do some research, you’ll find out that it’s slower than PhpRedis, especially when there’s big traffic coming on.

And that’s something to expect, since PhpRedis is a client written in C, while Predis is written in PHP.

PhpRedis is a bit trickier to set up, and since once again I barely could find a solution on Google, I decided to write a step-by-step guide for you to make things easier.

Install PhpRedis for Laravel on Ubuntu

Note that I’m using the recommended Homestead environment for Laravel, hence the guide is for Ubuntu. So, make sure you have Homestead installed properly, with SSH access and everything.

Step 1

After starting Vagrant from the Homestead directory using vagrant up, type vagrant ssh to access Ubuntu.

vagrant ssh laravel ubuntu

Step 2



Note that the link may change if the username or the name of the repository is changed.

So, check it on GitHub first.

Step 3

Type ls to list the directory content.

Something like code should appear. That’s the file you need to unzip.

Step 4



The unzipping feature should already be installed on Ubuntu.

If it isn’t, it will tell you, and it will also tell you what command to use to install the unzipping feature.

Normally, it’s sudo apt-get install unzip.

So, if you don’t have the unzipping feature, install it first, then use the previous command I mentioned to unzip the file.

Step 5

Type ls again.

Step 6


sudo mv phpredis-master/ /etc/

mv stands for “move”. So, this command will move phpredis-master/ in the /etc/ folder.

Nothing new will appear after hitting Enter, so don’t worry if you don’t see any confirmation message or something like that.

Step 7


cd /etc/phpredis-master/

This will change the directory (cd = change directory) and access the phpredis-master folder.

So, now you’ll execute code there. It will no longer be:

[email protected]:~$

It will be:

[email protected]:/etc/phpredis-master$

Step 8



Step 9



Step 10


make && make install

Step 11

For this command, first, you need to know the PHP version that you’ll be using and if it’s installed on Ubuntu.

If you already know this, then go ahead and type this command with the correct PHP version.

sudo vim /etc/php/7.3/fpm/conf.d/redis.ini

This will open the redis.ini file with the Vim editor, where you need to add a line that helps PHP to find Redis.

If you don’t know what PHP versions you have available

Find out what versions of PHP you have on Ubuntu by checking them out in the /etc/php/ folder, like this:

cd /etc/php/

Changing the directory.


Listing its content.

php versions ubuntu

As you can see, I have versions from 5.6 to 7.3.

After that, change the directory back to phpredis-master and then type the code I initially mentioned at the beginning of the step.

This is how you get back to the previous directory:

cd /etc/phpredis-master/

Step 12

When Vim opens the file, press I on your keyboard to enable Insert. You’ll see it in the bottom-left of the screen.

Then, add this line:

vim insert

To save the file and exit:

  • Press Esc to stop the editing mode.
  • Type :x, which will appear at the bottom-left, where Insert was. :x will both save the file and exit the Vim editor.
  • Press Enter.

Step 13

Make sure you’re still in the right directory after exiting the VIM editor:

[email protected]:/etc/phpredis-master$

Then, type:

sudo service php7.3-fpm restart

This will restart PHP-FPM.

Again, add the PHP version that you’re using!

Step 14

Even if you don’t really have to, you should restart Nginx as well.

sudo service nginx restart

You could type this, first, to see if everything’s all right with Nginx:

sudo nginx -t

You should get something like this if everything’s ok:

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

Step 15

If you plan to use the Redis Facade alias, follow Laravel’s instructions and rename it in config/app.php to something else (e.g. RedisManager).

redis alias laravel

Testing If PhpRedis Works in Laravel

Step 1

Create a Route like this:

Route::get('/redis', function () {
    $app = RedisManager::connection();
    $app->set('key', 'Testing PhpRedis');
    echo $app->get('key');

Now go to yourdomain.test/redis and you should see the value, which is “Testing PhpRedis”.

testing phpredis route laravel

Of course, change yourdomain with your actual testing domain.

Step 2

Let’s perform an additional check in Ubuntu.

Type this:



keys *

And you should see something like this:

1) "laravel_database_key"
laravel database key redis

The Video That Made All of This Possible

Of course, I didn’t figure out all of this on my own. 😀

Luckily, I found a good video tutorial that probably saved me a couple of days of frustration while trying to put the pieces together from 10 different guides.

Here it is:

Note that, in the video, he uses this library, which is a PhpRedis connector for Laravel.

Laravel doesn’t need that anymore because it uses PhpRedis by default now.

Several years ago, it used Predis by default.

Learn more: How to Create a Database in Laravel with Homestead (No phpMyAdmin)

That’s a Wrap

I hope this tutorial helped you out and everything will go smoothly when installing PhpRedis for Laravel on Ubuntu.

If you have questions or thoughts, please leave a comment or send me a message using the contact page.

Don’t forget to share the post to help out others!

Ad · I've been using TorGuard VPN for over 6 years. Try it, too, for free!

10 thoughts on “How to Install PhpRedis for Laravel on Ubuntu”

  1. Thanks , a question here is every where we want to use it we should first use RedisManager::connection and then call $redis->set which at least two line of code , it is not clean , is there anyway to do that like it , RedisManager::set ?

    • That part with RedisManager::connection() in the Route is just for quick testing purposes. You don’t have to use it like that, especially not in Routes.

      In your application, you can use the Facade and then call methods, like, RedisManager::get(). You can learn more from the Laravel documentation on Redis.

      Furthermore, what many others do, is to set the cache driver to Redis, then use the Cache Facade instead.

  2. Thanks for this guide, it was really useful. Do you however know the reasoning behind renaming Redis to RedisManager?

    “If you plan to use PhpRedis extension along with the Redis Facade alias, you should rename it to something else, like RedisManager, to avoid a collision with the Redis class. You can do that in the aliases section of your app.php config file.”

    • Yep, it says right there: “to avoid a collision with the Redis class”. 😀 So, there’s already a class named Redis, thus you should use a different name for the Redis Facade.

  3. Super helpful! I was using predis client and wanted to switch but I saw the phpredis library and just got overwhelmed. Thanks for the instructions! Made it pretty painless.


Leave a Comment