38

I am using solidity version "pragma solidity ^0.4.20;". When I checked JSON files of contract, compiler version shows :

"compiler": {
    "name": "solc",
    "version": "0.4.21+commit.dfe3193c.Emscripten.clang"
  }

Still I am getting error :

Source file requires different compiler version (current compiler is 0.5.0+commit.1d4f565a.Emscripten.clang - note that nightly builds are considered to be strictly less than the released version

Command 'truffle version' gives :

 Truffle v4.1.5 (core: 4.1.5)
 Solidity v0.4.21 (solc-js)
4
  • 2
    What are you doing specifically that triggers that error? Trying to run the Solc binaries? Commented Dec 4, 2018 at 13:15
  • I was deploying contract to privatenet. The contract was running fine and suddenly this error occurred. When I change pragma solidity ^0.4.20; to pragma solidity 0.5.0; error disappears. Even there is error, I am able to deploy contracts.
    – angelmaria
    Commented Dec 4, 2018 at 13:18
  • 3
    The error implies the installed Solidity compiler is version which is trying to compile your solidity code is v0.5.0. So the question I am asking is how are you triggering the compilation? Clearly solc-js is not being used since it appears to have the right version. Commented Dec 4, 2018 at 13:33
  • 1
    Like @ShawnTabrizi pointed out, we need to know how the compilation is triggered. Often this will happen because people have different truffle versions installed globally vs locally for the project. If you're compiling with the locally installed truffle, it may be a newer version that has 0.5.0, but not new enough that it has BYOC (bring your own compiler). Then when you do truffle version, it uses the global one which is the right version.
    – natewelch_
    Commented Dec 4, 2018 at 16:20

14 Answers 14

16

During solidity (solc) 0.5 releases the different frameworks like solc, truffle, web3, openzeppelin had a timeframe where some had a 0.5 compatible version released and some didn't. I think you could have changed specified the solc version like here (didn't try that) but meanwhile all of the mentioned fully support 0.5 (openzeppelin since 2 days ago at time of writing).

Consider doing the following

npm outdated
npm install [my-outdated-package] #repeat for all outdated packages
rm -R -f build #cleanup step (might not be necessary)
rm -R -f node_modules #cleanup step (might not be necessary)
npm install
truffle compile #(will still fail)

Check with truffle version in a terminal window at some other place (not project dir) if it matches the current release here otherwise do this:

npm uninstall -g truffle
npm install truffle

Usually you should avoid installing global packages but e.g. BlockCatIO/solidity-flattener requires that solc is installed globally because of certain features (installation described here). Remember to upgrade that one too.

When you then run truffle-compile you should still see errors because they changed the syntax of pragma (found that here).

So change:

#old
pragma solidity ^0.4.20;

#new
pragma solidity >=0.4.20;

I also recommend upgrading ganache-cli (I had problems running the tests with old version):

npm install -g ganache-cli

You might then still see compile errors but that's what makes v0.5 great because they made some improvements you should troubleshoot one-by-one

Here the Breaking Changes in Solidity 0.5.0

Doing this I got it compiling again :)

18

Thanks, I was using visualstudio editor and enabled an extension for solidity where compiler version was upgraded. By specifying the version of compiler in Editor resolved the issue for me.

4
  • What setting did you change? What value did you give it? How did you get to that setting? Commented Jun 5, 2020 at 4:01
  • 4
    @PhilipRego You probably figured it out, however, for anyone using a Mac: CTRL + Click anywhere within the contract file using your VS editor and select "Change Compiler Version", it will then display a list of compiler versions for your selection beginning with the latest. For windows you may need to right click.
    – SirLemuel
    Commented Apr 7, 2021 at 20:18
  • 3
    With Windows, WSL (Ubuntu) and VS Code I did just that: CTRL+right click in the editor, then Solidity: Change workspace compiler version (Remote), selected a version that was suggested for the particular project and it worked. Commented May 12, 2021 at 5:53
  • I think this should be the accepted answer. In case you are working on an older project and you can't switch the version, you will have to use a different compiler version :)
    – Donut
    Commented Jul 7, 2022 at 12:46
5

I would recommend using solc-select (https://github.com/crytic/solc-select), it will allow switching easily between solc versions.

2
5

I had the same issue and by doing following, my error disappeared.

The syntax change in pragma is giving error. just Change pragma solidity ^0.4.20; to pragma solidity >=0.4.20;.

5

What worked for me is the following:

  1. Type ctrl+SHIFT+p
  2. Select solidity:Change Global compiler version (Remote)
  3. Select your desired solidity version
  4. Additionally, I needed to change in the setting of solidity extension by Juan Blanco. Click on the settings icon and go to extension settings
  5. Set Solidity default compiler option to remote enter image description here
3

Try format of compiler at first line of Contract .sol :- pragma solidity >=0.4.21 <0.6.0

It worked for me. Hope this helps,

1

I had the same issue and by doing following, my error disappeared.

The syntax change in pragma is giving error. just Change pragma solidity >=0.7.0 <0.9.0;.

Post doing this you will get an warning

Warning: SPDX license identifier not provided in source file. Before publishing, consider adding a comment containing "SPDX-License-Identifier: " to each source file. Use "SPDX-License-Identifier: UNLICENSED" for non-open-source code. Please see https://spdx.org for more information.

Just add it above pragma line.

// SPDX-License-Identifier: GPL-3.0

1
  • This will work if the source code was is compatible with the new solc version. If it is not compatible you will have to fix all the issues.
    – Ismael
    Commented Feb 2, 2022 at 2:55
1

Fix

Change the source of the compiler for the extension:

  • Open the command palette ctrl+p
  • Type > followed by Change the default workspace
  • Select remote and then Change Global compiler version (Remote) to your version, or an option that suits your project the best.
  • You can read more about it here
  • Keep the grind

Reference

https://cesarvarela.com/blog/fix-source-file-requires-different-compiler-version.

1

So this is how you solve this compiler version problem.(Worked for me)

Right click the pragma solidity statement, and select the following option:

Solidity: Change workspace compiler version (Remote)

Then, all you have to do is select the Solidity compiler you want.

0

Run npm install solc go to package.json check the version of solc after go to truffle.config.json replace he version make sure its the same as in package.json change it on sol ils too

0

I ran into a similar error and I updated my file with the following

//SPDX-License-Identifier: UNLICENSED
pragma solidity >=0.7.0 <0.9.0;

Consider making these changes and your project will update

0

Go to Settings of VS Code. Choose default compiler => remote

choose and copy paste the version you want to support https://etherscan.io/solcversions

enter image description here

0

If you're using Juan Blanco's Solidity extension for VS Code, besides the compiler version in the truffle-config.js you will also have to change the compiler version of the extension itself. To do that:

  1. Open VS Code command palette by pressing Ctrl+Shift+P
  2. Type Solidity: Change global compiler version (Remote) then press enter
  3. This will open another menu from which you can select the desired version

NOTE: Only changing the extension's compiler version will actually allow you to compile your contract causing some warning. Nevertheless, it is important to also change the compiler version in the truffle-config.js file, because when compiling outside of your VS Code instance, the version specified that file will be used. You can verify that by running truffle compile --compiler in the terminal.

0

Just add this :

    compilers: {
solc: {
  version: "^0.4.20"
}
}

Where "^0.4.20" is the version of the compiler you want to use.

So your truffle config file should look like this :

   module.exports = {
     networks: {
       development: {
         host: "127.0.0.1",
         port: 7545,
         network_id: "*" // Match any network id
       }
   },
    compilers: {
      solc: {
        version: "^0.4.20"
         }
       }
     };

And the problem will be solved :)

Don't forget to check the documentation : https://trufflesuite.com/docs/truffle/reference/configuration/#compiler-configuration

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.