Skip to content

Instantly share code, notes, and snippets.

@iacchus
Last active April 9, 2024 09:56
Show Gist options
  • Star 13 You must be signed in to star a gist
  • Fork 6 You must be signed in to fork a gist
  • Save iacchus/5f694e726e80e4b9141f5ee082bfe848 to your computer and use it in GitHub Desktop.
Save iacchus/5f694e726e80e4b9141f5ee082bfe848 to your computer and use it in GitHub Desktop.
Install all Google Fonts on Linux (Debian/Ubuntu etc.)
#!/usr/bin/env sh
# Original author: Michalis Georgiou <mechmg93@gmail.comr>
# Modified by Andrew http://www.webupd8.org <andrew@webupd8.org>
# Current version by Kassius Iacchus https://github.com/iacchus/
# Depends on: `wget`
#
# Install `wget` on Debian/Ubuntu with:
# apt install wget
# Usage:
#
# 0. Copy the contents of this script to a text file named,
# for example, "install_fonts.sh".
#
# 1. Make this file executable with the command:
# chmod +x install_fonts.sh
#
# 2. Run it with:
# ./install_fonts.sh
_wgeturl="https://github.com/google/fonts/archive/main.tar.gz"
_gf="google-fonts"
echo "Connecting to Github server to download fonts..."
wget $_wgeturl -O $_gf.tar.gz
echo "Extracting the downloaded archive..."
tar -zxvf $_gf.tar.gz
echo "Creating the /usr/share/fonts/truetype/$_gf folder"
sudo mkdir -p /usr/share/fonts/truetype/$_gf
echo "Installing all .ttf fonts in /usr/share/fonts/truetype/$_gf"
find $PWD/fonts-main/ -name "*.ttf" -exec sudo install -m644 {} /usr/share/fonts/truetype/google-fonts/ \; || echo "An error occured, please run this script again."
echo "Updating the font cache"
fc-cache -f
echo "Done. Now you can delete the tarball file $_gf.tar.gz if you wish."
@ajohnclark
Copy link

Thanks for making this useful script. You now need to replace URL with https://github.com/google/fonts/archive/main.tar.gz now and the folder name to /fonts-main/ as well for this to work correctly.

@iacchus
Copy link
Author

iacchus commented May 15, 2021

Thank you, @ajohnclark, changed.

@crispy-cat
Copy link

Still works great in 2023!

@alexmyczko
Copy link

or maybe just install a few and preview some others using apt install fnt, see https://github.com/alexmyczko/fnt

@ClashTheBunny
Copy link

I prefer to have the category as part of the directory path, I also don't like to exec the sudo install a bunch of times:

find $PWD/fonts-main -iname METADATA.pb -exec grep "category: " {} + | cut -d \" -f 2 | sort | uniq | while read category; do echo $category; find $(find google-fonts -iname METADATA.pb -exec grep "category: \"$category\"" {} + | sed -e 's/\/METADATA.pb.*//g') -iname "*.ttf" -exec sudo install -D -m644 -t /usr/share/fonts/truetype/google-fonts/$category/ {} +; done

This would replace line 36.

This way, I can choose a random google font that's of the display category:

fc-list : family file | grep google-fonts/DISPLAY | cut -d " " -f 2- | cut -d , -f 1 | shuf -n1

@arvindautar
Copy link

arvindautar commented Sep 19, 2023

updated the script:

Directory Structure
Introduced a new variable _directory to represent the main directory where fonts are extracted (i.e., fonts-main), replacing the previously hardcoded path (google-fonts).
Adjusted all commands to work with the updated directory structure.

Font Installation
Replaced the old font installation method (which directly copied all .ttf files) with a more refined method that categorizes fonts based on metadata present in the METADATA.pb files.
Organized the installation so that fonts are installed in subdirectories corresponding to their categories (e.g., DISPLAY, HANDWRITING, etc.).

Error Handling
Redirected potential error messages during the font installation process (using 2>/dev/null) to discard overwrite warnings. This approach avoids showing warnings when the script attempts to install the same font under multiple categories.

Cleaned Up Feedback
Updated the echoed feedback messages to provide clearer instructions and information on the steps being taken.

#!/usr/bin/env sh

# Original author: Michalis Georgiou <mechmg93@gmail.comr>
# Modified by Andrew http://www.webupd8.org <andrew@webupd8.org>
# Current version by Kassius Iacchus https://github.com/iacchus/
# Further adjustments based on user's feedback
# basedupon feedback updated by Arvind Autar arvind.autar@gmail.com

# Dependencies:
# Install `wget` on Debian/Ubuntu with:
# apt install wget

# Usage:
# 0. Copy the contents of this script to a file named "install_fonts.sh".
# 1. Make this file executable: chmod +x install_fonts.sh
# 2. Run: ./install_fonts.sh

_wgeturl="https://github.com/google/fonts/archive/main.tar.gz"
_archive="google-fonts"
_directory="fonts-main"

echo "Connecting to Github server to download fonts..."
wget $_wgeturl -O $_archive.tar.gz

echo "Extracting the downloaded archive..."
tar -zxvf $_archive.tar.gz

echo "Creating the /usr/share/fonts/truetype/$_archive folder"
sudo mkdir -p /usr/share/fonts/truetype/$_archive

echo "Installing fonts categorized by their metadata..."

find $PWD/$_directory -iname METADATA.pb -exec grep "category: " {} + | cut -d \" -f 2 | sort | uniq | while read category; do 
    find $(find $_directory -iname METADATA.pb -exec grep "category: \"$category\"" {} + | sed -e 's/\/METADATA.pb.*//g') -iname "*.ttf" -exec sudo install -D -m644 -t /usr/share/fonts/truetype/$_archive/$category/ {} + 2>/dev/null; 
done

echo "Updating the font cache"
fc-cache -f

echo "Done. You can delete the tarball file $_archive.tar.gz if desired."

@Gauley123
Copy link

Does this work on arch/manjaro?

@arvindautar
Copy link

Does this work on arch/manjaro?

Yes, it is distribution-independent; just ensure that you have wget installed

@iacchus
Copy link
Author

iacchus commented Oct 20, 2023

@Gauley123 you can install wget on archlinux/manjaro with:

sudo pacman -Syu wget

It works, I use archlinux too.

@mittwerk
Copy link

mittwerk commented Mar 9, 2024

Thanks, it's works great!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment