July 1, 2021 in Tutorial3 minutes

This tutorial explain how to install memcached from source on Ubuntu 20.04 (LTS) Focal Fossa. Memcached can be use for any caching usage but mostly used by dynamic web application to reduce database load. We can also cache API calls and page rendering.
Memcached (pronounced: mem-cash-dee) is a free, high performance, distributed memory object caching system.
Why Memcached from source? Ubuntu already shipped with Memcached but not the latest version. The version shipped with Ubuntu 20.04 is 1.5.22 while the latest stable of Memcached is 1.6.9.
This tutorial assumes we have a fresh install of Ubuntu 20.04. Any flavor of ubuntu will works but in this tutorial I use Ubuntu Server 20.04.
Before we can compile memcached, we need to install tools to compile memcached and memcached dependencies.
We can use command below to install three packages needed so we can compile memcached on ubuntu 20.04.
sudo apt-get install gcc make libevent-dev libc6-dev --no-install-recommendsWe use --no-install-recommends options so apt will only install package that we asked to install and its dependencies only.
You can see description of each package that we install below:
Now all tools installed we can start compiling Memcached on Ubuntu 20.04.
Download memcached source code from memcached website. To get latest version of Memcached you can visit Memcached download page.
To download using wget we can use command below.
wget -c http://www.memcached.org/files/memcached-1.6.9.tar.gzTo download using curl we can use command below.
curl http://www.memcached.org/files/memcached-1.6.9.tar.gz --output memcached-1.6.9.tar.gzAfter download finished, Extract downloaded file using command below
tar xzf memcached-1.6.9.tar.gzGo to the new directory
cd memcached-1.6.9/Configure the code before we can compile memcached. We will use --prefix=/opt/memcached so memcached binary and libraries will be installed on /opt/memcached instead of /usr/local
./configure --prefix=/opt/memcachedAfter configuration complete. let’s compile Memcached
make$ ./memcached --version
memcached 1.6.9To install memcached we can use command below
sudo make installWe need to use sudo since we’re installing on /opt directory.
Memcached use libevent library. The one shipped with ubuntu is version 2.1.11
We can use latest version of Libevent to compile memcached. If you’re wondering why compile memcached with latest version of libevent? for now just because we can. I haven’t done any benchmark to compare memcached compiled with libevent shipped with ubuntu 20.04 and memcached compiled with latest version of libevent.
Let’s get started. Download libevent 2.1.8 using wget
wget -c https://github.com/libevent/libevent/releases/download/release-2.1.12-stable/libevent-2.1.12-stable.tar.gzExtract downloaded file
tar xzf libevent-2.1.12-stable.tar.gzGo to the extracted directory
cd libevent-2.1.12-stableBefore we run configure, we need to install libssl-dev package. Since this version of libevent needs openssl headers. We can install libssl-dev using the command below.
sudo apt-get install libssl-devRun configure with --prefix option since we want to install libevent on /opt/libevent instead of on default path.
./configure --prefix=/opt/libeventCompile libevent using make
makeInstall compiled libevent
sudo make installWe assume you already download source code of Memcached 1.6.9. Extracted the compressed source code
tar xzf memcached-1.6.9.tar.gzGo to the new directory
cd memcached-1.6.9/We run ./configure with prefix and with-libevent option
./configure --prefix=/opt/memcached --with-libevent=/opt/libeventCompile memcached source using make
makeWe can test whether memcached already compiled successfully by checking version using command below
./memcached --versionTo install compiled memcache we will use make with install option.
make installIn this tutorial we learned how to compile memcached from source on Ubuntu 20.04 (Focal Fossa). We also tried to compile latest stable release of libevent and compile memcached with compiled libevent. Until next time.