34

I've got a contract where I want to use <address>.transfer(), which was released in the latest Solidity version 0.4.10.

I have as the first line of my contract: pragma solidity 0.4.10; but when I compile I get Error: Source file requires different compiler version which pretty obviously means truffle is trying to compile my contract with not that version of Solidity. It still uses 0.4.8.

I installed 0.4.10 last night and solc --version shows 0.4.10.

So my question is, how do I change the version of Solidity in a truffle project? I've looked in the config file and the truffle docs and could not find this. I also cannot even find where 0.4.8 is installed (was assuming in truffle itself but it is not listed in truffle's package.json).

6
  • which truffle's version do you have?
    – Badr Bellaj
    Commented May 2, 2017 at 20:41
  • Latest version as of two days ago. 3.2.1
    – JohnAllen
    Commented May 2, 2017 at 21:33
  • Struggeling with the same, found any solution?
    – vonGohren
    Commented May 7, 2017 at 10:56
  • No using send for now. Truffle is being updated soon fyi.
    – JohnAllen
    Commented May 7, 2017 at 16:45
  • I couldn't figure it out as well. How do we see which latest solc version is being used in truffle.
    – rstormsf
    Commented Jun 9, 2017 at 1:53

6 Answers 6

19

In Truffle version 5.0.0 (currently in Beta) you can specify a Solidity version in the truffle.js config file eg

module.exports = {
  networks: {
    ... etc ...
  },
  compilers: {
     solc: {
       version: <string>  // ex:  "0.4.20". (Default: Truffle's installed solc)
     }
  }
};

This is copied from the release details here

1
  • Somehow, a long time after that, and despite having ``` Truffle v5.1.15 (core: 5.1.15) Solidity - 0.6.4 (solc-js) ``` and telling just like in your file that my solc version should be 0.6.4, my IDE still believes I'm working with a 0.4.17 compiler... Do you have an idea why? Commented Mar 18, 2020 at 17:11
8

Truffle unfortunately doesn't allow you to select a different compiler version, apparently this is a result of the solc API not yet being stable, thus making it difficult to change version.

If you run truffle version it will output both the truffle, and compiler version.

$ truffle version
Truffle v3.4.9 (core: 3.4.8)
Solidity v0.4.15 (solc-js)

Updating truffle will get you the most up to date version it supports. It tends to lag behind solc itself somewhat, since it takes time to implement the latest version, though they've been quicker recently.

3
  • I only get one line when I run $ truffle version. It is: Truffle v2.1.2
    – paulhauner
    Commented Aug 24, 2017 at 5:00
  • Printing the solidity version must be a more recent feature then. Why not update to truffle 3?
    – maurelian
    Commented Aug 28, 2017 at 15:52
  • 1
    I did update to truffle 3. It now shows my solidity version :)
    – paulhauner
    Commented Aug 30, 2017 at 0:36
7

As of truffle v5.2.0, you can let Truffle compile based on the pragma expressions of each solidity file. The only thing to do is to write the following in the truffle-config.js file:

module.exports = {
  compilers: {
    solc: {
      version: “pragma”
    }
  },
  // … the rest of your config goes here
};

Truffle will then check each solidity file in the project for the pragma expression and compile based on the expression mentioned in each.

Source: https://www.trufflesuite.com/blog/take-a-dive-into-truffle-5#pragma-compilation

1

You will have to update your truffle to do this. I've answered how to update your truffle here.

1

All answers are moot above.

Add a 2nd line to the truffle-config.

Where it says version: "X.X.X"

Add a comma, then copy paste without the comma to the next line. Now your config should look like this:

version: "X.X.X",

version: "Y.Y.Y"

Do not add a comma to the 2nd one. Save the changes.

Now try compiling. It will use BOTH versions. Neat trick huh? :D

*** This only works in truffle 5 and up but you can specify lower versions such as 4.

1

in 2022.6, truffle 5.x + version, you can speicify the compiler version. e.g.

Bby default , solc version is 0.4.x , you can make it 0.8.x


$ cat truffle-config.js

module.exports = {
  networks: {
    development: {
      host: "127.0.0.1",
      port: 8545,
      network_id: "*"
    },
  },
  // here you can speicify the compiler version
  // by default , solc version is 0.4.x 
  compilers: {
    solc: {
      version: "^0.8.0",
    }
  }
};

and at the first time you run truffle migrate, you may see truffle downloaded the 0.8.x compiler, e.g.

$ truffle migrate --network development --verbose-rpc --interactive

Compiling your contracts...
===========================
✓ Fetching solc version list from solc-bin. Attempt #1
✓ Downloading compiler. Attempt #1.
✓ Fetching solc version list from solc-bin. Attempt #1
> Compiling ./contracts/MyTestNft.sol
> Artifacts written to /mnt/d/workspace/test_erc_721/build/contracts
> Compiled successfully using:
   - solc: 0.8.15+commit.e14f2714.Emscripten.clang

refer to: https://trufflesuite.com/docs/truffle/reference/configuration/

a compile example from official doc:

module.exports = {
  compilers: {
    solc: {
      version: <string>, // A version or constraint - Ex. "^0.5.0"
                         // Can be set to "native" to use a native solc or
                         // "pragma" which attempts to autodetect compiler versions
      docker: <boolean>, // Use a version obtained through docker
      parser: "solcjs",  // Leverages solc-js purely for speedy parsing
      settings: {
        optimizer: {
          enabled: <boolean>,
          runs: <number>   // Optimize for how many times you intend to run the code
        },
        evmVersion: <string> // Default: "istanbul"
      },
      modelCheckerSettings: {
        // contains options for SMTChecker
      }
    }
  }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.