We model firm adaptation to local factor markets in which firms care about both the price and availability of inputs. The model is estimated by combining firm and population census data, and quantifies the role of factor markets in input use, productivity and welfare. Considering China's diverse factor markets, we find within industry interquartile labor costs vary by 30-80%, leading to 3-12% interquartile differences in TFP. In general equilibrium, homogenization of labor markets would increase real income by 1.33%. Favorably endowed regions attract more economic activity, providing new insights into within-country comparative advantage and specialization.