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

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.

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

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

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

To test, open a new terminal and

1 Like

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).

You need to specify the entire key:

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

You were missing the DIR part.

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.

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

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

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.

I did. It works perfectly.

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

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.

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

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

~/GitHub/ main !4 ?1 ❯ export HUGO_CACHES_MODULES_DIR=$HOME/.local/share/hugo              5s
~/GitHub/ main !4 ?1 ❯ hugo version                                          
hugo v0.92.2-CDF6A0D6+extended linux/amd64 BuildDate=2022-02-11T14:17:39Z VendorInfo=gohugoio
~/GitHub/ 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/{,hugo-yoru}
Watching for changes in /tmp/hugo_cache/modules/filecache/modules/pkg/mod/!utkarsh!verma/hugo-modules/bootstrap-v5@v0.0.0-20220106145939-09cc0f355a13/_vendor/{index.d.ts,lib,package.json}
Watching for config changes in /home/subaru/GitHub/, /home/subaru/GitHub/hugo-yoru/config/_default, /home/subaru/GitHub/
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
Press Ctrl+C to stop
~/GitHub/ main !4 ?1 ❯ echo $HUGO_CACHES_MODULES_DIR                         
~/GitHub/ main !4 ?1 ❯ ls -lha 
~/GitHub/ main !4 ?1 ❯ tree $HUGO_CACHES_MODULES_DIR                                    ✘ INT

0 directories, 0 files

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?

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.

  hugo server