Installing Cairo for R on Ubuntu 17.04

The Cairo library is a great 2D graphics library supporting multiple output devices. I usually use it to save plots in R.

Getting Cairo work in Ubuntu environment is pretty easy, although there is a problem which shows up pretty regularly: some dependencies are not set up properly in the Cairo DEB package.

In order to get Cairo work in R, one has to install the libcairo package first:

$ apt-get install libcairo2-dev

After that, trying to install the Cairo R package:

$ R

R version 3.3.2 (2016-10-31) -- "Sincere Pumpkin Patch"
Copyright (C) 2016 The R Foundation for Statistical Computing
Platform: x86_64-pc-linux-gnu (64-bit)

R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.

Natural language support but running in an English locale

R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.

Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.

> install.packages('Cairo')

leads to a compilation error:

gcc -std=gnu99 -I/usr/share/R/include -DNDEBUG -I/usr/include/cairo -I/usr/include/glib-2.0 -I/usr/lib/x86_64-linux-gnu/glib-2.0/include -I/usr/include/pixman-1 -I/usr/include/freetype2 -I/usr/include/libpng16  -I. -Ii
nclude    -g -O2 -fdebug-prefix-map=/build/r-base-6WVosl/r-base-3.3.2=. -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -g -fpic  -g -O2 -fdebug-prefix-map=/build/r-base-6WVosl
/r-base-3.3.2=. -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -g  -c xlib-backend.c -o xlib-backend.o
xlib-backend.c:34:74: fatal error: X11/Intrinsic.h: No such file or directory
#include <X11/Intrinsic.h>      /*->    Xlib.h  Xutil.h Xresource.h .. */
^
compilation terminated.
/usr/lib/R/etc/Makeconf:132: recipe for target 'xlib-backend.o' failed
make: *** [xlib-backend.o] Error 1
ERROR: compilation failed for package ‘Cairo’
* removing ‘/home/gykovacs/R/x86_64-pc-linux-gnu-library/3.3/Cairo’

The downloaded source packages are in
‘/tmp/RtmpZt8OjJ/downloaded_packages’
Warning message:
In install.packages("Cairo") :
installation of package ‘Cairo’ had non-zero exit status

In order to get rid of this error, you have to install a couple of additional Ubuntu packages:

  • libgtk2.0-dev – GTK is a popular graphical toolkit
  • xvfb – provides an X server that can run on machines with no display hardware and no physical input devices.
  • xauth – a small utility to read and manipulate Xauthority files, which are used by servers and clients alike to control authentication and access to X sessions
  • xfonts-base – provides a standard set of low-resolution bitmapped fonts. In most cases it is desirable to have the X font server (xfs) and/or an X server installed to make the fonts available to X clients.
  • libxt-dev – provides the X Toolkit Intrinsics (development headers), an abstract widget library upon which other toolkits are based.

by firing

$ apt-get install libgtk2.0-dev xvfb xauth xfonts-base libxt-dev

After that, the installation of the R Cairo package should work:

$ R

R version 3.3.2 (2016-10-31) -- "Sincere Pumpkin Patch"
Copyright (C) 2016 The R Foundation for Statistical Computing
Platform: x86_64-pc-linux-gnu (64-bit)

R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.

Natural language support but running in an English locale

R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.

Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.

> install.packages('Cairo')
Installing package into ‘/home/gykovacs/R/x86_64-pc-linux-gnu-library/3.3’
(as ‘lib’ is unspecified)
--- Please select a CRAN mirror for use in this session ---
trying URL 'https://cran.wu.ac.at/src/contrib/Cairo_1.5-9.tar.gz'
Content type 'application/x-gzip' length 86982 bytes (84 KB)
==================================================
downloaded 84 KB

* installing *source* package ‘Cairo’ ...
** package ‘Cairo’ successfully unpacked and MD5 sums checked
checking for gcc... gcc -std=gnu99
checking whether the C compiler works... yes
checking for C compiler default output file name... a.out
checking for suffix of executables...
checking whether we are cross compiling... no
checking for suffix of object files... o
checking whether we are using the GNU C compiler... yes
checking whether gcc -std=gnu99 accepts -g... yes
checking for gcc -std=gnu99 option to accept ISO C89... none needed
checking how to run the C preprocessor... gcc -std=gnu99 -E
checking for grep that handles long lines and -e... /bin/grep
checking for egrep... /bin/grep -E
checking for ANSI C header files... yes
checking for sys/wait.h that is POSIX.1 compatible... yes
checking for sys/types.h... yes
checking for sys/stat.h... yes
checking for stdlib.h... yes
checking for string.h... yes
checking for memory.h... yes
checking for strings.h... yes
checking for inttypes.h... yes
checking for stdint.h... yes
checking for unistd.h... yes
checking for string.h... (cached) yes
checking sys/time.h usability... yes
checking sys/time.h presence... yes
checking for sys/time.h... yes
checking for unistd.h... (cached) yes
checking for an ANSI C-conforming const... yes
checking for pkg-config... /usr/bin/pkg-config
checking whether pkg-config knows about cairo... yes
checking for configurable backends... cairo cairo-ft cairo-pdf cairo-png cairo-ps cairo-xlib cairo-xlib-xrender
configure: CAIRO_CFLAGS=-I/usr/include/cairo -I/usr/include/glib-2.0 -I/usr/lib/x86_64-linux-gnu/glib-2.0/include -I/usr/include/pixman-1 -I/usr/include/freetype2 -I/usr/include/libpng16
checking if R was compiled with the RConn patch... no
checking cairo.h usability... yes
checking cairo.h presence... yes
checking for cairo.h... yes
checking for PNG support in Cairo... yes
checking for ATS font support in Cairo... no
configure: CAIRO_LIBS=-lfreetype -lz -lpng16 -lz -lcairo -lXext -lXrender -lX11
checking for library containing deflate... none required
checking whether Cairo programs can be compiled... yes
checking whether cairo_image_surface_get_format is declared... no
checking for FreeType support in cairo... yes
checking whether FreeType needs additional flags... no
checking wheter libjpeg works... yes
checking wheter libtiff works... yes
configure: creating ./config.status
config.status: creating src/Makevars
config.status: creating src/cconfig.h
** libs
gcc -std=gnu99 -I/usr/share/R/include -DNDEBUG -I/usr/include/cairo -I/usr/include/glib-2.0 -I/usr/lib/x86_64-linux-gnu/glib-2.0/include -I/usr/include/pixman-1 -I/usr/include/freetype2 -I/usr/include/libpng16  -I. -Ii
nclude    -g -O2 -fdebug-prefix-map=/build/r-base-6WVosl/r-base-3.3.2=. -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -g -fpic  -g -O2 -fdebug-prefix-map=/build/r-base-6WVosl
/r-base-3.3.2=. -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -g  -c cairobem.c -o cairobem.o
gcc -std=gnu99 -I/usr/share/R/include -DNDEBUG -I/usr/include/cairo -I/usr/include/glib-2.0 -I/usr/lib/x86_64-linux-gnu/glib-2.0/include -I/usr/include/pixman-1 -I/usr/include/freetype2 -I/usr/include/libpng16  -I. -Ii
nclude    -g -O2 -fdebug-prefix-map=/build/r-base-6WVosl/r-base-3.3.2=. -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -g -fpic  -g -O2 -fdebug-prefix-map=/build/r-base-6WVosl
/r-base-3.3.2=. -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -g  -c cairogd.c -o cairogd.o
gcc -std=gnu99 -I/usr/share/R/include -DNDEBUG -I/usr/include/cairo -I/usr/include/glib-2.0 -I/usr/lib/x86_64-linux-gnu/glib-2.0/include -I/usr/include/pixman-1 -I/usr/include/freetype2 -I/usr/include/libpng16  -I. -Ii
nclude    -g -O2 -fdebug-prefix-map=/build/r-base-6WVosl/r-base-3.3.2=. -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -g -fpic  -g -O2 -fdebug-prefix-map=/build/r-base-6WVosl
/r-base-3.3.2=. -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -g  -c cairotalk.c -o cairotalk.o
gcc -std=gnu99 -I/usr/share/R/include -DNDEBUG -I/usr/include/cairo -I/usr/include/glib-2.0 -I/usr/lib/x86_64-linux-gnu/glib-2.0/include -I/usr/include/pixman-1 -I/usr/include/freetype2 -I/usr/include/libpng16  -I. -Ii
nclude    -g -O2 -fdebug-prefix-map=/build/r-base-6WVosl/r-base-3.3.2=. -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -g -fpic  -g -O2 -fdebug-prefix-map=/build/r-base-6WVosl
/r-base-3.3.2=. -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -g  -c img-backend.c -o img-backend.o
gcc -std=gnu99 -I/usr/share/R/include -DNDEBUG -I/usr/include/cairo -I/usr/include/glib-2.0 -I/usr/lib/x86_64-linux-gnu/glib-2.0/include -I/usr/include/pixman-1 -I/usr/include/freetype2 -I/usr/include/libpng16  -I. -Ii
nclude    -g -O2 -fdebug-prefix-map=/build/r-base-6WVosl/r-base-3.3.2=. -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -g -fpic  -g -O2 -fdebug-prefix-map=/build/r-base-6WVosl
/r-base-3.3.2=. -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -g  -c img-jpeg.c -o img-jpeg.o
gcc -std=gnu99 -I/usr/share/R/include -DNDEBUG -I/usr/include/cairo -I/usr/include/glib-2.0 -I/usr/lib/x86_64-linux-gnu/glib-2.0/include -I/usr/include/pixman-1 -I/usr/include/freetype2 -I/usr/include/libpng16  -I. -Ii
nclude    -g -O2 -fdebug-prefix-map=/build/r-base-6WVosl/r-base-3.3.2=. -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -g -fpic  -g -O2 -fdebug-prefix-map=/build/r-base-6WVosl
/r-base-3.3.2=. -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -g  -c img-tiff.c -o img-tiff.o
gcc -std=gnu99 -I/usr/share/R/include -DNDEBUG -I/usr/include/cairo -I/usr/include/glib-2.0 -I/usr/lib/x86_64-linux-gnu/glib-2.0/include -I/usr/include/pixman-1 -I/usr/include/freetype2 -I/usr/include/libpng16  -I. -Ii
nclude    -g -O2 -fdebug-prefix-map=/build/r-base-6WVosl/r-base-3.3.2=. -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -g -fpic  -g -O2 -fdebug-prefix-map=/build/r-base-6WVosl
/r-base-3.3.2=. -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -g  -c pdf-backend.c -o pdf-backend.o
gcc -std=gnu99 -I/usr/share/R/include -DNDEBUG -I/usr/include/cairo -I/usr/include/glib-2.0 -I/usr/lib/x86_64-linux-gnu/glib-2.0/include -I/usr/include/pixman-1 -I/usr/include/freetype2 -I/usr/include/libpng16  -I. -Ii
nclude    -g -O2 -fdebug-prefix-map=/build/r-base-6WVosl/r-base-3.3.2=. -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -g -fpic  -g -O2 -fdebug-prefix-map=/build/r-base-6WVosl
/r-base-3.3.2=. -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -g  -c ps-backend.c -o ps-backend.o
gcc -std=gnu99 -I/usr/share/R/include -DNDEBUG -I/usr/include/cairo -I/usr/include/glib-2.0 -I/usr/lib/x86_64-linux-gnu/glib-2.0/include -I/usr/include/pixman-1 -I/usr/include/freetype2 -I/usr/include/libpng16  -I. -Ii
nclude    -g -O2 -fdebug-prefix-map=/build/r-base-6WVosl/r-base-3.3.2=. -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -g -fpic  -g -O2 -fdebug-prefix-map=/build/r-base-6WVosl
/r-base-3.3.2=. -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -g  -c svg-backend.c -o svg-backend.o
gcc -std=gnu99 -I/usr/share/R/include -DNDEBUG -I/usr/include/cairo -I/usr/include/glib-2.0 -I/usr/lib/x86_64-linux-gnu/glib-2.0/include -I/usr/include/pixman-1 -I/usr/include/freetype2 -I/usr/include/libpng16  -I. -Ii
nclude    -g -O2 -fdebug-prefix-map=/build/r-base-6WVosl/r-base-3.3.2=. -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -g -fpic  -g -O2 -fdebug-prefix-map=/build/r-base-6WVosl
/r-base-3.3.2=. -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -g  -c w32-backend.c -o w32-backend.o
gcc -std=gnu99 -I/usr/share/R/include -DNDEBUG -I/usr/include/cairo -I/usr/include/glib-2.0 -I/usr/lib/x86_64-linux-gnu/glib-2.0/include -I/usr/include/pixman-1 -I/usr/include/freetype2 -I/usr/include/libpng16  -I. -Ii
nclude    -g -O2 -fdebug-prefix-map=/build/r-base-6WVosl/r-base-3.3.2=. -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -g -fpic  -g -O2 -fdebug-prefix-map=/build/r-base-6WVosl
/r-base-3.3.2=. -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -g  -c xlib-backend.c -o xlib-backend.o
gcc -std=gnu99 -shared -L/usr/lib/R/lib -Wl,-Bsymbolic-functions -Wl,-z,relro -o Cairo.so cairobem.o cairogd.o cairotalk.o img-backend.o img-jpeg.o img-tiff.o pdf-backend.o ps-backend.o svg-backend.o w32-backend.o xlib
-backend.o -lfreetype -lz -lpng16 -lz -lcairo -lXext -lXrender -lX11 -ljpeg -ltiff -L/usr/lib/R/lib -lR
installing to /home/gykovacs/R/x86_64-pc-linux-gnu-library/3.3/Cairo/libs
** R
** preparing package for lazy loading
** help
*** installing help indices
** building package indices
** testing if installed package can be loaded
* DONE (Cairo)

The downloaded source packages are in
‘/tmp/RtmpMkK8NN/downloaded_packages’

Having a successfully installed Cairo library, one can

  • import it into an R session or in a script by
    library('Cairo')
    
  • open a plotting device, like PS:
    cairo_ps(filename='dummy.ps", width=4, height=8)
    
  • create a plot object, for example using ggplot2:
    p <- ggplot(data, aes(x=x, y=y))
    
  • print the plot on the device:
    print(p)
    
  • and finally, close the device:
    def.off()
    

To read more on Cairo devices, check out the official documentation: https://cran.r-project.org/web/packages/Cairo/Cairo.pdf.

Advertisements