Installing a solidity compiler
You have multiple options when it comes to compiling smart contracts written in solidity. In this recipe, you will learn about installing a solidity compiler and using it to compile your smart contract.
Getting ready
If you are using macOS, you will need homebrew
to install the binary compiler. There are other options, such as using npm
or docker
, which require Node.js
and Docker
, respectively, installed on your machine.
How to do it…
- If you are in Ubuntu, use
ppa
to install the compiler by running the following command:
sudoadd-apt-repositoryppa:ethereum/ethereumsudoapt-getupdatesudoapt-getinstallsolc
- If you are using macOS, install the compiler using
brew
:
brewupdatebrewtapethereum/ethereumbrewinstallsolidity
- Verify the installation with the following command:
solc --version
- Use this command to compile a contract and print the binary:
solc --bin SampleContract.sol
- You want to get some of the more advanced output from
solc
:
solc -o outDirectory --bin --ast --asm --abi --opcodes SampleContract.sol
We can configure it using these provided flags:
--ast
: Abstracts the syntax trees of source files--asm
: EVM assembly of the contracts--abi
: ABI specification of the contracts--opcodes
: Opcodes of the contracts
You can get the complete list of operations supported by the solc
compiler by running solc --help
.
There's more…
There is also a JavaScript-based solidity compiler available for you to use. You can install SolcJS
using npm
. The solc-js
project is derived from the C++ solc
project and can be used in JavaScript projects directly:
npm install -g solc
To see all the supported features, run the following command:
solcjs --help