Debian Codegraph @

docs/content/en/configuration/introduction.md

raw
1 ---
2 title: Introduction
3 description: Configure your site using files, directories, and environment variables.
4 categories: []
5 keywords: []
6 weight: 10
7 ---
8
9 ## Sensible defaults
10
11 Hugo offers many configuration settings, but its defaults are often sufficient. A new project requires only these settings:
12
13 {{< code-toggle file=hugo >}}
14 baseURL = 'https://example.org/'
15 locale = 'en-us'
16 title = 'My New Hugo Site'
17 {{< /code-toggle >}}
18
19 Only define settings that deviate from the defaults. A smaller configuration file is easier to read, understand, and debug. Keep your configuration concise.
20
21 > [!NOTE]
22 > The best configuration file is a short configuration file.
23
24 ## Configuration file
25
26 Create a project configuration file in the root of your project directory, naming it `hugo.toml`, `hugo.yaml`, or `hugo.json`, with that order of precedence.
27
28 ```tree
29 my-project/
30 └── hugo.toml
31 ```
32
33 A simple example:
34
35 {{< code-toggle file=hugo >}}
36 baseURL = 'https://example.org/'
37 locale = 'en-us'
38 title = 'ABC Widgets, Inc.'
39 [params]
40 subtitle = 'The Best Widgets on Earth'
41 [params.contact]
42 email = '[email protected]'
43 phone = '+1 202-555-1212'
44 {{< /code-toggle >}}
45
46 To use a different configuration file when building your project, use the `--config` flag:
47
48 ```sh
49 hugo build --config other.toml
50 ```
51
52 Combine two or more configuration files, with left-to-right precedence:
53
54 ```sh
55 hugo build --config a.toml,b.yaml,c.json
56 ```
57
58 > [!NOTE]
59 > See the specifications for each file format: [TOML][], [YAML][], and [JSON][].
60
61 ## Configuration directory
62
63 Instead of a single project configuration file, split your configuration by [environment](g), root configuration key, and language. For example:
64
65 ```tree
66 my-project/
67 └── config/
68 ├── _default/
69 │ ├── hugo.toml
70 │ ├── menus.en.toml
71 │ ├── menus.de.toml
72 │ └── params.toml
73 └── production/
74 └── params.toml
75 ```
76
77 The root configuration keys are {{< root-configuration-keys >}}.
78
79 ### Root key
80
81 {{< new-in 0.162.0 />}}
82
83 When splitting the configuration by root key, you may omit or include the root key in the component file. For example, these are equivalent:
84
85 {{< code-toggle file=config/_default/hugo >}}
86 [params]
87 foo = 'bar'
88 {{< /code-toggle >}}
89
90 {{< code-toggle file=config/_default/params >}}
91 foo = 'bar'
92 {{< /code-toggle >}}
93
94 This also applies to keys whose values are maps of slices, such as `menus`. For example, these are equivalent:
95
96 {{< code-toggle file=config/_default/menus >}}
97 [[main]]
98 name = 'Home'
99 pageRef = '/'
100 weight = 10
101 {{< /code-toggle >}}
102
103 {{< code-toggle file=config/_default/menus >}}
104 [[menus.main]]
105 name = 'Home'
106 pageRef = '/'
107 weight = 10
108 {{< /code-toggle >}}
109
110 For pure slice-typed keys such as `cascade` and `permalinks`, including the root key is required. For example:
111
112 {{< code-toggle file=config/_default/cascade >}}
113 [[cascade]]
114 [cascade.params]
115 color = 'red'
116 [cascade.target]
117 path = '/articles/**'
118 {{< /code-toggle >}}
119
120 > [!NOTE]
121 > Hugo unwraps the root key only when it is the sole top-level key in the file and matches the file's basename.
122
123 ### Recursive parsing
124
125 Hugo parses the `config` directory recursively, allowing you to organize the files into subdirectories. For example:
126
127 ```tree
128 my-project/
129 └── config/
130 └── _default/
131 ├── navigation/
132 │ ├── menus.de.toml
133 │ └── menus.en.toml
134 └── hugo.toml
135 ```
136
137 ### Example
138
139 ```tree
140 my-project/
141 └── config/
142 ├── _default/
143 │ ├── hugo.toml
144 │ ├── menus.en.toml
145 │ ├── menus.de.toml
146 │ └── params.toml
147 ├── production/
148 │ ├── hugo.toml
149 │ └── params.toml
150 └── staging/
151 ├── hugo.toml
152 └── params.toml
153 ```
154
155 Considering the structure above, when running `hugo build --environment staging`, Hugo will use every setting from `config/_default` and merge `staging`'s on top of those.
156
157 Let's take an example to understand this better. Let's say you are using Google Analytics for your website. This requires you to specify a [Google tag ID][] in your project configuration:
158
159 {{< code-toggle file=hugo >}}
160 [services.googleAnalytics]
161 ID = 'G-XXXXXXXXX'
162 {{< /code-toggle >}}
163
164 Now consider the following scenario:
165
166 1. You don't want to load the analytics code when running `hugo server`.
167 1. You want to use different Google tag IDs for your production and staging environments. For example:
168 - `G-PPPPPPPPP` for production
169 - `G-SSSSSSSSS` for staging
170
171 To satisfy these requirements, configure your site as follows:
172
173 1. `config/_default/hugo.toml`
174 - Exclude the `services.googleAnalytics` section. This will prevent loading of the analytics code when you run `hugo server`.
175 - By default, Hugo sets its `environment` to `development` when running `hugo server`. In the absence of a `config/development` directory, Hugo uses the `config/_default` directory.
176 1. `config/production/hugo.toml`
177 - Include this section only:
178
179 {{< code-toggle file=hugo >}}
180 [services.googleAnalytics]
181 ID = 'G-PPPPPPPPP'
182 {{< /code-toggle >}}
183
184 - You do not need to include other parameters in this file. Include only those parameters that are specific to your production environment. Hugo will merge these parameters with the default configuration.
185 - By default, Hugo sets its `environment` to `production` when running `hugo build`. The analytics code will use the `G-PPPPPPPPP` tag ID.
186
187 1. `config/staging/hugo.toml`
188
189 - Include this section only:
190
191 {{< code-toggle file=hugo >}}
192 [services.googleAnalytics]
193 ID = 'G-SSSSSSSSS'
194 {{< /code-toggle >}}
195
196 - You do not need to include other parameters in this file. Include only those parameters that are specific to your staging environment. Hugo will merge these parameters with the default configuration.
197 - To build your staging site, run `hugo build --environment staging`. The analytics code will use the `G-SSSSSSSSS` tag ID.
198
199 ## Merge configuration settings
200
201 Hugo merges configuration settings from themes and modules, prioritizing the project's own settings. Given this simplified project structure with two themes:
202
203 ```tree
204 project/
205 ├── themes/
206 │ ├── theme-a/
207 │ │ └── hugo.toml
208 │ └── theme-b/
209 │ └── hugo.toml
210 └── hugo.toml
211 ```
212
213 and this project-level configuration:
214
215 {{< code-toggle file=hugo >}}
216 baseURL = 'https://example.org/'
217 locale = 'en-us'
218 title = 'My New Hugo Site'
219 theme = ['theme-a','theme-b']
220 {{< /code-toggle >}}
221
222 Hugo merges settings in this order:
223
224 1. Project configuration (`hugo.toml` in the project root)
225 1. `theme-a` configuration
226 1. `theme-b` configuration
227
228 The `_merge` setting within each top-level configuration key controls _which_ settings are merged and _how_ they are merged.
229
230 The value for `_merge` can be one of:
231
232 `none`
233 : No merge.
234
235 `shallow`
236 : Only add values for new keys.
237
238 `deep`
239 : Add values for new keys, merge existing.
240
241 Note that you don't need to be so verbose as in the default setup below; a `_merge` value higher up will be inherited if not set.
242
243 {{< code-toggle file=hugo dataKey="config_helpers.mergeStrategy" skipHeader=true />}}
244
245 > [!NOTE]
246 > Hugo can merge map configuration values from modules and themes into the project configuration, but cannot merge slice values. This applies to top-level slice keys such as `menus`, as well as to map keys whose values are slices, such as the per-kind format lists in `outputs`.
247
248 ## Environment variables
249
250 You can also configure settings using operating system environment variables:
251
252 ```sh
253 export HUGO_BASEURL=https://example.org/
254 export HUGO_ENABLEGITINFO=true
255 hugo
256 ```
257
258 The above configures the [`baseURL`][] and [`enableGitInfo`][] settings and then builds your site.
259
260 > [!NOTE]
261 > An environment variable takes precedence over the values set in the configuration file. This means that if you set a configuration value with both an environment variable and in the configuration file, the value in the environment variable will be used.
262
263 Environment variables simplify configuration for [CI/CD](g) platforms by allowing you to set values directly within their respective configuration and workflow files.
264
265 > [!NOTE]
266 > Environment variable names must be prefixed with `HUGO_`.
267 >
268 > To set custom site parameters, prefix the name with `HUGO_PARAMS_`.
269
270 For snake_case variable names, the standard `HUGO_` prefix won't work. Hugo infers the delimiter from the first character following `HUGO`. This allows for variations like `HUGOxPARAMSxAPI_KEY=abcdefgh` using any [permitted delimiter][].
271
272 In addition to configuring standard settings, environment variables may be used to override default values for certain internal settings:
273
274 `DART_SASS_BINARY`
275 : (`string`) The absolute path to the Dart Sass executable. By default, Hugo searches for the executable in each of the paths in the `PATH` environment variable.
276
277 `HUGO_ENVIRONMENT`
278 : (`string`) The build environment. Default is `production` when running `hugo build` and `development` when running `hugo server`.
279
280 `HUGO_FILE_LOG_FORMAT`
281 : (`string`) A format string for the file path, line number, and column number displayed when reporting errors, or when calling the `Position` method from a shortcode or Markdown render hook. Valid tokens are `:file`, `:line`, and `:col`. Default is `:file::line::col`.
282
283 `HUGO_MEMORYLIMIT`
284 : (`int`) The maximum amount of system memory, in gigabytes, that Hugo can use while rendering your site. Default is 25% of total system memory. Note that `HUGO_MEMORYLIMIT` is a "best effort" setting. Don't expect Hugo to build a million pages with only 1 GB of memory. You can get more information about how this behaves during the build by running `hugo build --logLevel info` and look for the `dynacache` label.
285
286 `HUGO_NUMWORKERMULTIPLIER`
287 : (`int`) The number of workers used in parallel processing. Default is the number of logical CPUs.
288
289 ## Current configuration
290
291 Display the complete project configuration with:
292
293 ```sh
294 hugo config
295 ```
296
297 Display a specific configuration setting with:
298
299 ```sh
300 hugo config | grep [key]
301 ```
302
303 Display the configured file mounts with:
304
305 ```sh
306 hugo config mounts
307 ```
308
309 [Google tag ID]: https://support.google.com/tagmanager/answer/12326985?hl=en
310 [JSON]: https://datatracker.ietf.org/doc/html/rfc7159
311 [TOML]: https://toml.io/en/latest
312 [YAML]: https://yaml.org/spec/
313 [`baseURL`]: /configuration/all#baseurl
314 [`enableGitInfo`]: /configuration/all#enablegitinfo
315 [permitted delimiter]: https://pubs.opengroup.org/onlinepubs/000095399/basedefs/xbd_chap08.html