# Caching

> How to configure caching for your builds



Caching can improve the efficiency of your build and deployment processes on Codemagic by reusing components that are generated or fetched during the build, such as packages, libraries, and compiled code.

When you have enabled caching for a workflow, Codemagic will automatically generate a cache based on the output of the first successful build. This cache is stored for a maximum of 14 days. After 14 days, the cache expires and is no longer accessible for subsequent builds. When this occurs, your workflow will fetch all dependencies and artifacts again and generate a new cache. This new cache is then uploaded and can be used for the next 14 days, following the same process.

For example, you may consider caching the following paths:

| **Path**                                    | **Description**                                  |
| ------------------------------------------- | ------------------------------------------------ |
| `$HOME/.pub-cache`                          | Dart cache                                       |
| `$HOME/.gradle/caches`                      | Gradle cache. Note: do not cache `$HOME/.gradle` |
| `$HOME/Library/Caches/CocoaPods`            | CocoaPods cache                                  |

A great article on Unity caching can be found in [our blog](https://blog.codemagic.io/unity-caching/).




> 
> **Note:** Caching `$HOME/Library/Developer/Xcode/DerivedData` won't help to speed up iOS builds with Xcode 10.2 or later.
> 



## Enabling dependency caching





### Option: codemagic.yaml



To use caching, simply add a `cache` section to workflows you would like to have caching enabled for in the `codemagic.yaml` file and list the paths you would like to cache.


```yaml

workflows:
  example-workflow:
    cache:
      cache_paths:
        - ~/.gradle/caches
        - ...

```






### Option: Flutter WFE



1. In your app settings, open the **Dependency caching** section.
2. Check the **Enable dependency caching** option. By default, caching is disabled.
3. Enter the path(s) to the dependencies to be cached and click **Add**. Note that you can delete added paths anytime.







> 
> **⚠️ Note:** If your dependencies have any warnings or errors when trying to cache them, the caching process will not complete successfully even though you will see a cache for the workflow listed under the Caching tab in the UI. You should reset the cache by deleting the workflow cache under the Caching tab and deal with any errors or warnings.
> 



## Cache usage limits

Maximum cache size is limited to
- **10GB** per workflow for teams, and 
- **3GB** per workflow for personal accounts.

Note that installing dependencies without using caching could be faster than retrieving or updating cached data.

## Removing cached dependencies

In order to clear the collected cache, navigate to the **Dependency caching** section in app settings and click **Clear cache**. During the next build, dependencies will be downloaded afresh.

## Xcode compilation caching 

Starting with Xcode 26 it is possible to use **compilation caching**. This is a new build system feature designed to make builds significantly faster by caching and reusing compilation outputs across different builds.

On macOS, the compilation cache can usually be found in `~/Library/Developer/Xcode/DerivedData/CompilationCache.noindex`.

For Codemagic to reuse the compilation cache you should add the its path to your cache paths in your `codemagic.yaml` configuration file as follows:


```yaml

  cache:
    cache_paths:
      - ~/Library/Developer/Xcode/DerivedData/CompilationCache.noindex

```


When building and exporting your `.ipa` with Codemagic's CLI tools you can ensure the compilation cache is used during builds by adding the `COMPILATION_CACHE_ENABLE_CACHING=True` flag as follows:


```yaml

  - name: Build ipa for distribution
    script: | 
      xcode-project build-ipa \
        --workspace "${XCODE_SCHEME}.xcworkspace" \
        --scheme "${XCODE_SCHEME}" \
        --archive-xcargs "COMPILATION_CACHE_ENABLE_CACHING=True"

```


The first build will generate the cache which will be uploaded to Codemagic's cache server. On subsequent builds Codemagic restores the directory containing the compilation cache which should speed up the build.

You can see if the compilation cache is being used by looking at the Xcode build logs.


```bash

CompilationCacheMetrics
note: 85 hits / 85 cacheable tasks (100%)

```


Xcode build logs can be gathered as artifacts by setting the path to the logs in the `artifacts` section of your `codemagic.yaml` configuration file.


```yaml

artifacts:
  - /tmp/xcodebuild_logs/*.log

```


## Swift Package Manager caching

Swift Package Manager (SPM) is Apple's official dependency manager for Swift projects. By caching SPM dependencies in your workflow, subsequent builds can reuse previously fetched packages rather than downloading them again, resulting in faster build times.

You can cache the **global SPM cache** which is shared across all projects on the machine


```yaml

  cache:
    cache_paths:
      - ~/Library/Caches/org.swift.swiftpm 

```


Alternatively, when building and exporting your `.ipa` with Codemagic's CLI tools you can cache **project level SPM dependencies** by setting the path to the `SourcePackages` directory as follows:


```yaml

  - name: Build ipa for distribution
    script: | 
      xcode-project build-ipa \
        --project "$XCODE_PROJECT" \
        --scheme "$XCODE_SCHEME" \
        --archive-flags "-clonedSourcePackagesDirPath $CM_BUILD_DIR/SourcePackages"

```


You can then set your cache paths as follows:


```yaml

  cache:
    cache_paths:
    - $CM_BUILD_DIR/SourcePackages

```




  