Installing fonts on a remote server
Tags: engineeringmonday, bash, font, tutorial
Published: 2021-05-17 18:33:12

Recently we encountered an interesting situation after migrating our web application to a new server - all the generated *PDFs started to look slightly different* than before. This was unexpected as no changes were made to fonts, yet after a short research the conclusion was that the **fonts were different**.

We used Adobe Acrobat to check for font names and found three different results for old server, new server and locally generated PDFs. This pointed to the fact that the generic *sans-serif* font family was declared and the tool we use for PDF generation ([wkhtmltopdf](https://wkhtmltopdf.org/)) used the default sans-serif font available on the operating system the application was running on (`Helvetica` on local Mac OS and `DejaVu Sans` on new CentOS Linux 7 server). Since the documents were required to look the same and wkhtmltopdf has issues with Webfonts, the agreed solution was to install the same old font and set it as the default sans-serif on the new remote server.


## Gathering information and preparing directories

After SSH-ing to the remote server, we checked which operating system was present:
```bash
cat /etc/os-release
```
We also used the following commands to check
```bash
fc-match -a  # print which fonts are used
fc-list # print where font files are located
```
This gave us enough information for further steps.

On CentOS, custom font files for each user need to be placed inside `~/.fonts` directory. Since that directory didn't exist, we created it with
```bash
cd # make sure we are in home directory
mkdir .fonts
```

## Uploading and installing the font

After the .fonts directory is created, we needed to upload the font files in that directory. This can be done in multiple ways, but here we will mention the two simplest (if you don't have sudo access).

### Option 1: `wget` from a third-party server

This is a great option if the font in question is available on a publicly available third-party server like [TTFonts](https://ttfonts.net/). Inside the .fonts directory we run:
```bash
wget --no-check-certificate "http://ttfonts.net/sfonts/2/27260_NimbusSanL.ttf"
wget --no-check-certificate "https://ttfonts.net/sfonts/2/27258_NimbusSanLBold.ttf"
```
Notice we used both regular and bold font files.


### Option 2: `scp` from local machine

In case you have the needed font files available on your machine, you need to upload them to the remote. Our colleague Maroje recently wrote a [great article](https://dev.to/bornfightcompany/transferring-files-between-local-machine-and-aws-instance-4efg) about how to do just that and it was very useful.

For this option, open a terminal on your local machine and navigate to the directory which contains your font files.
Since we use SSH on our server and login without a password, we used the following command to copy all .ttf files in current directory to remote server:
```bash
scp -i /Users/username/.ssh/public_key.pub *.ttf remote-user@remote-host-ip:~/.fonts
```
For more information please see Maroje's post as he explained it in more detail.

## Setting up the alias

With the font files present on the server, they are ready to be used... but our situation required us to specify that font as the default sans-serif.

This was done by navigating to `~/.config` directory and creating a new `fontconfig` directory to hold our font alias file, which we create using the ubiquitous *nano* editor.
```bash
cd ~/.config
mkdir fontconfig
cd fontconfig
nano fonts.conf
```

The content of fonts.conf file:
```xml
<?xml version='1.0'?>
<!DOCTYPE fontconfig SYSTEM 'fonts.dtd'>
<fontconfig>
<alias>
  <family>sans-serif</family>
  <prefer><family>NimbusSanL</family></prefer>
</alias>
</fontconfig>
```

And that's it! The new font started being used immediately, even though several references advised to use:
``` bash
fc-cache -f -v
```

## Conclusion

This was a short tutorial-like post on font installation on a remote Linux machine without GUI. As a mostly front-end dev, this was somewhat out of my comfort zone but very interesting and I was happy with the result. Hopefully this helps somebody and I will put the references for more info.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6ak13tfnm5yaejo1ygns.jpg)

### References

- https://jichu4n.com/posts/how-to-set-default-fonts-and-font-aliases-on-linux/
- https://ohana.windwardstudios.com/m/76878/l/879660-how-do-i-install-fonts-in-linux
- https://askubuntu.com/questions/1138020/how-to-install-nimbussanl-regu-font-in-ubuntu-18-04

[Cover image source](https://www.w3schools.com/css/css_font.asp)