How to install Redis on your OS of choice?
Tags: redis, docker, windows, macos
Published: 2021-09-23 00:45:38

In this article, I'm going to teach you how you can use the latest version of Redis on your OS(including Windows, Linux-based OS, MacOSX, FreeBSD-based OS) using docker and binaries. We will use three different ways to install and use Redis on your OS.

## What is Redis?

Redis is an open-source (BSD licensed), in-memory data structure store, used as a database, cache, and message broker. Redis provides data structures such as strings, hashes, lists, sets, sorted sets with range queries, bitmaps, hyperloglogs, geospatial indexes, and streams. Redis has built-in replication, Lua scripting, LRU eviction, transactions, and different levels of on-disk persistence, and provides high availability via Redis Sentinel and automatic partitioning with Redis Cluster.

![reids logo](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pd45j8b4regahvg0tsdw.jpeg)

## How to use Redis on your OS of choice?

I will teach you three ways to install and use Redis on your OS of choice. Keep in mind not all of the methods work for every OS.

1. [Using source code(for Unix-like OS(e.g. MacOSX, Linux-based OS, FreeBSD-based OS))](#using-source-code)
2. [Using package managers(for Unix-like OS(e.g. MacOSX, Linux-based OS, FreeBSD-based OS))](#using-package-managers)
3. [Using Docker(for every OS that supports Docker)](#using-docker)

My preferred method is using Docker, it will work perfectly for every OS and the only thing you need is to install Docker.

> I have a complete article on ["How to install Redis on Windows?"](https://dev.to/devaddict/how-to-install-redis-on-windows-533c) if you are a Windows consumer that can help you more.

## What is Docker?

In 2013, Docker introduced what would become the industry standard for containers. Containers are a standardized unit of software that allows developers to isolate their app from its environment, solving the “it works on my machine” headache. For millions of developers today, Docker is the de facto standard to build and share containerized apps - from desktop, to the cloud. We are building on our unique connected experience from code to cloud for developers and developer teams.

![docker logo](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2e0zc6p6quaghhmft1b1.jpeg)

## Using source code

You can download the source code from the [redis.io](https://redis.io) website but in this article, I'm going to use `wget` to download the source for the stable release. 

> This method only works for Unix-like systems that can run `tar` and `make` commands.

> If any of these links didn't work just search for Redis's official installation page.

1.First check if you have `wget` installed on your system:

  ```
  wget --version
  ```

2.If you don't have `wget` installed on your system, install it:

  - CentOS/RHEL/Fedora:
    ```
    $ sudo yum install wget
    ```

  - Debian/Ubuntu/Mint:

    ```
    $ sudo apt install wget
    ```

  - ArchLinux/Manjaro:

    ```
    $ sudo pacman -S wget
    ```

  - openSUSE:

    ```
    $ zypper install wget
    ```

  - AlpineLinux:

    ```
    $ sudo apk add wget
    ```

  - MacOSX:
  
    ```
    # install Homebrew first, then run this command
    $ brew install wget
    ```

  - FreeBSD:

    ```
    $ pkg install wget
    ```

3.Now you need to download the source using `wget`. I'm going to install the stable release, but if you want any other versions you can check [Redis's download page](https://redis.io/download):

  ```
  $ wget http://download.redis.io/redis-stable.tar.gz
  ```

4.Now you can extract the downloaded files using the `tar` command:

  ```
  $ tar xvzf redis-stable.tar.gz
  ```

5.Now enter the directory that is created from extracting the downloaded file:

  ```
  $ cd redis-stable
  ```

6.Now you need to build the code in order to use it:

  ```
  $ make install
  ```

  If permission was denied, you need to run it as a superuser:

  ```
  $ sudo make install
  ```

  > Note that you can just run `make` and it will build the code to `src/` directory and you can run the commands from there.

7.Finally, you can run redis-server and redis-cli to run and use redis in your terminal.

## Using package managers

In this method, we are going to use your OS package manager in order to install Redis.

> This method only works for Unix-like systems with package managers.

> You can install Homebrew for MacOSX to use this method.

1.First, install the Redis package via your package manager:

  - CentOS/RHEL/Fedora:
    ```
    $ sudo yum install redis
    ```

  - Debian/Ubuntu/Mint:

    ```
    $ sudo apt install redis
    ```

  - ArchLinux/Manjaro:

    ```
    $ sudo pacman -S redis
    ```

  - openSUSE: for different distributions of openSUSE, you can check the official [package page](https://software.opensuse.org/download/package?package=redis&project=server%3Adatabase).

  - AlpineLinux:

    ```
    $ sudo apk add redis
    ```

  - MacOSX:
  
    ```
    # install Homebrew first, then run this command
    $ brew install redis
    ```

  - FreeBSD:

    ```
    $ pkg install redis
    ```

2.Now you have access to redis-server and redis-cli commands.

3.You can also start the redis service on your OS so you don't have to run redis-server to initialize the server and you can have your service running in the background:

  - Linux:

    ```
    $ systemctl start redis
    ```

    If permission was denied, you need to run it as a superuser:

    ```
    $ sudo systemctl start redis
    ```

  - MacOSX:

    ```
    $ brew services start redis
    ```

  - FreeBSD:

    ```
    $ service redis start
    ```

4.Finally, you can stop redis service whenever you don't need it anymore.

  - Linux:

    ```
    $ systemctl stop redis
    ```

    If permission was denied, you need to run it as a superuser:

    ```
    $ sudo systemctl stop redis
    ```

  - MacOSX:

    ```
    $ brew services stop redis
    ```

  - FreeBSD:

    ```
    $ service redis stop
    ```
  
  > If you are on a Linux-based system, you can also use the snap store, but I do not recommend it.

## Using Docker

In this method, you need to have Docker installed. Docker has pretty good documentation to help you.

1.First, you need to create the Redis container and expose port `6379`:

  ```
  $ docker container run --name container-name -p 6379:6379 -d redis
  ```

  > `docker container run` will make docker create and run a new container. 

  > `--name` flag will specify the name of the container so you can change container-name to your name of choice.

  > `-p` flag will tell docker to expose a port from container to computer, in this case, is the Redis default port.

  > `-d` flag will start the container in the detach mode so the container won't stop when we close our terminal.

  > And finally the Redis means use the Redis image.

2.Finally, you can run redis-cli using Docker:

  ```
  $ docker container run -it --link container-name:redis --rm redis redis-cli -h redis -p 6379
  ```

  > `-it` flag will make docker open an interactive instance of the container.

  > `--link` flag will link the container we previously created as Redis in this container.

  > Note that you should change container-name with the name you chose in the first command.

  > `--rm` flag will make docker remove the container after we close it. (This is useful cause we will no longer need this container after we ran redis-cli command)

  > Same as before Redis means use the Redis image.

  > `redis-cli -h redis -p 6379` is the command docker will run for us.

  > In the redis-cli command `-h` specifies the host which we set it to redis using `--link` flag.
  
  > And finally in the redis-cli command `-p` specifies the port which is by default `6379`.

### Resources


### Find Me

- [`@AriaAzadiPour`](https://twitter.com/AriaAzadiPour) on Twitter