Primary liver cancer represents a major health problem. It comprises hepatocellular carcinoma (HCC) and intrahepatic cholangiocarcinoma (ICC), which differ markedly with regards to their morphology, metastatic potential and responses to therapy. However, the regulatory molecules and tissue context that commit transformed hepatic cells towards HCC or ICC are largely unknown. Here we show that the hepatic microenvironment epigenetically shapes lineage commitment in mosaic mouse models of liver tumorigenesis. Whereas a necroptosis-associated hepatic cytokine microenvironment determines ICC outgrowth from oncogenically transformed hepatocytes, hepatocytes containing identical oncogenic drivers give rise to HCC if they are surrounded by apoptotic hepatocytes. Epigenome and transcriptome profiling of mouse HCC and ICC singled out Tbx3 and Prdm5 as major microenvironment-dependent and epigenetically regulated lineage-commitment factors, a function that is conserved in humans. Together, our results provide insight into lineage commitment in liver tumorigenesis, and explain molecularly why common liver-damaging risk factors can lead to either HCC or ICC.