BACKGROUND: Life expectancy of people with HIV has increased considerably. We used data from the Comparative Outcomes And Service Utilization Trends (COAST) study to examine sex differences in life expectancy and mortality between 1996 and 2020 among people with HIV in British Columbia (BC), Canada. METHODS: COAST, a population-based cohort study, includes clinical and administrative health data on virtually all people with HIV in BC. We calculated life expectancy for people with HIV at ages 20, 40, and 55 years stratified by sex and calendar period. We used Cox regression to model associations between sex and all-cause and cause-specific mortality, adjusting for individual and social determinants of health. FINDINGS: 11 738 males (82·2%) and 2534 females (17·8%) of 14 272 people with HIV, aged ≥20 years, were included. Life expectancy at age 20 years for males increased by 23·54 years from 1996-2001 to 2012-20 (from 24·46 years to 48·00 years), but life expectancy for females in that period increased only by 18·81 years (from 22·13 years to 40·94 years). Similarly, life expectancy at ages 40 years and 55 years increased over time in all strata but remained lower among females than males. The sex-gap in life expectancy at ages 20 and 40 years increased over time. The association between female sex and all-cause mortality was attenuated but remained statistically significant after adjusting for individual and social determinants of health (hazard ratio 1·11 [95% CI 1·03-1·20]). INTERPRETATION: Although life expectancy among people with HIV in BC increased between 1996 and 2020, life expectancy of females remained lower than males with the gap between them increasing over time. Sex differences in all-cause mortality were partially explained by differences in known socio-structural determinants and immune status. Women with HIV should be considered a priority for public health strategies to address structural factors with adverse health impact. FUNDING: Canadian Institutes of Health Research (CIHR), Health Research BC, CIHR Canadian HIV Trials Network, and National Institutes of Health.