All organisms have the same genetic code, so it must have arisen early in evolution. Earliest heterotrophs got the organic molecules they needed from the environment. Anaerobic fermentation of organic molecules have first cells their energy. Some cells mutated and could use sunlight to make ATP for energy. Advantage when supply of organic molecules ran out.
Autotrophs produce their own food from simple raw materials.
First autotrophs probably used sunlight to split H2S for a source of electrons in photosynthesis. Green sulfur bacteria and purple sulfur bacteria still use H2S for photosynthesis. Cyanobacteria split water to get electrons for photosynthesis. Oxygen was combined with minerals, but eventually O2 accumulated in the atmosphere. First photosynthetic organisms 3.1 to 3.5 billion years ago in fossil stromalites.