Installing Varnish Cache on Linux: guide and advantages

Instalar Varnish Cache para acelerar la carga web

Accelerating a website load speed by installing Varnish Cache on Linux is a simple way to improve SEO and increase visibility. In this post we are going to explain how to install Varnish Cache step by step on different Linux distributions.

Reasons to install Varnish Cache for web acceleration

Loading a web page as fast as possible is essential. The impact of a website load speed on its position on search engines like Google or Bing is significant. Since it is closely related to the user experience.

For instance, Google’s “Core Web Vitals” are a good reference for optimizing the loading speed of web pages on desktop and mobile devices. These metrics are closely related to a web page’s response time and interaction time, thus it is important that web pages load content as fast as possible.

A solution to accelerate a website load speed is using Varnish.

Varnish Cache is a memory cache to serve static content — such as images, JavaScript files, CSS sheets or pages that do not often change — directly from the server’s RAM memory. This allows loading pages faster, because it is not necessary to search in the hard disk (which is considerably slower) where the content is stored, load it and serve it.

And that’s not all! By using Varnish, the server’s processor/CPU usage will also be lower.

Moreover, using a CDN solution is also a simple and efficient way of accelerating your web load speed. Besides, a CDN allows adding WAF security options and other additional advantages, in addition to the Varnish layer.

But let’s get to the point! How can you install Varnish Cache?

How to install Varnish Cache on Linux distributions

In this post we are going to explain step by step how to install Varnish on several widely used Linux distributions: Ubuntu, Rocky Linux, CentOS and Red Hat Enterprise Linux (RHEL).

Although versions 6.6 and 7.0 of Varnish are already available, this guide will focus on the installation of Varnish Cache 6.0 LTS, as its authors recommend. Moreover, the LTS version offers a longer maintenance period, which provides greater stability and extended support.

Installing Varnish Cache on Ubuntu Linux

Ubuntu is a popular choice among servers; especially its LTS version. Therefore, in this guide we have opted for using Ubuntu 20.04 LTS for installing Varnish.

If you prefer not to install the LTS version of Varnish, Ubuntu 20.04 LTS includes the version 6.2 in its repository. In this case, it would only be necessary to use the following command to install it:

apt install -y varnish

However, in order to use Varnish Cache 6.0 LTS in Ubuntu 20.04 LTS — so that you can benefit from extended support both for caching and the operating system, here are the steps you need to follow.

Firstly, you need to import the GPG key from the APT repository of Varnish 6.0 LTS:

sudo curl -s -L https://packagecloud.io/varnishcache/varnish60lts/gpgkey | sudo apt-key add -

Once it is imported, you need to create a file for the repository:

sudo tee /etc/apt/sources.list.d/varnish60lts.list > /dev/null <<EOF
deb https://packagecloud.io/varnishcache/varnish60lts/ubuntu/ focal main
EOF

Then, you need to force APT to use the version from packagecloud.io. — since the main repository does only include the version 6.2:

sudo tee /etc/apt/preferences.d/varnishcache > /dev/null <<-EOF
Package: varnish varnish-*
Pin: release o=packagecloud.io/varnishcache/*
Pin-Priority: 1000
EOF

The next step is to instruct the system to read the repository packets that have just been added:

apt update

Finally, after completing all the previous steps, you just need to install Varnish Cache using the following command:

apt install varnish

Installing Varnish Cache on Rocky Linux

This enterprise-grade distribution, inherited from CentOS, includes by default the version 6.0 of Varnish. Thus, the installation is really simple, since you only need to use the following command:

dnf -y update
dnf install @varnish

Installing Varnish Cache on CentOS 7 and RHEL 7

Although the version 7 of CentOS and RHEL are not very common anymore, they are still widely installed.

To install Varnish Cache on CentOS 7 and RHEL 7, it is necessary to enable the EPEL (Extra Packages for Enterprise Linux) repository, add an additional repository for Varnish 6.0 LTS (since EPEL includes the version 4.0, which is too old) and finally install it.

So, start by installing the EPEL repository:

yum install -y epel-release

After doing so, install the repository of Varnish 6.0 LTS from packagecloud.io:

curl -L -s https://packagecloud.io/install/repositories/varnishcache/varnish60lts/script.rpm.sh | sudo bash

Finally, after completing all the previous steps, you only need to install Varnish Cache 6.0 LTS using the following command:

yum install -y varnish

Following these simple steps, you can easily increase the load speed of any website using Varnish. Nevertheless, of course, there are additional factors that can slow down a web page load speed, as we explain in our post about the aspects that can slow down a website.

Moreover, depending on the size and complexity of the project, relying on a specialized team of system administrators can be the best choice.

Share it on Social Media!