I tried to compile the following contract within geth:

pragma solidity ^0.4.8;

contract Test {
    uint256 public value;

    function Test() {
        value = 123;    
    }
}

using the following commands on the flattened source:

> var testSource = "pragma solidity ^0.4.8; contract Test { uint256 public value; function Test() { value = 123; } }";
undefined
> var testCompiled = web3.eth.compile.solidity(testSource);
Error: The method eth_compileSolidity does not exist/is not available
    at web3.js:3104:20
    at web3.js:6191:15
    at web3.js:5004:36
    at <anonymous>:1:20

As described in eth_compilers, eth_compileSolidity are gone in go-ethereum 1.6.0 #3793, the ability to compile Solidity source has been removed from within geth.

How can I compile my Solidity source code?

up vote 29 down vote accepted

Alternative 1 - Use Remix To Compile, Then Copy-Paste To Deploy

Use Remix from https://ethereum.github.io/browser-solidity:

enter image description here

Copy the Web3 deployment instructions from the Remix page:

var untitled_testContract = web3.eth.contract([{"constant":true,"inputs":[],"name":"value","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"inputs":[],"payable":false,"type":"constructor"}]);
var untitled_test = untitled_testContract.new(
   {
     from: web3.eth.accounts[0], 
     data: '0x60606040523415600b57fe5b5b607b6000819055505b5b608f806100246000396000f30060606040526000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff1680633fa4f24514603a575bfe5b3415604157fe5b6047605d565b6040518082815260200191505060405180910390f35b600054815600a165627a7a72305820a4fac284b98d43538f802082b0db8c67ddd6d72df4e8d9fbccb4cec0e52ea0df0029', 
     gas: '4700000'
   }, function (e, contract){
    console.log(e, contract);
    if (typeof contract.address !== 'undefined') {
         console.log('Contract mined! address: ' + contract.address + ' transactionHash: ' + contract.transactionHash);
    }
 })

And paste it into the geth console with an unlocked account:

> personal.unlockAccount(eth.accounts[0], "{top secret password}");
true
> var untitled_testContract = web3.eth.contract([{"constant":true,"inputs":[],"name":"value","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"inputs":[],"payable":false,"type":"constructor"}]);
"0xe22dc29e3e05d3206d9636ae7cba87a2827e7b3ef28c6e8e6d2ad49a14ba3ce9"
var untitled_test = untitled_testContract.new(
   {
     from: web3.eth.accounts[0],
     data: '0x60606040523415600b57fe5b5b607b6000819055505b5b608f806100246000396000f30060606040526000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff1680633fa4f24514603a575bfe5b3415604157fe5b6047605d565b6040518082815260200191505060405180910390f35b600054815600a165627a7a72305820a4fac284b98d43538f802082b0db8c67ddd6d72df4e8d9fbccb4cec0e52ea0df0029',
     gas: '4700000'
   }, function (e, contract){
    console.log(e, contract);
    if (typeof contract.address !== 'undefined') {
         console.log('Contract mined! address: ' + contract.address + ' transactionHash: ' + contract.transactionHash);
    }
 })



Alternative 2 - Use Solidity Compiler (Linux and OS/X, maybe Windows With Cygwin)

$ more Test.sol 
pragma solidity ^0.4.8;

contract Test {
    uint256 public value;

    function Test() {
        value = 123;    
    }
}

Compile Test.sol in the .json format, assign the data to a JavaScript variable and send the output into a file:

$ echo "var testOutput=`solc --optimize --combined-json abi,bin,interface Test.sol`" > test.js
$ cat test.js
var testOutput={"contracts":{"Test.sol:Test":{"abi":"[{\"constant\":true,\"inputs\":[],\"name\":\"value\",\"outputs\":[{\"name\":\"\",\"type\":\"uint256\"}],\"payable\":false,\"type\":\"function\"},{\"inputs\":[],\"payable\":false,\"type\":\"constructor\"}]","bin":"60606040523415600b57fe5b5b607b6000819055505b5b608f806100246000396000f30060606040526000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff1680633fa4f24514603a575bfe5b3415604157fe5b6047605d565b6040518082815260200191505060405180910390f35b600054815600a165627a7a72305820d0e71d151634ac6ae7626860a17881104022e5cd6d3a088eb8f941d9aa8e3bd20029"}},"version":"0.4.9+commit.364da425.Darwin.appleclang"}

In geth, load the contents of test.js:

$ geth console
...
> loadScript("test.js")
true
> testOutput
{
  contracts: {
    Test.sol:Test: {
      abi: "[{\"constant\":true,\"inputs\":[],\"name\":\"value\",\"outputs\":[{\"name\":\"\",\"type\":\"uint256\"}],\"payable\":false,\"type\":\"function\"},{\"inputs\":[],\"payable\":false,\"type\":\"constructor\"}]",
      bin: "60606040523415600b57fe5b5b607b6000819055505b5b608f806100246000396000f30060606040526000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff1680633fa4f24514603a575bfe5b3415604157fe5b6047605d565b6040518082815260200191505060405180910390f35b600054815600a165627a7a72305820d0e71d151634ac6ae7626860a17881104022e5cd6d3a088eb8f941d9aa8e3bd20029"
    }
  },
  version: "0.4.9+commit.364da425.Darwin.appleclang"
}

> testOutput.contracts
{
  abi: "[{\"constant\":true,\"inputs\":[],\"name\":\"value\",\"outputs\":[{\"name\":\"\",\"type\":\"uint256\"}],\"payable\":false,\"type\":\"function\"},{\"inputs\":[],\"payable\":false,\"type\":\"constructor\"}]",
...
> var testContract = web3.eth.contract(JSON.parse(testOutput.contracts["Test.sol:Test"].abi));
undefined
> personal.unlockAccount(eth.accounts[0], "{top secret password}");
true
> var test = testContract.new({ from: eth.accounts[0], data: "0x" + testOutput.contracts["Test.sol:Test"].bin, gas: 4700000},
  function (e, contract) {
    console.log(e, contract);
    if (typeof contract.address !== 'undefined') {
         console.log('Contract mined! address: ' + contract.address + ' transactionHash: ' + contract.transactionHash);
    }
  }
);
...
  • If I follow your instructions from Alternative 1 I get the error: "err: Error: invalid argument 0: invalid hex". I figured out that the bytecode (bin) causes this error. I also tried Alternative 2 but I get the same error. – Bumblebee May 15 '17 at 18:58
  • 2
    Alternative 2 worked and was the only working instruction I could find so far. Big thanks! – Jasper Jun 12 '17 at 6:12
  • Very good Alternative 2, thanks. – bortzmeyer Jul 4 '17 at 17:30
  • I was able to compile contract with ethereum.github.io/browser-solidity but when i got code from field WEB3DEPLOY and paste it in myetherwallet.com/#contracts Deploy contract filed it throw an error (red borders) – Vic Nicethemer Sep 25 '17 at 19:28
  • 2
    In a newer version of remix click on "Details" button of "Compile" section and look for WEB3DEPLOY section – yname Sep 30 '17 at 23:33

I've encountered the same issue recently. There are a variety of workarounds, but here is the method I've employed. This isn't necessarily the best approach.

To generate bytecode:

> solc --bin soliditySource.sol

To generate ABI:

> solc --abi soliditySource.sol

Then, with your newly generated bytecode and abi, in geth 1.6:

 var abi = <abi from solc>;
 var myContract = eth.contract(abi); 
 var bytecode = '0x' + <bytecode from solc>;
 var txDeploy = {from:eth.coinbase, data: bytecode, gas: 1000000}; 
 var myContractPartialInstance = myContract.new(txDeploy); 

 // Mine block containing transaction...

 var myContractInstance = myContract.at(myContractPartialInstance.address);
  • The abi needs to be a JSON object, so convert with JSON.parse. Also the account might need to be unlocked first with eg. personal.unlockAccount(eth.accounts[0]). – jordanpg Dec 24 '17 at 16:50

Maybe you'll consider downloading the truffle framework instead. With the truffle framework, I'm able to compile but also store my files easily (it does it for me).

Once you have downloaded truffle with the command

npm i -g truffle

you can easily compile using the command truffle compile.

After bashing my head against out of date docs for a few days, I've made a super simple version here that might help: https://alanbuxton.wordpress.com/2017/07/19/first-steps-with-ethereum-private-networks-and-smart-contracts-on-ubuntu-16-04/

Because geth deprecated the command-line compiler interface after version 1.6, I was puzzled for a long time.I wrote a simple tutorial to describe how to compile and deploy a contract after geth1.6. Hope this tutorial will help you. Depoly contract on the private chain

Building a smart contract using the command line https://www.ethereum.org/greeter

  • Hi and welcome to Ethereum StackExchange. Please do not post questions as answers to other questions. If you need assistance with a problem, please post a new question and someone will hopefully answer it. – Lauri Peltonen Apr 10 at 12:37

protected by Community Apr 11 at 2:16

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

Would you like to answer one of these unanswered questions instead?

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