How to set packages caches (including AUR and YAY) on /tmp

,
Overview
Skill level Advanced
Difficulty ★★★★★
Type Scripting

I’m using Manjaro since February 2019, and I never had the necessity to downgrade packages from cache.
So, finally, I decided to move the package’s cache in /tmp.
Also for the benefit of the SSD, eg, to reduce the WAI (Wear Acceleration Index).

IMPORTANT: this method is for experienced users which know what they are doing.

Furthermore:

  • Suitable for users with ample RAM and zswap or zram to prevent OOM (out-of-memory) issues when upgrading or installing a large number of packages while running multiple other processes at the same time (but is recommended to performs the updates by logging out of the session and performs the update in TTY).
  • Eg, if you have plenty of RAM, I suggest to increase the available size of /tmp; this can be done in /etc/fstab:
tmpfs	/tmp		tmpfs	mode=1777,noatime,nosuid,nodev,size=10G,inode64	0	0
  • Reinstalling or downgrading packages won’t be possible without an internet connection once the cache is cleared (e.g., while traveling offline or if laptop lacks a WiFi driver, etc …).

  • Ideal for users who have a good internet speed and don’t mind re-downloading packages after the cache is cleared.


Proceed:

For pacman/pamac is simply. in the file /etc/pacman.conf, change/edit:

CacheDir = /tmp/pacman-pkg/

But for AUR and YAY, there is the necessity to create every time after boot, the folders (linked from /tmp to the disk), so I’ve made these bash scripts, executed on boot (I’m on Xfce, so I’ve set them in the preflet “Session and Startup”).

The scripts are the following:

AUR cache 1:

#!/bin/bash

tmpcachefolder="/tmp/pamac-build-dave"
if [ -d "$tmpcachefolder" ]; then
echo "Aur's cache folder already created"
else
## SET AUR'S CACHE ON /TMP
rm -fr /var/tmp/pamac-build-dave
mkdir -p /tmp/pamac-build-dave
ln -s /tmp/pamac-build-dave /var/tmp/pamac-build-dave
fi
exit 0

AUR cache 2:

tmpcachefolder2="/tmp/aur-dave"
if [ -d "$tmpcachefolder2" ]; then
echo "Aur's cache folder already created"
else
## ON THE FIRST BOOT, /var/tmp/pamac/aur-dave DOESN'T EXISTS, SO CREATE IT
mkdir -p /var/tmp/pamac/aur-dave
## SET AUR'S CACHE ON /TMP
rm -fr /var/tmp/pamac/aur-dave
mkdir -p /tmp/aur-dave
ln -sf /tmp/aur-dave /var/tmp/pamac/aur-dave
fi
exit 0

YAY cache:

#!/bin/bash

tmpcachefolder="/tmp/yay"
if [ -d "$tmpcachefolder" ]; then
echo "Yay's cache folder already created"
else
## SET YAY'S CACHE ON /TMP
rm -fr /home/dave/.cache/yay
mkdir -p /tmp/yay
ln -s /tmp/yay /home/dave/.cache/yay
fi
exit 0

Obviously, you have to change “dave” with your username.

1 Like

What is the benefit of doing this?

pamac installs the pamac-cleancache.timer so there is no need for doing as you suggest.

If the cache bothers you - to better serve your preference it would be better to create an override for pamac-cleancache.service (trigger by pamac-cleancache.timer, once a month) - intially that only work for the system cache location - one would need to add more Exec lines AUR helpers.

 $ cat /usr/lib/systemd/system/pamac-cleancache.service 
[Unit]
Description=Clean packages cache

[Service]
Type=oneshot
ExecStart=/usr/bin/pamac clean --no-confirm
sudo systemctl edit pamac-cleancache.service
 $ cat /etc/systemd/system/pamac-cleancache.service.d/override.conf
ExecStart=/usr/bin/pamac clean --keep 0 --no-confirm

If you want to clear the cache more frequently, create an override for the timer as well

sudo systemctl edit pamac-cleancache.timer
2 Likes

To not have the cache on disk.

You are correct the cache can be moved to a volatile location and you are the system administrator of your system.

What you suggests is fine for your usecase - but for other less experienced - overriding native services may be more relevant.

You’re totally right and I agree with you.
In facts, this method is for experienced users.

I added a description - modify as you prefer :slight_smile: - thank your for your input and thoughts.

Really appreciated, I don’t have to modify what you added; thank you :slight_smile:

Moving the package caches from disk to RAM has its advantages but can also introduce some issues.

I suggest adding a note:

  • Suitable for users with ample RAM and zswap or zram to prevent OOM (out-of-memory) issues when upgrading or installing a large number of packages while running multiple other processes at the same time.

  • Reinstalling or downgrading packages won’t be possible without an internet connection once the cache is cleared (e.g., while traveling offline or if laptop lacks a WiFi driver, etc …).

  • Ideal for users who have a good internet speed and don’t mind re-downloading packages after the cache is cleared.

1 Like

Done; thank you.

Am I missing something - these lines, to me, appear to create a directory and then remove it:

## ON THE FIRST BOOT, /var/tmp/pamac/aur-dave DOESN'T EXISTS, SO CREATE IT
mkdir -p /var/tmp/pamac/aur-dave
## SET AUR'S CACHE ON /TMP
rm -fr /var/tmp/pamac/aur-dave

And is there a reason to have three scrips doing essentially the same thing, when it could be one script? Maybe something like this could be considered:

#!/bin/bash
# Move some cache directories to be on tmp

function createOnTmp {
  tmpCacheDirectory="$1"
  varCacheDirectory="$2"
  if [[ -d "$tmpCacheDirectory" ]]; then
    echo "Cache directory $tmpCacheDirectory already created"
  else
    ## SET CACHE ON /TMP
    echo rm -fr $varCacheDirectory
    mkdir -p $tmpCacheDirectory
    echo ln -s $tmpCacheDirectory $varCacheDirectory
  fi
}

createOnTmp "/tmp/pamac-build-$USER" "/var/tmp/pamac-build-$USER"
createOnTmp "/tmp/aur-$USER" "/var/tmp/pamac/aur-$USER"
createOnTmp "/tmp/yay" "/home/$USER/.cache/yay"

exit 0

Sure, because on the first boot, the directory /var/tmp/pamac with the subfolder aur-dave doesn’t exists, so is impossible to link it from /tmp: I implemented this after noticing this fact.

There is no reason, obviously; personally is a fact or preferences.