Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to free disk space taken up by (ana)conda?

I am using the conda package manager - a lot. By now I have quite a few environments and a lot of downloaded packages taking a lot of space on my SSD. An obvious path to free some of that space is to use the command

conda env export > environment.yml 

from https://conda.io/docs/user-guide/tasks/manage-environments.html#exporting-the-environment-file to export which packages my old, inactive projects use(d) and then delete these environments. As far as I understand, this should free some of the space in anaconda2/envs/, but not in anaconda2/pkgs/. How do I get rid of these packages? Also, I suspect that there might be quite a few packages still sitting around, to which no environment is linking to - could that happen?

Questions:

  1. In general: What is the best way to reduce the space taken up by conda?
  2. How do I get rid of packages that no environment is using anymore? How do I prune my packages? I am searching for something like sudo apt-get autoremove from Ubuntu/Debian.
like image 362
Make42 Avatar asked Feb 09 '18 13:02

Make42


People also ask

How much space does conda take?

Be sure to give Anaconda enough room to operate. The minimum disk space for an Anaconda installation is 6 GB, and the recommended amount is 20 GB.

How do you clean a conda environment?

Removal TargetsRemove index cache, lock files, unused cache packages, and tarballs. Remove index cache. Remove unused packages from writable package caches. WARNING: This does not check for packages installed using symlinks back to the package cache.

Is it safe to do conda clean?

Conda documentation says that it it's safe to do that, as it will only erase packages that "have never been used in any environment".

Does deleting conda environment delete packages?

The conda environment will be deleted. Sometimes some packages stay behind, although they are not bound to any environment.


2 Answers

You can free some space with:

conda clean --all 

clean Remove unused packages and caches.

Conda already use symlinks when possible for packages. So, not much to improve here, I guess.

Ok, thanks, but I would like to know "not for a specific environment, but in general" - for all environments.

You can list all packages in all envs with a few lines of Python:

import os import subprocess for env in os.listdir('/Users/me/miniconda3/envs'):     subprocess.call(['conda', 'list', '-n', env]) 
like image 136
Mike Müller Avatar answered Sep 22 '22 21:09

Mike Müller


Finally I got around dealing with this issue. In the end it was a couple of days work:

  1. For all my Python projects I use PyCharm and with it I checked which project uses which environment. For all environments I used the conda env export > environment.yml to save the settings of the environment from https://conda.io/projects/conda/en/latest/user-guide/tasks/manage-environments.html#exporting-the-environment-file
  2. Check whether my projects still work with new environments created from the environment.yml.
  3. Use anaconda-clean from option B in https://docs.anaconda.com/anaconda/install/uninstall and put the created backup in a save place.
  4. Rename the old anaconda2 directory to anaconda2_backup.
  5. Install a new conda environment - miniconda3 in my case.
  6. Build new environments which are need for current projects from the environment.ymls and check whether these work.
  7. Delete the old anaconda backups.

Finally I also reduced my logical volume with https://blog.shadypixel.com/how-to-shrink-an-lvm-volume-safely/ but this is only for Linux users using LVMs.

This way I was able to free 20 to 30 GB of space.

like image 21
Make42 Avatar answered Sep 23 '22 21:09

Make42