30

I want to move installed snap packages to home directory. Snap packages are slightly larger than .deb packages thus they take space in my root partition. I know packages are located in /var/lib/snapd/snaps and they are working with mounting the package to /snap/app_dir. I tried to simlinked, But it didn't work.

6 Answers6

44

Change snaps installation directory

This is a common issue that many ask for. It seems snap developers are not intending to solve it soon, so some solutions were suggested.

The first solution that was strongly refused by the community, but I didn't try, is to use symbolic links to link the directories that lead to snaps. This method seems not to work as the community replies says that AppArmor does not work with symbolic links.

The Second solution is mount --bind the directory /val/lib/snapd/snaps which works fine for me with the already installed applications but failed to install new applications because I was moving the directory to another partition. That gave me an error about hard linking the snap application with the cache directory which is located in /var/lib/snapd/cache. I don't know if this solution may work if the location are in the same partition or not; but this is the reason why I am moving the snap directory to free some space from partition to another one.

Third and working option is to move the /var/lib/snapd directory as a whole then mount --bind to it from another location and that worked for me and here is the steps.

Just a small note, you already know, after the rsync is done in the below steps, you can backup the data inside /var/lib/snapd to another location until the whole process is successfully done then you can remove the backup data if you want to free more space but keep /var/lib/snapd directory itself even free, as it is used by the mount point.

##############################################################################
# Take Care this section may break your System !!!
##############################################################################
##Move snap folder to Home instead of root.
#Create the directory : you can change the location
mkdir -p /home/$USER/snap/snapd

#Stop auto-updating (will not crash snaps already open) sudo systemctl mask snapd.service sudo systemctl stop snapd.service sudo systemctl disable snapd.service

#Copy the data sudo rsync -avzP /var/lib/snapd/ /home/$USER/snap/snapd/

#Do backups sudo mv /var/lib/snapd /var/lib/snapd.bak sudo cp /etc/fstab /etc/fstab.bak

#Change fstab (Change $USER with your name or change the path totally) echo "/home/$USER/snap/snapd /var/lib/snapd none bind 0 0" | sudo tee -a /etc/fstab

#remount fstab Or reboot. sudo mkdir /var/lib/snapd sudo mount -a

if ls /var/lib/snapd/ | grep snaps then echo "Re-mounting snapd folder is done successfully. !!!!" sudo rm -rf /var/lib/snapd.bak else echo "WARNING : Re-mounting snapd folder failed, please revert !!!!! " echo "WARNING : Re-mounting snapd folder failed, please revert !!!!! " echo "WARNING : Re-mounting snapd folder failed, please revert !!!!! " echo "WARNING : Re-mounting snapd folder failed, please revert !!!!! " echo "WARNING : Re-mounting snapd folder failed, please revert !!!!! "

# Trying to revert automatically
sudo cp /etc/fstab.bak /etc/fstab

sudo mount -a
sudo umount /var/lib/snapd

sudo mv /var/lib/snapd.bak /var/lib/snapd

echo "Files located at ~/snap/snapd should be removed, but are kept for
recovery until you, manually reboot the system and make sure the service
is running correctly. Then you can manually remove the folder ~/snap/snapd
!!!!!!!!!!!!!!, you should do that manually."

fi

#Restart auto-updating sudo systemctl unmask snapd.service sudo systemctl start snapd.service sudo systemctl reenable snapd.service

##############################################################################

Take care the previous section may break your System !!!

##############################################################################

References

  1. Custom disk location for certain snaps issue
  2. ask ubuntu Question (Not best solution) Link
  3. Suggestion to use bind mount instead of symbolic links in moving snap directories Link
  4. Differences between bind mount and symbolic links Link
  5. Make bind mount permenant
  • 3
    Works great for me. Thank you! I only had to (re-)create the /var/lib/snapd directory after moving it to .bak for mount -a to work – Salim Apr 02 '19 at 13:36
  • @Salim, You are right, I have missed this step which I will add to the script – Muhammad Yusuf Apr 02 '19 at 13:52
  • What do you mean "delete ~/snap/snapd", that's where we moved the files and are bind mounting from!
  • Nor can we delete from /var/lib/snapd, because that is the mount point and the contents are shadowed by the ones in ~/snap/snapd
  • – Mihail Malostanidis Nov 18 '20 at 09:22
  • 1
    @MihailMalostanidis, 1. I didn't meant to delete ~/snap/snapd. 2. I have modified the post to make it clear we can free space by emptying /var/lib/snapd but we need to keep it as it is used by mount point. I think the script is more correct than any language, you can review it and the steps inside it are correct and working. – Muhammad Yusuf Dec 29 '20 at 18:43
  • Thank you, I did my move by hand already, but I wanted to make sure the answer doesn't mislead anyone :) – Mihail Malostanidis Dec 30 '20 at 09:52
  • 2
    Riskwise, does it matter if Snapcraft it active (https://askubuntu.com/questions/1045542/how-to-stop-snapd-from-auto-updating) during Rsync behaviors? Snap does not give predictable timing, and can push random updates during this parallel (r)sync, leading to catastrophic data loss in that important edge case? I edited the script at https://askubuntu.com/review/suggested-edits/1167854 with sudo systemctl mask/stop/disable snapd.service before, and sudo systemctl unmask/start/reenable snapd.service after, in an abundance of caution. – prosody-Gabe Vereable Context Sep 06 '21 at 22:02
  • 1
    @prosody-GabVereableContext , You are right. I have accepted the review change. Thank you. – Muhammad Yusuf Sep 09 '21 at 18:14
  • I followed the above script, but all my big files are located in /snap how do I move them? – user977828 Oct 15 '21 at 04:41
  • if you are mounting on to an ntfs partition (that you have for easier data directory undeletion and Windows compatibility) see my answer below. – alchemy Mar 18 '22 at 01:58
  • Please note that Muhammad Yusuf's nice solution won't work with symlinks in the fstab. I've symlinked the external drive /media/user/Data to the shorter and more convenient /data and entered this path /data/var/lib/snapd instead of the original path, /media/user/Data/var/lib/snapd as new destination, and all my Snaps weren't linked. I've corrected the path, and now it works. – DaniJan Oct 01 '22 at 17:57
  • 1
    I'm on Kali Linux, which is also a Debian derivative like Ubuntu. I followed the script by hand and everything worked except steam. – dotnetCarpenter Nov 21 '23 at 15:52