We develop a theory of total factor productivity with heterogeneous firms, to explain differences in human capital across countries. In our model, firms operate in an economy with capital markets imperfections and costs of creating and operating in the formal sector. These distortions give rise to endogenous formal and informal sectors. Formal firms have a larger set of production opportunities but informal firms can avoid the costs of formalization. The model predicts that countries with a low degree of debt enforcement and high costs of formality are characterized by low allocative efficiency and a larger informal sector, lower TFP and lower human capital accumulation. We find that this mechanism is important in generating the skill distribution observed in developing countries.