Installing Truffle is simple and only takes a few minutes. The first step is to download and install Node.
js. This will allow you to use the npm package manager, which we will use to install Truffle.
Next, open a terminal window and enter the following command:
npm install -g truffle
This will install the latest version of Truffle globally on your system. You can now move on to initialize a project.
If you don’t already have a project initialized, create a new directory for your project and initialize it with Truffle using the init command:
truffle init
This will create a basic Truffle project with all of the necessary files and directories. Now that your project is initialized, you can install any Ethereum client you like.
For this guide, we’ll be using Ganache, which you can download here. Once you have Ganache installed and running, you should see something like this:.
Now that you have an Ethereum client up and running, you’re ready to compile and migrate your contracts. In your project’s root directory, create a file called Migration.sol with the following contents:
pragma solidity ^0.4.24; contract Migrations { uint public last_completed_migration; function setCompleted(uint completed) internal { last_completed_migration = completed; } function up() internal { last_completed_migration = 0; } function migrate() public { setCompleted(last_completed_migration + 1); } }
This contract keeps track of which migration has been run so that they can be run in order. The up function resets the last completed migration back to zero so that the migrations can be run from the beginning again if necessary.
The migrate function increments the last completed migration by one so that the next migration can be run.
Next, create a file called 2_deploy_contracts.js in your project’s migrations directory with the following contents:
var Migrations = artifacts.require(“./Migrations.sol”); module.exports = function(deployer) { deployer.
deploy(Migrations); };
This file tells Truffle what contracts need to be deployed and how they should be deployed. In this case, we’re telling Truffle to deploy the Migrations contract from the Migration.sol file we created earlier. This file also exports a function that tells Truffle how to deploy the contract (in this case, we’re just using the default deployer).
Now that your contracts are written and your migrations are set up, you’re ready to compile your contracts using the following command:
truffle compile
This will create compiled versions of your contracts in the build/contracts directory of your project which can then be deployed to an Ethereum network.