{"id":1979,"date":"2025-02-21T07:02:28","date_gmt":"2025-02-21T07:02:28","guid":{"rendered":"https:\/\/mailitics.com\/index.php\/2025\/02\/21\/dont-let-conda-eat-your-hard-drive\/"},"modified":"2025-02-21T07:02:28","modified_gmt":"2025-02-21T07:02:28","slug":"dont-let-conda-eat-your-hard-drive","status":"publish","type":"post","link":"https:\/\/mailitics.com\/index.php\/2025\/02\/21\/dont-let-conda-eat-your-hard-drive\/","title":{"rendered":"Don\u2019t Let Conda Eat Your Hard Drive"},"content":{"rendered":"<p>    Don\u2019t Let Conda Eat Your Hard Drive<br \/>\n \t<BR><br \/>\n<BR><\/BR><br \/>\n    <!-- no image --><br \/>\n \t<BR><br \/>\n<BR><\/BR><\/p>\n<div>\n<p class=\"wp-block-paragraph\" id=\"de65\">If you\u2019re an Anaconda user, you know that\u00a0<a href=\"https:\/\/docs.anaconda.com\/working-with-conda\/environments\/\" rel=\"noreferrer noopener\" target=\"_blank\"><em>conda environments<\/em><\/a>\u00a0help you manage package dependencies, avoid compatibility conflicts, and share your projects with others. Unfortunately, they can also take over your computer\u2019s hard drive.<\/p>\n<p class=\"wp-block-paragraph\" id=\"6333\">I write lots of computer tutorials and to keep them organized, each has a dedicated folder structure complete with a <a href=\"https:\/\/towardsdatascience.com\/tag\/conda-environment\/\" title=\"Conda Environment\">Conda Environment<\/a>. This worked great at first, but soon my computer\u2019s performance degraded, and I noticed that my SSD was filling up. At one point I had only 13 GB free.<\/p>\n<figure class=\"wp-block-image size-full\"><img data-recalc-dims=\"1\" data-dominant-color=\"dbe7ed\" data-has-transparency=\"false\" style=\"--dominant-color: #dbe7ed;\" loading=\"lazy\" decoding=\"async\" width=\"502\" height=\"142\" src=\"https:\/\/i0.wp.com\/towardsdatascience.com\/wp-content\/uploads\/2025\/02\/1_7wR-7xQoglU2OcEpWQksNA.webp?resize=502%2C142&#038;ssl=1\" alt=\"\" class=\"wp-image-598224 not-transparent\" srcset=\"https:\/\/towardsdatascience.com\/wp-content\/uploads\/2025\/02\/1_7wR-7xQoglU2OcEpWQksNA.webp 502w, https:\/\/towardsdatascience.com\/wp-content\/uploads\/2025\/02\/1_7wR-7xQoglU2OcEpWQksNA-300x85.webp 300w\" sizes=\"auto, (max-width: 502px) 100vw, 502px\"><figcaption class=\"wp-element-caption\">C: drive memory usage (by author)<\/figcaption><\/figure>\n<p class=\"wp-block-paragraph\" id=\"915a\">Conda helps manage this problem by storing downloaded package files in a single \u201ccache\u201d (<code>pkgs_dirs<\/code>). When you install a package, conda checks for it in the package cache before downloading. If not found, conda will download and extract the package and link the files to the active environment. Because the cache is \u201cshared,\u201d different environments can use the same downloaded files without duplication.<\/p>\n<p class=\"wp-block-paragraph\" id=\"d96e\">Because conda caches\u00a0<em>every downloaded package<\/em>,\u00a0<code>pkgs_dirs<\/code>\u00a0can grow to many gigabytes. And while conda links to shared packages in the cache, there is still a need to store some packages in the environment folder. This is mainly to avoid\u00a0<em>version conflicts<\/em>, where different environments need different versions of the same\u00a0<em>dependency\u00a0<\/em>(a package required to run another package).<\/p>\n<p class=\"wp-block-paragraph\" id=\"9e53\">In addition, large, compiled binaries like\u00a0<a href=\"https:\/\/opencv.org\/\" rel=\"noreferrer noopener\" target=\"_blank\">OpenCV<\/a>\u00a0may require\u00a0<em>full copies<\/em>\u00a0in the environment\u2019s directory, and each environment requires a copy of the Python interpreter (at 100\u2013200 MB). All these issues can bloat conda environments to several gigabytes.<\/p>\n<p class=\"wp-block-paragraph\" id=\"a7c2\">In this\u00a0<em>Quick Success Data Science<\/em>\u00a0project, we\u2019ll look at some techniques for reducing the storage requirements for conda environments, including those stored in default locations and dedicated folders.<\/p>\n<hr class=\"wp-block-separator has-alpha-channel-opacity is-style-dotted\">\n<h2 class=\"wp-block-heading\" id=\"c915\">Memory Management Techniques<\/h2>\n<p class=\"wp-block-paragraph\" id=\"5940\">Below are some <a href=\"https:\/\/towardsdatascience.com\/tag\/memory-management\/\" title=\"Memory Management\">Memory Management<\/a> techniques that will help you reduce conda\u2019s storage footprint on your machine. We\u2019ll discuss each in turn.<\/p>\n<ol class=\"wp-block-list\">\n<li class=\"wp-block-list-item\">Cache cleaning<\/li>\n<li class=\"wp-block-list-item\">Sharing task-based environments<\/li>\n<li class=\"wp-block-list-item\">Archiving with environment and specifications files<\/li>\n<li class=\"wp-block-list-item\">Archiving environments with conda-pack<\/li>\n<li class=\"wp-block-list-item\">Storing environments on an external drive<\/li>\n<li class=\"wp-block-list-item\">Relocating the package cache<\/li>\n<li class=\"wp-block-list-item\">Using virtual environments (<code>venv<\/code>)<\/li>\n<\/ol>\n<h3 class=\"wp-block-heading\" id=\"efc5\">1. Cleaning the Package Cache<\/h3>\n<p class=\"wp-block-paragraph\" id=\"9d38\">Cleaning the package cache is the first and easiest step for freeing up memory. Even after deleting environments, conda keeps the related package files in the cache. You can free up space by removing these unused packages and their associated\u00a0<em>tarballs<\/em>\u00a0(compressed package files), logs,\u00a0<em>index caches<\/em>\u00a0(metadata stored in conda), and temporary files.<\/p>\n<p class=\"wp-block-paragraph\" id=\"d897\">Conda permits an optional \u201cdry run\u201d to see how much memory will be reclaimed. You\u2019ll want to run this from either the terminal or Anaconda Prompt in your\u00a0<em>base<\/em>\u00a0environment:<\/p>\n<pre class=\"wp-block-prismatic-blocks\"><code class=\"language-bash\">conda clean --all --dry-run<\/code><\/pre>\n<p class=\"wp-block-paragraph\" id=\"eaca\">To commit, run:<\/p>\n<pre class=\"wp-block-prismatic-blocks\"><code class=\"language-bash\">conda clean --all<\/code><\/pre>\n<p class=\"wp-block-paragraph\">Here\u2019s how this looks on my machine:<\/p>\n<figure class=\"wp-block-image size-full\"><img data-recalc-dims=\"1\" data-dominant-color=\"e7e7e7\" data-has-transparency=\"false\" style=\"--dominant-color: #e7e7e7;\" loading=\"lazy\" decoding=\"async\" width=\"502\" height=\"552\" src=\"https:\/\/i0.wp.com\/towardsdatascience.com\/wp-content\/uploads\/2025\/02\/1_0LqvoSRUToXiuSUqq-jg5Q.webp?resize=502%2C552&#038;ssl=1\" alt=\"\" class=\"wp-image-598225 not-transparent\" srcset=\"https:\/\/towardsdatascience.com\/wp-content\/uploads\/2025\/02\/1_0LqvoSRUToXiuSUqq-jg5Q.webp 502w, https:\/\/towardsdatascience.com\/wp-content\/uploads\/2025\/02\/1_0LqvoSRUToXiuSUqq-jg5Q-273x300.webp 273w\" sizes=\"auto, (max-width: 502px) 100vw, 502px\"><figcaption class=\"wp-element-caption\">Conda dry run and clean command in Anaconda Prompt (by author)<\/figcaption><\/figure>\n<p class=\"wp-block-paragraph\" id=\"7c1b\">This process trimmed a healthy 6.28 GB and took several minutes to run.<\/p>\n<h3 class=\"wp-block-heading\" id=\"1776\">2. Sharing Task-based Environments<\/h3>\n<p class=\"wp-block-paragraph\" id=\"40ad\">Creating a few environments for\u00a0<em>specialized tasks<\/em>\u00a0\u2014 like computer vision or geospatial work \u2014 is more memory efficient than using dedicated environments for each\u00a0<em>project<\/em>. These environments would include basic packages plus ones for the specific task (such as OpenCV, scikit-image, and PIL for computer vision).<\/p>\n<p class=\"wp-block-paragraph\" id=\"2efa\">An advantage of this approach is that you can easily keep all the packages up to date and link the environments to multiple projects. However, this won\u2019t work if some projects require different versions of the shared packages.<\/p>\n<h3 class=\"wp-block-heading\" id=\"a17f\">3. Archiving with Environment and Specifications Files<\/h3>\n<p class=\"wp-block-paragraph\" id=\"ba61\">If you don\u2019t have enough storage sites or want to preserve legacy projects efficiently, consider using\u00a0<em>environment<\/em>\u00a0or\u00a0<em>specifications\u00a0<\/em>files. These small files record an environment\u2019s\u00a0<em>contents<\/em>, allowing you to rebuild it later.<\/p>\n<p class=\"wp-block-paragraph\" id=\"67ff\">Saving conda environments in this manner reduces their size on disk from gigabytes to a few kilobytes. Of course, you\u2019ll have to recreate the environment to use it. So, you\u2019ll want to avoid this technique if you frequently revisit projects that link to the archived environments.<\/p>\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p class=\"wp-block-paragraph\" id=\"3f68\">NOTE: Consider using\u00a0<a href=\"https:\/\/mamba.readthedocs.io\/en\/latest\/\" rel=\"noreferrer noopener\" target=\"_blank\">Mamba<\/a>, a drop-in replacement for conda, for faster rebuilds. As the docs say, \u201cIf you know conda, you know Mamba!\u201d<\/p>\n<\/blockquote>\n<p class=\"wp-block-paragraph\" id=\"761b\"><strong>Using Environment Files:<\/strong>\u00a0An\u00a0<em>environmental file<\/em>\u00a0is a small file that lists all the packages and versions installed in an environment, including those installed using Python\u2019s package installer (<a href=\"https:\/\/pypi.org\/project\/pip\/\" rel=\"noreferrer noopener\" target=\"_blank\">pip<\/a>). This helps you both restore an environment and share it with others.<\/p>\n<p class=\"wp-block-paragraph\" id=\"4886\">The environment file is written in\u00a0<a href=\"https:\/\/en.wikipedia.org\/wiki\/YAML\" target=\"_blank\" rel=\"noreferrer noopener\"><em>YAML<\/em><\/a>\u00a0(<em>.yml<\/em>), a human-readable data-serialization format for data storage. To generate an environment file, you must activate and then export the environment. Here\u2019s how to make a file for an environment named\u00a0<em>my_env<\/em>:<\/p>\n<pre class=\"wp-block-prismatic-blocks\"><code class=\"language-bash\"> conda activate my_env\n conda env export &gt; my_env.yml<\/code><\/pre>\n<p class=\"wp-block-paragraph\" id=\"d70f\">You can name the file any valid filename but be careful as an existing file with the same name will be overwritten.<\/p>\n<p class=\"wp-block-paragraph\" id=\"0922\">By default, the environment file is written to the\u00a0<em>user\u00a0<\/em>directory. Here\u2019s a truncated example of the file\u2019s contents:<\/p>\n<pre class=\"wp-block-prismatic-blocks\"><code class=\"language-yaml\">name: C:Usershannaquick_successfed_hikesfed_env\nchannels:\n  - defaults\n  - conda-forge\ndependencies:\n  - asttokens=2.0.5=pyhd3eb1b0_0\n  - backcall=0.2.0=pyhd3eb1b0_0\n  - blas=1.0=mkl\n  - bottleneck=1.3.4=py310h9128911_0\n  - brotli=1.0.9=ha925a31_2\n  - bzip2=1.0.8=he774522_0\n  - ca-certificates=2022.4.26=haa95532_0\n  - certifi=2022.5.18.1=py310haa95532_0\n  - colorama=0.4.4=pyhd3eb1b0_0\n  - cycler=0.11.0=pyhd3eb1b0_0\n  - debugpy=1.5.1=py310hd77b12b_0\n  - decorator=5.1.1=pyhd3eb1b0_0\n  - entrypoints=0.4=py310haa95532_0\n\n  ------SNIP------<\/code><\/pre>\n<p class=\"wp-block-paragraph\" id=\"5705\">You can now remove your conda environment and reproduce it again with this file. To remove an environment, first deactivate it and then run the\u00a0<code>remove<\/code>\u00a0command (where\u00a0<code>ENVNAME<\/code>\u00a0is the name of your environment):<\/p>\n<pre class=\"wp-block-prismatic-blocks\"><code class=\"language-bash\">conda deactivate\nconda remove -n ENVNAME --all<\/code><\/pre>\n<p class=\"wp-block-paragraph\" id=\"b1c7\">If the conda environment exists outside of Anaconda\u2019s default\u00a0<em>envs<\/em>\u00a0folder, then include the directory path to the environment, as so:<\/p>\n<pre class=\"wp-block-prismatic-blocks\"><code class=\"language-bash\">conda remove -p PATHENVNAME --all<\/code><\/pre>\n<p class=\"wp-block-paragraph\" id=\"f5b0\">Note that this archiving technique will only work perfectly if you continue to use the same operating system, such as Windows or macOS. This is because solving for dependencies can introduce packages that might not be compatible across platforms.<\/p>\n<p class=\"wp-block-paragraph\" id=\"e605\">To restore a conda environment using a file, run the following, where\u00a0<code>my_env<\/code>\u00a0represents your conda environment name and\u00a0<code>environment.yml<\/code>\u00a0represents your environment file:<\/p>\n<pre class=\"wp-block-prismatic-blocks\"><code class=\"language-bash\"> conda env create -n my_env -f directorypathtoenvironment.yml<\/code><\/pre>\n<p class=\"wp-block-paragraph\" id=\"adf8\">You can also use the environment file to recreate the environment on your D: drive. Just provide the new path when using the file. Here\u2019s an example:<\/p>\n<pre class=\"wp-block-prismatic-blocks\"><code class=\"language-bash\">conda create --prefix D:my_envsmy_new_env --file environment.yml<\/code><\/pre>\n<p class=\"wp-block-paragraph\" id=\"543d\">For more on environment files, including how to manually produce them, visit the\u00a0<a href=\"https:\/\/docs.conda.io\/projects\/conda\/en\/latest\/user-guide\/tasks\/manage-environments.html\" rel=\"noreferrer noopener\" target=\"_blank\">docs<\/a>.<\/p>\n<p class=\"wp-block-paragraph\" id=\"764e\"><strong>Using Specifications Files:\u00a0<\/strong>If you haven\u2019t installed any packages using pip, you can use a\u00a0<em>specifications file<\/em>\u00a0to reproduce a conda environment on the same operating system. To create a specification file, activate an environment, such as\u00a0<em>my_env<\/em>, and enter the following command:<\/p>\n<pre class=\"wp-block-prismatic-blocks\"><code class=\"language-bash\"> conda list --explicit &gt; exp_spec_list.txt<\/code><\/pre>\n<p class=\"wp-block-paragraph\" id=\"33d5\">This produces the following output, truncated for brevity:<\/p>\n<pre class=\"wp-block-prismatic-blocks\"><code class=\"language-\"> # This file may be used to create an environment using:\n # $ conda create --name &lt;env&gt; --file &lt;this file&gt;\n # platform: win-64\n @EXPLICIT\n https:\/\/conda.anaconda.org\/conda-forge\/win-64\/ca-certificates-202x.xx.x-h5b45459_0.tar.bz2\n https:\/\/conda.anaconda.org\/conda-forge\/noarch\/tzdata-202xx-he74cb21_0.tar.bz2\n\n------snip------<\/code><\/pre>\n<p class=\"wp-block-paragraph\" id=\"a2a9\">Note that the\u00a0<code>--explicit<\/code>\u00a0flag ensures that the targeted platform is annotated in the file, in this case,\u00a0<em># platform: win-64<\/em>\u00a0in the third line.<\/p>\n<p class=\"wp-block-paragraph\" id=\"e5c1\">You can now remove the environment as described in the previous section.<\/p>\n<p class=\"wp-block-paragraph\" id=\"4e63\">To re-create\u00a0<em>my_env<\/em>\u00a0using this text file, run the following with a proper directory path:<\/p>\n<pre class=\"wp-block-prismatic-blocks\"><code class=\"language-bash\">conda create -n my_env -f directorypathtoexp_spec_list.txt<\/code><\/pre>\n<h3 class=\"wp-block-heading\" id=\"3f3b\">4. Archiving Environments with conda-pack<\/h3>\n<p class=\"wp-block-paragraph\" id=\"87f6\">The\u00a0<code>conda-pack<\/code>\u00a0command lets you archive a conda environment before removing it. It packs the entire environment into a compressed archive with the extension:\u00a0<em>.tar.gz<\/em>. It\u2019s handy for backing up, sharing, and moving environments without the need to reinstall packages.<\/p>\n<p class=\"wp-block-paragraph\" id=\"606d\">The following command will preserve an environment but remove it from your system (where\u00a0<em>my_env<\/em>\u00a0represents the name of your environment):<\/p>\n<pre class=\"wp-block-prismatic-blocks\"><code class=\"language-bash\">conda install -c conda-forge conda-pack\nconda pack -n my_env -o my_env.tar.gz<\/code><\/pre>\n<p class=\"wp-block-paragraph\" id=\"c9f7\">To restore the environment later run this command:<\/p>\n<pre class=\"wp-block-prismatic-blocks\"><code class=\"language-bash\">mkdir my_env &amp;&amp; tar -xzf my_env.tar.gz -C my_env<\/code><\/pre>\n<p class=\"wp-block-paragraph\" id=\"4384\">This technique won\u2019t save as much memory as the text file option. However, you won\u2019t need to re-download packages when restoring an environment, which means it can be used without internet access.<\/p>\n<h3 class=\"wp-block-heading\" id=\"3c36\">5. Storing Environments on an External Drive<\/h3>\n<p class=\"wp-block-paragraph\" id=\"843c\">By default, conda stores all environments in a default location. For Windows, this is under the\u00a0<em>\u2026anaconda3envs<\/em>\u00a0folder. You can see these environments by running the command\u00a0<code>conda info --envs<\/code>\u00a0in a prompt window or terminal. Here\u2019s how it looks on my C: drive (this is a truncated view):<\/p>\n<figure class=\"wp-block-image size-full\"><img data-recalc-dims=\"1\" data-dominant-color=\"d7d7d7\" data-has-transparency=\"false\" style=\"--dominant-color: #d7d7d7;\" loading=\"lazy\" decoding=\"async\" width=\"646\" height=\"210\" src=\"https:\/\/i0.wp.com\/towardsdatascience.com\/wp-content\/uploads\/2025\/02\/1_oYmWw5A-WftZOFp3NpvoUA.webp?resize=646%2C210&#038;ssl=1\" alt=\"\" class=\"wp-image-598226 not-transparent\" srcset=\"https:\/\/towardsdatascience.com\/wp-content\/uploads\/2025\/02\/1_oYmWw5A-WftZOFp3NpvoUA.webp 646w, https:\/\/towardsdatascience.com\/wp-content\/uploads\/2025\/02\/1_oYmWw5A-WftZOFp3NpvoUA-300x98.webp 300w\" sizes=\"auto, (max-width: 646px) 100vw, 646px\"><figcaption class=\"wp-element-caption\">Truncated view of conda environments on my C: drive (by author)<\/figcaption><\/figure>\n<p class=\"wp-block-paragraph\" id=\"11fd\"><strong>Using a Single Environments Folder:<\/strong>\u00a0If your system supports an external or secondary drive, you can configure conda to store environments there to free up space on your primary disk. Here\u2019s the command; you\u2019ll need to substitute your specific path:<\/p>\n<pre class=\"wp-block-prismatic-blocks\"><code class=\"language-bash\">conda config --set envs_dirs \/path\/to\/external\/drive<\/code><\/pre>\n<p class=\"wp-block-paragraph\" id=\"0c73\">If you enter a path to your D drive, such as\u00a0<em>D:conda_envs<\/em>, conda will create new environments at this location.<\/p>\n<p class=\"wp-block-paragraph\" id=\"00b2\">This technique works well when your external drive is a fast SSD and when you\u2019re storing packages with large dependencies, like TensorFlow. The downside is slower performance. If your OS and notebooks remain on the primary drive, you may experience some read\/write latency when running Python.<\/p>\n<p class=\"wp-block-paragraph\" id=\"c497\">In addition, some OS settings may power down idle external drives, adding a delay when they spin back up. Tools like Jupyter may struggle to locate conda environments if the drive letter changes, so you\u2019ll want to use a fixed drive letter and ensure that the correct kernel paths are set.<\/p>\n<p class=\"wp-block-paragraph\" id=\"869f\"><strong>Using Multiple Environment Folders:<\/strong>\u00a0Instead of using a single\u00a0<code>envs_dirs<\/code>\u00a0directory for\u00a0<em>all<\/em>\u00a0environments, you can store each environment inside its respective\u00a0<em>project<\/em>\u00a0folder. This lets you store everything related to a project in one place.<\/p>\n<figure class=\"wp-block-image size-full\"><img data-recalc-dims=\"1\" data-dominant-color=\"f6f3ed\" data-has-transparency=\"false\" style=\"--dominant-color: #f6f3ed;\" loading=\"lazy\" decoding=\"async\" width=\"201\" height=\"194\" src=\"https:\/\/i0.wp.com\/towardsdatascience.com\/wp-content\/uploads\/2025\/02\/1_wlHf7VfilrmBLZEJnZ8aVQ.webp?resize=201%2C194&#038;ssl=1\" alt=\"\" class=\"wp-image-598227 not-transparent\"><figcaption class=\"wp-element-caption\">Example project file structure with embedded (1.7 GB) conda environment (opencv_env) (by author)<\/figcaption><\/figure>\n<p class=\"wp-block-paragraph\" id=\"b075\">For example, suppose you have a project on your Windows D: drive in a folder called\u00a0<em>D:projectsgeospatial<\/em>. To place the project\u2019s conda environment in this folder, loaded with\u00a0<code>ipykernel<\/code>\u00a0for JupyterLab, you would run:<\/p>\n<pre class=\"wp-block-prismatic-blocks\"><code class=\"language-bash\">conda create -p D:projectsgeospatialenv ipykernel<\/code><\/pre>\n<p class=\"wp-block-paragraph\" id=\"c30d\">Of course, you can call\u00a0<em>env<\/em>\u00a0something more descriptive, like\u00a0<em>geospatial_env<\/em>.<\/p>\n<p class=\"wp-block-paragraph\" id=\"3b89\">As with the previous example, environments stored on a different disk can cause performance issues.<\/p>\n<p class=\"wp-block-paragraph\" id=\"057b\"><strong>Special Note on JupyterLab:<\/strong>\u00a0Depending on how you launch JupyterLab, its default behavior may be to open in your\u00a0<em>user<\/em>\u00a0directory (such as,\u00a0<em>C:Usersyour_user_name<\/em>). Since its file browser is restricted to the directory from which it is launched, you won\u2019t see directories on other drives like\u00a0<code>D:<\/code>. There are many ways to handle this, but one of the simplest is to launch JupyterLab from the D: drive.<\/p>\n<p class=\"wp-block-paragraph\" id=\"2424\">For example, in Anaconda Prompt, type:<\/p>\n<pre class=\"wp-block-prismatic-blocks\"><code class=\"language-\">D:<\/code><\/pre>\n<p class=\"wp-block-paragraph\" id=\"5d79\">followed by:<\/p>\n<pre class=\"wp-block-prismatic-blocks\"><code class=\"language-\">jupyter lab<\/code><\/pre>\n<p class=\"wp-block-paragraph\" id=\"e8bb\">Now, you will be able to pick from kernels on the D: drive.<\/p>\n<p class=\"wp-block-paragraph\" id=\"1681\">For more options on changing JupyterLab\u2019s working directory, ask an AI about \u201chow to change Jupyter\u2019s default working directory\u201d or \u201chow to create a Symlink to\u00a0<code>D:<\/code>\u00a0in your user folder.\u201d<\/p>\n<p class=\"wp-block-paragraph\" id=\"3005\"><strong>Moving Existing Environments:<\/strong>\u00a0You should never manually move a conda environment, such as by cutting and pasting to a new location. This is because conda relies on internal paths and metadata that can become invalid with location changes.<\/p>\n<p class=\"wp-block-paragraph\" id=\"0e77\">Instead, you should\u00a0<em>clone\u00a0<\/em>existing environments to another drive. This will\u00a0<em>duplicate<\/em>\u00a0the environment, so you\u2019ll need to manually remove it from its original location.<\/p>\n<p class=\"wp-block-paragraph\" id=\"98f8\">In the following example, we use the\u00a0<code>--clone<\/code>\u00a0flag to produce an exact copy of a C: drive environment (called\u00a0<em>my_env<\/em>) on the D: drive:<\/p>\n<pre class=\"wp-block-prismatic-blocks\"><code class=\"language-bash\">conda create -p D:new_envsmy_env --clone C:pathtooldenv<\/code><\/pre>\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p class=\"wp-block-paragraph\" id=\"8c72\"><strong>NOTE:<\/strong>\u00a0Consider exporting your environment to a\u00a0<em>YAML<\/em>\u00a0file (as described in Section 3 above) before cloning. This allows you to recreate the environment if something goes wrong with the clone procedure.<\/p>\n<\/blockquote>\n<p class=\"wp-block-paragraph\" id=\"5803\">Now, when you run\u00a0<code>conda env list<\/code>, you\u2019ll see the environment listed in both the C: and D: drives. You can remove the old environment by running the following command in the\u00a0<em>base\u00a0<\/em>environment:<\/p>\n<pre class=\"wp-block-prismatic-blocks\"><code class=\"language-bash\">conda remove --name my_env --all -y<\/code><\/pre>\n<p class=\"wp-block-paragraph\" id=\"dd53\">Again, latency issues may affect these setups if you\u2019re working across two disks.<\/p>\n<p class=\"wp-block-paragraph\" id=\"e469\">You may be wondering, is it better to move a conda environment using an environment (YAML) file or to use<code>--clone<\/code>? The short answer is that\u00a0<code>--clone<\/code>\u00a0is the best and fastest option for moving an environment to a different drive on the\u00a0<em>same<\/em>\u00a0machine. An environment file is best for recreating the same environment on a\u00a0<em>different\u00a0<\/em>machine. While the file guarantees a consistent environment across different systems, it can take much longer to run, especially with large environments.<\/p>\n<h3 class=\"wp-block-heading\" id=\"e469\">6. Relocating the Package Cache<\/h3>\n<p class=\"wp-block-paragraph\" id=\"3773\">If your primary drive is low on space, you can move the package cache to a larger external or secondary drive using this command:<\/p>\n<pre class=\"wp-block-prismatic-blocks\"><code class=\"language-bash\">conda config --set pkgs_dirs D:conda_pkgs<\/code><\/pre>\n<p class=\"wp-block-paragraph\" id=\"4637\">In this example, packages are now stored on the D drive (<em>D:conda_pkgs<\/em>) instead of the default location.<\/p>\n<p class=\"wp-block-paragraph\" id=\"3db3\">If you\u2019re working in your primary drive and both drives are SSD, then latency issues should not be significant. However, if one of the drives is a slower HDD, you can experience slowdowns when creating or updating environments. If D: is an external drive connected by USB, you may see significant slowdowns for large environments.<\/p>\n<p class=\"wp-block-paragraph\" id=\"363b\">You can mitigate some of these issues by keeping the package cache (<code>pkgs_dirs<\/code>) and frequently used environments on the faster SSD, and other environments on the slower HDD.<\/p>\n<p class=\"wp-block-paragraph\" id=\"8336\">One last thing to consider is\u00a0<em>backups<\/em>. Primary drives may have routine backups scheduled but secondary or external drives may not. This puts you at risk of losing all your environments.<\/p>\n<h3 class=\"wp-block-heading\" id=\"975b\">7. Using Virtual Environments<\/h3>\n<p class=\"wp-block-paragraph\" id=\"63ba\">If your project doesn\u2019t require conda\u2019s extensive package management system for handling heavy dependencies (like TensorFlow or GDAL), you can significantly reduce disk usage with a Python\u00a0<em>virtual environment<\/em>\u00a0(<code>venv<\/code>). This represents a lightweight alternative to a conda environment.<\/p>\n<p class=\"wp-block-paragraph\" id=\"3ce6\">To create a\u00a0<code>venv<\/code>\u00a0named\u00a0<em>my_env<\/em>, run the following command:<\/p>\n<figure class=\"wp-block-image size-large\"><img data-recalc-dims=\"1\" data-dominant-color=\"f5f5f5\" data-has-transparency=\"false\" style=\"--dominant-color: #f5f5f5;\" loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"94\" src=\"https:\/\/i0.wp.com\/towardsdatascience.com\/wp-content\/uploads\/2025\/02\/Screenshot-2025-02-20-at-1.28.15%25E2%2580%25AFPM-1024x94.png?resize=1024%2C94&#038;ssl=1\" alt=\"\" class=\"wp-image-598231 not-transparent\" srcset=\"https:\/\/towardsdatascience.com\/wp-content\/uploads\/2025\/02\/Screenshot-2025-02-20-at-1.28.15\u202fPM-1024x94.png 1024w, https:\/\/towardsdatascience.com\/wp-content\/uploads\/2025\/02\/Screenshot-2025-02-20-at-1.28.15\u202fPM-300x28.png 300w, https:\/\/towardsdatascience.com\/wp-content\/uploads\/2025\/02\/Screenshot-2025-02-20-at-1.28.15\u202fPM-768x71.png 768w, https:\/\/towardsdatascience.com\/wp-content\/uploads\/2025\/02\/Screenshot-2025-02-20-at-1.28.15\u202fPM.png 1346w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\"><\/figure>\n<p class=\"wp-block-paragraph\" id=\"807b\">This type of environment has a small base installation. A minimal conda environment takes up about 200 MB and includes multiple utilities, such as\u00a0<code>conda<\/code>,\u00a0<code>pip<\/code>,\u00a0<code>setuptools<\/code>, and so on. A\u00a0<code>venv<\/code>\u00a0is much lighter, with a minimum install size of only 5\u201310 MB.<\/p>\n<p class=\"wp-block-paragraph\" id=\"35cb\">Conda also caches package tarballs in\u00a0<code>pkgs_dirs<\/code>. These tarballs can grow to several GBs over time. Because\u00a0<code>venv<\/code>\u00a0installs packages directly into the environment, no extra copies are preserved.<\/p>\n<p class=\"wp-block-paragraph\" id=\"117d\">In general, you\u2019ll want to consider\u00a0<code>venv<\/code>\u00a0when you only need basic Python packages like NumPy, pandas, or Scikit-learn. Packages for which conda is strongly recommended, like Geopandas, should still be placed in a conda environment. If you use lots of environments, you\u2019ll probably want to stick with conda and benefit from its package linking.<\/p>\n<p class=\"wp-block-paragraph\" id=\"3ce6\">You can find details on how to activate and use Python virtual environments in the\u00a0<code>venv<\/code>\u00a0<a href=\"https:\/\/docs.python.org\/3\/library\/venv.html\" target=\"_blank\" rel=\"noreferrer noopener\">docs<\/a>.<\/p>\n<hr class=\"wp-block-separator has-alpha-channel-opacity is-style-dotted\">\n<h2 class=\"wp-block-heading\" id=\"987c\">Recap<\/h2>\n<p class=\"wp-block-paragraph\" id=\"918c\">High impact\/low disruption memory management techniques for conda environments include cleaning the package cache and storing little-used environments as YAML or text files. These methods can save many gigabytes of memory while retaining Anaconda\u2019s default directory structure.<\/p>\n<p class=\"wp-block-paragraph\" id=\"d630\">Other high impact methods include moving the package cache and\/or conda environments to a secondary or external drive. This will resolve memory problems but may introduce latency issues, especially if the new drive is a slow HDD or uses a USB connection.<\/p>\n<p class=\"wp-block-paragraph\" id=\"e469\">For simple environments, you can use a Python virtual environment (<code>venv<\/code>) as a lightweight alternative to conda.<a href=\"https:\/\/medium.com\/@lee_vaughan?source=post_page---post_author_info--3aa2791a7623---------------------------------------\"><\/a><\/p>\n<p>The post <a href=\"https:\/\/towardsdatascience.com\/dont-let-conda-eat-your-hard-drive\/\">Don\u2019t Let Conda Eat Your Hard Drive<\/a> appeared first on <a href=\"https:\/\/towardsdatascience.com\/\">Towards Data Science<\/a>.<\/p>\n<\/div>\n<p> \t<BR><br \/>\n <BR><\/BR><br \/>\n    Lee Vaughan<br \/>\n \t<BR><br \/>\n<BR><\/BR><br \/>\n<a href=\"https:\/\/towardsdatascience.com\/dont-let-conda-eat-your-hard-drive\/\">Go to original source<\/a><br \/>\n \t<BR><br \/>\n <BR><\/BR><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Don\u2019t Let Conda Eat Your Hard Drive If you\u2019re an Anaconda user, you know that\u00a0conda environments\u00a0help you manage package dependencies, avoid compatibility conflicts, and share your projects with others. Unfortunately, they can also take over your computer\u2019s hard drive. I write lots of computer tutorials and to keep them organized, each has a dedicated folder [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[62,1808,1809,401,83,1810,160],"tags":[1811,1812,739],"class_list":["post-1979","post","type-post","status-publish","format-standard","hentry","category-aimldsaimlds","category-conda","category-conda-environment","category-data-engineering","category-data-science","category-memory-management","category-programming","tag-conda","tag-environments","tag-package"],"_links":{"self":[{"href":"https:\/\/mailitics.com\/index.php\/wp-json\/wp\/v2\/posts\/1979"}],"collection":[{"href":"https:\/\/mailitics.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mailitics.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mailitics.com\/index.php\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/mailitics.com\/index.php\/wp-json\/wp\/v2\/comments?post=1979"}],"version-history":[{"count":0,"href":"https:\/\/mailitics.com\/index.php\/wp-json\/wp\/v2\/posts\/1979\/revisions"}],"wp:attachment":[{"href":"https:\/\/mailitics.com\/index.php\/wp-json\/wp\/v2\/media?parent=1979"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mailitics.com\/index.php\/wp-json\/wp\/v2\/categories?post=1979"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mailitics.com\/index.php\/wp-json\/wp\/v2\/tags?post=1979"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}