Configuring the compiler
Solidity compilation in Hardhat is fully customizable. This guide covers the main ways you can configure the compiler to suit your project’s needs.
Configuring the compiler version and settings
Section titled “Configuring the compiler version and settings”The simplest way to configure compilation is to set the Solidity compiler version and, optionally, its settings:
import { defineConfig } from "hardhat/config";
export default defineConfig({ //... solidity: { version: "0.8.29",
settings: { /* solc settings, as expected by solc */ }, },});Enabling the optimizer
Section titled “Enabling the optimizer”One common use of settings is enabling the optimizer. When you enable it, you can also define the number of runs, which affects how the compiler balances between deployment cost and execution cost:
import { defineConfig } from "hardhat/config";
export default defineConfig({ //... solidity: { version: "0.8.29", settings: { optimizer: { enabled: true, runs: 200, }, }, },});Enabling viaIR
Section titled “Enabling viaIR”You can also enable the IR-based code generator. While this compilation mode is slower, it enables more powerful optimizations:
import { defineConfig } from "hardhat/config";
export default defineConfig({ //... solidity: { version: "0.8.29", settings: { viaIR: true, }, },});Other settings
Section titled “Other settings”The settings property accepts the same options supported by the chosen compiler version. For the full details, see the Solidity compiler documentation.
Next steps
Section titled “Next steps”You now know how to configure the Solidity compiler version, enable the optimizer, and adjust compiler settings. For more advanced configuration options, see the Solidity Configuration reference or these guides: