happened way befoee bacteria. First started off when protein learned to replicated itself..
Nothing has to learn anything in evolutionary terms. Things just happen because they can. If chemical reactions happen because they are faster than neighbouring chemical reactions, they are going to sequester the available chemical resources before competing systems do and put them out of business. This triggers open warfare and the winning reactions are the observably extant. don't forget that it's thought that it took c. a billion years of chemical 'evolution' before the first provable cell appeared. Once you've got your first cell , the rest is easy.
Once you have chemicals that help other chemicals to capture yet more chemicals, by positioning, compartmentalising and ordering their reactions, then you have life, of a sort.
After that, you need cells that capture other cells, to mutual advantage, and then you've got multi-cellular life.