Prevalence of mental illness is higher in people living with HIV than in the general population, but the incidence of composite mental illness and its components is unclear. We aimed to identify the risk of incident mental illness along with individual conditions of depression, anxiety, and severe mental illness in people living with HIV in the UK. Data for this population-based cohort were extracted from the IQVIA Medical Research Database, a nationally representative UK-based database of primary care electronic health records. We included adults (aged ≥18 years) living with HIV, matched with adults without HIV using propensity score matching (1:1 ratio). The primary outcome was composite mental illness comprising a diagnosis of depression, anxiety, or severe mental illness. Secondary outcomes were individual mental health conditions. Cox proportional hazard regression models were used to compare the risk of each outcome between people with and without HIV. Each model excluded those with the outcome at baseline. Individuals were followed up prospectively. The study period was from Jan 1, 2000, to Jan 1, 2020. Of 7167 people living with HIV without mental illness at baseline, 586 developed a mental illness (incidence rate 19·6 per 1000 person-years) compared with 418 of 7167 people without HIV (incidence rate 12·1 per 1000 person-years), resulting in an adjusted hazard ratio (HR) of 1·63 (95% CI 1·44-1·85). People living with HIV had higher incidence rates for depression (15·4 per 1000 person-years), anxiety (7·2 per 1000 person-years), and severe mental illness (1·6 per 1000 person-years) compared with people without HIV (7·9, 5·0, and 0·6 per 1000 person-years, respectively), with adjusted HRs of 1·94 (95% CI 1·68-2·24) for depression, 1·38 (1·15-1·66) for anxiety, and 2·18 (1·41-3·39) for severe mental illness. People living with HIV have an increased risk for developing composite mental illness, depression, anxiety, and severe mental illness compared with people without HIV. People living with HIV should be regularly screened for mental illness; however, there is a strong need to improve prevention of mental illness in people living with HIV and for more outreach programmes to ensure that no groups of people living with HIV are being underdiagnosed. None.