· Kalpa Madhushan · documentation · 2 min read

How to Install hiredis and Use in C

Learn how to install the hiredis library and use it in your C projects to interact with Redis, ensuring efficient and effective database management.

Learn how to install the hiredis library and use it in your C projects to interact with Redis, ensuring efficient and effective database management.

How to Install hiredis and Use in C

When you’re trying to use Redis in your C projects, you need the hiredis library. This article guides you through the process of installing hiredis and integrating it into your C program. Let’s get started.

Here is the updated section with the Redis installation instructions:

Install Redis

To install Redis on your system, run the following commands:

curl -fsSL https://packages.redis.io/gpg | sudo gpg --dearmor -o /usr/share/keyrings/redis-archive-keyring.gpg

echo "deb [signed-by=/usr/share/keyrings/redis-archive-keyring.gpg] https://packages.redis.io/deb $(lsb_release -cs) main" | sudo tee /etc/apt/sources.list.d/redis.list

sudo apt-get update
sudo apt-get install redis

Install Dependencies

First, ensure you have the necessary build tools installed on your system:

sudo apt-get update
sudo apt-get install build-essential

Download and Install hiredis

You can download hiredis from its GitHub repository and then install it with the following commands:

git clone https://github.com/redis/hiredis.git
cd hiredis
make
sudo make install

Update the Library Path

After installing hiredis, update the library path by adding it to /etc/ld.so.conf.d/ and running ldconfig:

echo "/usr/local/lib" | sudo tee /etc/ld.so.conf.d/hiredis.conf
sudo ldconfig

Compile Your C Program

Now, you should be able to compile your C program without encountering the “No such file or directory” error. Make sure to link the hiredis library when compiling:

gcc -o your_program your_program.c -lhiredis

Replace your_program.c with the name of your C source file.

Example Usage

Here is a simple example of how to use hiredis in your C program:

#include <stdio.h>
#include <hiredis/hiredis.h>

int main() {
    redisContext *c = redisConnect("127.0.0.1", 6379);
    if (c == NULL || c->err) {
        if (c) {
            printf("Error: %s\n", c->errstr);
            redisFree(c);
        } else {
            printf("Can't allocate redis context\n");
        }
        return 1;
    }

    redisReply *reply = redisCommand(c, "PING");
    printf("PING: %s\n", reply->str);
    freeReplyObject(reply);
    redisFree(c);

    return 0;
}

This simple program connects to a local Redis server and sends a PING command, then prints the response.

By following these steps, you’ll be able to install hiredis and use it in your C projects, enabling efficient interaction with Redis.

This file provides a comprehensive guide for installing and using hiredis in C, complete with dependencies installation, hiredis installation, and a sample usage program. Let me know if you need any adjustments or additional information!

Back to Blog

Related Posts

View All Posts »

Markdown elements demo post

Sint sit cillum pariatur eiusmod nulla pariatur ipsum. Sit laborum anim qui mollit tempor pariatur nisi minim dolor.