# How do I set module cache directory to \`~/.local/share/hugo\` globally?

**URL:** <https://discourse.gohugo.io/t/how-do-i-set-module-cache-directory-to-local-share-hugo-globally/37118>\
**Category:** support\
**Created:** [February 12, 2022, 7:14am UTC](https://discourse.gohugo.io/t/how-do-i-set-module-cache-directory-to-local-share-hugo-globally/37118 "2022-02-12T07:14:58Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![UtkarshVerma](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/utkarshverma/32/11273_2.png) [@UtkarshVerma](https://discourse.gohugo.io/u/UtkarshVerma)\
**Post date:** [February 12, 2022, 7:14am UTC](https://discourse.gohugo.io/t/how-do-i-set-module-cache-directory-to-local-share-hugo-globally/37118/1 "2022-02-12T07:14:58Z")

</div>

Downloading Hugo modules over and over again (after reboots) is something I want to avoid. I can see that there is a config variable for changing the Hugo module cache, but I don’t know how to set this globally.

> **[Configure Hugo](https://gohugo.io/getting-started/configuration/#configure-file-caches)**
>
> How to configure your Hugo site.

---

<div class="post-metadata">

**Author:** ![jmooring](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/jmooring/32/4214_2.png) [@jmooring](https://discourse.gohugo.io/u/jmooring)\
**Post date:** [February 12, 2022, 8:07am UTC](https://discourse.gohugo.io/t/how-do-i-set-module-cache-directory-to-local-share-hugo-globally/37118/2 "2022-02-12T08:07:25Z")

</div>

Set the `HUGO_CACHEDIR` environment variable. The “how” depends on your OS.

On Ubuntu, add this to bottom of $HOME/.bashrc:

```bash
export HUGO_CACHEDIR=$HOME/.local/share/hugo

```

To test, open a new terminal and

```bash
echo $HUGO_CACHEDIR

```

---

<div class="post-metadata">

**Author:** ![UtkarshVerma](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/utkarshverma/32/11273_2.png) [@UtkarshVerma](https://discourse.gohugo.io/u/UtkarshVerma)\
**Post date:** [February 12, 2022, 8:34am UTC](https://discourse.gohugo.io/t/how-do-i-set-module-cache-directory-to-local-share-hugo-globally/37118/3 "2022-02-12T08:34:42Z")

</div>

Thanks for your suggestion. But I want something more fine-grained. Setting HUGO\_CACHEDIR also writes build cache to disk which is what I’d like to avoid.

Earlier (v0.90), I used to have HUGO\_CACHES\_MODULES set but that functionality seems to have gone in current version (0.92.1).

---

<div class="post-metadata">

**Author:** ![jmooring](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/jmooring/32/4214_2.png) [@jmooring](https://discourse.gohugo.io/u/jmooring)\
**Post date:** [February 12, 2022, 8:54am UTC](https://discourse.gohugo.io/t/how-do-i-set-module-cache-directory-to-local-share-hugo-globally/37118/4 "2022-02-12T08:54:26Z")

</div>

You need to specify the entire key:

```auto
export HUGO_CACHES_MODULES_DIR=$HOME/.local/share/hugo

```

You were missing the DIR part.

---

<div class="post-metadata">

**Author:** ![davidsneighbour](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/davidsneighbour/32/24507_2.png) [@davidsneighbour](https://discourse.gohugo.io/u/davidsneighbour)\
**Post date:** [February 12, 2022, 11:32am UTC](https://discourse.gohugo.io/t/how-do-i-set-module-cache-directory-to-local-share-hugo-globally/37118/5 "2022-02-12T11:32:12Z")

</div>

My Hugo modules are not downloading after reboots, only if they are updated. Maybe something more global is afoot?

Other than that, have you looked into “vendoring” where you load your modules into a vendor folder? That is more or less used to add a “node\_modules-type” directory for golang modules to your repo.

> **[hugo mod vendor](https://gohugo.io/commands/hugo_mod_vendor/)**
>
> hugo mod vendor Vendor all module dependencies into the \_vendor directory.
> Synopsis Vendor all module dependencies into the \_vendor directory.
> If a module is vendored, that is where Hugo will look for it’s dependencies.
> hugo mod vendor \[flags\]...

You can configure your modules to allow only vendoring certain folders/files or nothing at all.

---

<div class="post-metadata">

**Author:** ![UtkarshVerma](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/utkarshverma/32/11273_2.png) [@UtkarshVerma](https://discourse.gohugo.io/u/UtkarshVerma)\
**Post date:** [February 12, 2022, 11:36am UTC](https://discourse.gohugo.io/t/how-do-i-set-module-cache-directory-to-local-share-hugo-globally/37118/6 "2022-02-12T11:36:00Z")

</div>

Hugo(v0.92.1+extended) doesn’t seem to pick this up. Could you please test this on your end?

---

<div class="post-metadata">

**Author:** ![UtkarshVerma](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/utkarshverma/32/11273_2.png) [@UtkarshVerma](https://discourse.gohugo.io/u/UtkarshVerma)\
**Post date:** [February 12, 2022, 11:39am UTC](https://discourse.gohugo.io/t/how-do-i-set-module-cache-directory-to-local-share-hugo-globally/37118/7 "2022-02-12T11:39:05Z")

</div>

For me, modules are downloaded in `/tmp/hugo_cache/modules`. On my machine, tmpfs is mounted on the RAM, which is cleared after every reboot.

---

<div class="post-metadata">

**Author:** ![jmooring](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/jmooring/32/4214_2.png) [@jmooring](https://discourse.gohugo.io/u/jmooring)\
**Post date:** [February 12, 2022, 11:39am UTC](https://discourse.gohugo.io/t/how-do-i-set-module-cache-directory-to-local-share-hugo-globally/37118/8 "2022-02-12T11:39:46Z")

</div>

I did. It works perfectly.

Remember, you have to start a new terminal session after making the change.

---

<div class="post-metadata">

**Author:** ![davidsneighbour](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/davidsneighbour/32/24507_2.png) [@davidsneighbour](https://discourse.gohugo.io/u/davidsneighbour)\
**Post date:** [February 12, 2022, 11:42am UTC](https://discourse.gohugo.io/t/how-do-i-set-module-cache-directory-to-local-share-hugo-globally/37118/9 "2022-02-12T11:42:21Z")

</div>

The solution @jmooring described will work only if the folder up to `~/.local/share/`exist and Hugo has writing access. That might be the missing link. Especially with Snap-Hugo.

---

<div class="post-metadata">

**Author:** ![UtkarshVerma](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/utkarshverma/32/11273_2.png) [@UtkarshVerma](https://discourse.gohugo.io/u/UtkarshVerma)\
**Post date:** [February 12, 2022, 12:24pm UTC](https://discourse.gohugo.io/t/how-do-i-set-module-cache-directory-to-local-share-hugo-globally/37118/10 "2022-02-12T12:24:05Z")

</div>

I’m using Hugo from Arch’s official repo. I even tried the binary on GitHub releases, with the same behaviour.

---

<div class="post-metadata">

**Author:** ![UtkarshVerma](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/utkarshverma/32/11273_2.png) [@UtkarshVerma](https://discourse.gohugo.io/u/UtkarshVerma)\
**Post date:** [February 12, 2022, 12:25pm UTC](https://discourse.gohugo.io/t/how-do-i-set-module-cache-directory-to-local-share-hugo-globally/37118/11 "2022-02-12T12:25:00Z")

</div>

That’s weird. I have set the environment variable properly, but it still doesn’t work. Here’s my shell session.

```auto
~/GitHub/bitbanged.com main !4 ?1 ❯ export HUGO_CACHES_MODULES_DIR=$HOME/.local/share/hugo 5s
~/GitHub/bitbanged.com main !4 ?1 ❯ hugo version                                          
hugo v0.92.2-CDF6A0D6+extended linux/amd64 BuildDate=2022-02-11T14:17:39Z VendorInfo=gohugoio
~/GitHub/bitbanged.com main !4 ?1 ❯ hugo server                
Start building sites … 
hugo v0.92.2-CDF6A0D6+extended linux/amd64 BuildDate=2022-02-11T14:17:39Z VendorInfo=gohugoio

                   | EN   
-------------------+------
  Pages | 85  
  Paginator pages | 0  
  Non-page files | 63  
  Static files | 0  
  Processed images | 141  
  Aliases | 0  
  Sitemaps | 1  
  Cleaned | 0  

Built in 937 ms
Watching for changes in /home/subaru/GitHub/{bitbanged.com,hugo-yoru}
Watching for changes in /tmp/hugo_cache/modules/filecache/modules/pkg/mod/github.com/!utkarsh!verma/hugo-modules/bootstrap-v5@v0.0.0-20220106145939-09cc0f355a13/_vendor/github.com/popperjs/popper-core/{index.d.ts,lib,package.json}
Watching for config changes in /home/subaru/GitHub/bitbanged.com/config.yaml, /home/subaru/GitHub/hugo-yoru/config/_default, /home/subaru/GitHub/bitbanged.com/go.mod
Environment: "development"
Serving pages from memory
Running in Fast Render Mode. For full rebuilds on change: hugo server --disableFastRender
Web Server is available at http://localhost:1313/ (bind address 127.0.0.1)
Press Ctrl+C to stop
^C%                                                                                                        
~/GitHub/bitbanged.com main !4 ?1 ❯ echo $HUGO_CACHES_MODULES_DIR                         
/home/subaru/.local/share/hugo
~/GitHub/bitbanged.com main !4 ?1 ❯ ls -lha 
~/GitHub/bitbanged.com main !4 ?1 ❯ tree $HUGO_CACHES_MODULES_DIR ✘ INT
/home/subaru/.local/share/hugo

0 directories, 0 files

```

---

<div class="post-metadata">

**Author:** ![UtkarshVerma](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/utkarshverma/32/11273_2.png) [@UtkarshVerma](https://discourse.gohugo.io/u/UtkarshVerma)\
**Post date:** [February 12, 2022, 12:43pm UTC](https://discourse.gohugo.io/t/how-do-i-set-module-cache-directory-to-local-share-hugo-globally/37118/12 "2022-02-12T12:43:54Z")

</div>

I think I’ve noticed the cause of all this. I think it is being caused by `replace` rules in my `go.mod` which causes modules to default to `/tmp/hugo_cache`.

Is this an expected behaviour or should I create an issue for this?

---

<div class="post-metadata">

**Author:** ![davidsneighbour](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/davidsneighbour/32/24507_2.png) [@davidsneighbour](https://discourse.gohugo.io/u/davidsneighbour)\
**Post date:** [February 12, 2022, 12:55pm UTC](https://discourse.gohugo.io/t/how-do-i-set-module-cache-directory-to-local-share-hugo-globally/37118/13 "2022-02-12T12:55:55Z")

</div>

I experienced issues with Hugo when having a redirect rule without existing target folder (the redirected module) some weeks back and moved to define them via environment variable now. Not sure if that helps, but there might be a deeper issue here.

And a second thing: in my scripts I am using the env command, not just an export. Maybe hugo is blind to your console session (my sample is the replacements, but could apply to the directory env too? Note that there is no `&&` in between.

```auto
env HUGO_MODULE_REPLACEMENTS="${HUGO_MODULE_REPLACEMENTS}" \
  hugo server

```
