1. Overview

Software engineers can generally use many kinds of databases. Some have specific clients for connection and management. For example, Redis CLI interacts with and manipulates the data from a Redis server. In fact, depending on our project requirements, we may not need the entire Redis setup.

In this tutorial, we’ll learn how to install only Redis CLI and connect to a Redis server from a local Linux machine.

First, we start with a short introduction to Redis and Redis CLI. Next, we understand the need for installing Redis CLI only and how we can achieve that. On the final point, we go through different means and platforms that support the client.

2. Introduction to Redis and Redis CLI

Redis is an open-source database that saves data in the form of key-value pairs. It’s widely used because it stores information in memory rather than on secondary drives. It can be used for different purposes:

  • cache: high-speed volatile memory with limited size, placed physically close to the CPU
  • message broker: facilitates the communication between applications, services, and systems
  • streaming engine: enables a system to analyze, filter, and transform streaming data in real-time

In effect, this makes the data access very fast. Simply put, Redis works as an application cache or quick response data store. Thus, the Redis database provides reliability and performance.

Redis CLI is a command-line tool that interacts with Redis servers. It’s used to query the Redis database. We can use Redis CLI in two modes:

  • interactive mode: user fires commands at a prompt and gets replies
  • command mode: user adds a command as an argument to redis-cli and the command result is shown on the standard output

Now that we’ve established the basics, let’s find out why it might be important to know ways to install Redis CLI alone.

3. Why Install Only Redis CLI?

Sometimes, we don’t want to install a complete Redis setup.

For instance, we could be building software using Redis, but we may not need to personally manage or administrate a localhost process of Redis. In this scenario, we just need Redis CLI installed locally. This way, we can avoid the installation of unnecessary files and, more importantly, services.

Let’s see some reasons why we might prefer installing Redis CLI alone.

3.1. Resolving Dependencies

The full Redis installation is a combination of many components including the Redis server and Redis CLI.

To install the Redis server, we first need to have dependencies installed on the system. Moreover, we might require a compiler like the C compiler, as well as some configuration. It might not be easy for all users to make a system compatible in such a manner. To only install Redis CLI, we need much fewer prerequisites, thereby eliminating the need for most of our efforts.

Getting dependencies ready is also overkill for users who only need to connect to a server instead of hosting it, especially on a low-specification machine.

3.2. Platform Compatibility

For users on different OS platforms like Windows, Mac OS, and various Linux distributions it may be very complex to install a complete Redis setup. Each OS platform has different packages and compatibility requirements. In general, it’s better to avoid the installation of unnecessary files.

Keeping this in mind, we’ll learn a few effective ways of installing Redis CLI alone.

4. Using redis-tools Package

redis-tools is a Linux software package that contains Redis CLI along with many other Redis tools but only on the client side.

To use them, we first install redis-tools:

$ sudo apt install redis-tools

After the installation of redis-tools, we can go ahead with the connection to a Redis server. To do so, we supply the exact hostname and port of the desired Redis server:

$ redis-cli -h redis_server_endpoint -p port_number

Notably, the redis-tools package is available only on Debian-based Linux distributions such as Ubuntu.

5. Using the Node.js Redis CLI

The Node.js runtime environment can be used to install various software along with their dependencies. In fact, we can get a Node Redis CLI up and running on a system with the help of Node.js.

First, we install Node.js and npm. Then, we can install the Node.js version of Redis CLI via npm, a package manager for Node.js packages:

$ npm install -g redis-cli

After the installation, we can connect to the Redis server:

$ rdcli -h my.redis.host -a myredispass -p 1111

Conversely, we can avoid the global installs if we want. To do that, we can clone the below repository and install dependencies:

$ git clone https://github.com/lujiajing1126/redis-cli
$ cd redis-cli

Now, we can install the Redis CLI with npm:

$ npm install

Then, we can run redis-cli from the directory containing index.js. It invokes index.js and also accepts command-line arguments:

$ node index.js -h my.redis.host -a myredispass -p 1111

Node.js has a wider range of installation options. That’s why many users prefer Node.js compared to building Redis from the source. Further, we can install Node.js on many platforms like Linux, Windows, macOS, and others.

6. Building Redis CLI From Source

Basically, in this method, we download the Redis source code from the Redis Github repository and install Redis CLI. This method is usually much more straightforward than an entire Redis server deployment and applies to almost all Linux distributions.

First, we ensure we have the necessary system libraries installed. Then, we create an executable from the Redis package followed by the installation of Redis CLI:

$ sudo yum -y install openssl-devel gcc
$ wget http://download.redis.io/redis-stable.tar.gz
$ tar xvzf redis-stable.tar.gz
$ cd redis-stable
$ make distclean
$ make redis-cli BUILD_TLS=yes
$ sudo install -m 755 src/redis-cli /usr/local/bin/

As a result, we can connect to a Redis server and run queries as we already saw.

There is also an efficient script that achieves the same goal. We can download it and run it on a machine via curl:

$ curl -sL "https://raw.githubusercontent.com/SecretX33/redis-cli/main/install_redis_cli.sh" | bash

Thus, we can avoid most of the manual work when deploying Redis CLI alone.

7. Using a Docker Image

Using Docker to install Redis CLI is a possible alternative too. Actually, the Redis Docker image is based on the popular Alpine Linux Project and comes with Redis CLI preinstalled on it. The benefit of that is that the Alpine Linux image is smaller than most other distributions, so it’s a convenient container for Redis CLI users despite the host OS.

Basically, the deployment and usage process boils down to a single command:

$ docker run -it --rm redis:latest-alpine redis-cli -h my.redis.host -p 6379

In the example above, we’ve included the latest Alpine Docker image for Redis but we can always check supported Alpine Docker images on Docker hub. After that, we specify the redis-cli command and its parameters.

It’s possible to use any other Linux image as well. We can achieve that by adding Redis CLI to it.

We can use a Dockerfile with a fairly basic script to install Redis CLI within specific Linux distribution containers:

FROM	Linux_distro

RUN	cd /tmp &&\
	curl http://download.redis.io/redis-stable.tar.gz | tar xz &&\
	make -C redis-stable &&\
	cp redis-stable/src/redis-cli /usr/local/bin &&\
	rm -rf /tmp/redis-stable

Then, we can build a Docker image:

$ docker build -t redis-cli .

A new image will be created with the name redis-cli using the Dockerfile from the current directory.

This way, we download Redis files and install only Redis CLI to any image. Thus, we can use this method of installing Redis CLI on any platform that supports Docker.

8. Alternative to Using Redis CLI

Let’s look at a fairly unique, but very portable and effective way of connecting to a Redis server.

Specifically, we can use the netcat command to query a Redis server without the need to install Redis CLI:

$ nc -v --ssl redis.example.com 6379
Connection to redis.example.com 6379 [tcp/*] succeeded

The -v option shows additional processing information. The next option, –ssl is used to connect or listen with SSL.

Here, we specify the redis.example.com Redis server at the usual port 6379.

9. Conclusion

In this article, we talked about Redis and Redis CLI, as well as ways to install the latter without the former.

First, we touched on the basics of using Redis and Redis CLI. Then, we learned why we might need Redis CLI alone. Finally, we went through some ways of installing Redis CLI and using it to query a Redis server.

Comments are closed on this article!