FAQ - How do change truffle version

When deploying a smart contract, you might see this error popping up in your terminal.

As stated in the error message, there is a mismatch between the truffle version declared in the pragma statements and the compiler version used to migrate the contracts.

In this example, truffle is using solc 0.5.16, but my contracts specify “pragma solidity 0.5.12”.

In order to fix this error we can follow two paths:

  • Change the version in the pragma statement so that it matches the compiler one.
  • Change the compiler version so that it matches the one in the pragma statement.

The correct solution is to change the compiler version stated in the truffle-config.js file.

Before start the procedure below, make sure to turn off your local blockchain (if running).

  • Open your truffle project and double click on the truffle-config.js file.
  • Scroll till the bottom of the file until you see:
  compilers: {
    solc: {
      // version: "0.5.8",    // Fetch exact version from solc-bin (default: truffle's version)
      // docker: true,        // Use "0.5.1" you've installed locally with docker (default: false)
      // settings: {          // See the solidity docs for advice about optimization and evmVersion
      //  optimizer: {
      //    enabled: false,
      //    runs: 200
      //  },
      //  evmVersion: "byzantium"
      // }
    },
  },
  • Uncomment the version and write there the version you want to use, in the example case 0.5.12;
compilers: {
    solc: {
       version: "0.5.12",    // Fetch exact version from solc-bin (default: truffle's version)
      // docker: true,        // Use "0.5.1" you've installed locally with docker (default: false)
      // settings: {          // See the solidity docs for advice about optimization and evmVersion
      //  optimizer: {
      //    enabled: false,
      //    runs: 200
      //  },
      //  evmVersion: "byzantium"
      // }
    },
  },
  • Save the file.

Migrate your contract once again, it should work as expected.

2 Likes