Background Treatment options are limited for patients infected by hepatitis C virus (HCV) with advanced liver disease. We assessed the safety and efficacy of ledipasvir, sofosbuvir, and ribavirin in patients with HCV genotype 1 or 4 and advanced liver disease. Methods We did an open-label study at 34 sites in Europe, Canada, Australia, and New Zealand. Cohort A included patients with Child-Turcotte-Pugh class B (CTP-B) or CTP-C cirrhosis who had not undergone liver transplantation. Cohort B included post-transplantation patients who had either no cirrhosis; CTP-A, CTP-B, or CTP-C cirrhosis; or fibrosing cholestatic hepatitis. Patients in each group were randomly assigned (1:1) using a computer-generated randomisation sequence to receive 12 or 24 weeks of ledipasvir (90 mg) and sofosbuvir (400 mg) once daily (combination tablet), plus ribavirin (600–1200 mg daily). The primary endpoint was the proportion of patients achieving a sustained virological response 12 weeks after treatment (SVR12). All patients who received at least one dose of study drug were included in the safety analysis and all patients who received at least one dose of study drug and did not undergo liver transplantation during treatment were included in the efficacy analyses. Estimates of SVR12 and relapse rates and their two-sided 90% CI (Clopper-Pearson method) were provided. This exploratory phase 2 study was not powered for formal comparisons among treatment groups; no statistical hypothesis testing was planned or conducted. The trial is registered with EudraCT (number 2013-002802-30) and ClinicalTrials.gov (number NCT02010255). Findings Between Jan 14, 2014, and Aug 19, 2014, 398 patients were screened. Of 333 patients who received treatment, 296 had genotype 1 HCV and 37 had genotype 4 HCV. In cohort A, among patients with genotype 1 HCV, SVR12 was achieved by 20 (87%, 90% CI 70–96) of 23 CTP-B patients with 12 weeks of treatment; 22 (96%, 81–100) of 23 CTP-B patients with 24 weeks of treatment; 17 (85%, 66–96) of 20 CTP-C patients (12 weeks treatment); and 18 (78%, 60–91) of 23 CTP-C patients (24 weeks treatment). In cohort B, among patients with genotype 1 HCV, SVR12 was achieved by 42 (93%, 84–98) of 45 patients without cirrhosis (12 weeks treatment); 44 (100%, 93–100) of 44 patients without cirrhosis (24 weeks treatment); 30 (100%, 91–100) of 30 CTP-A patients (12 weeks treatment); 27 (96%, 84–100) of 28 CTP-A patients (24 weeks treatment); 19 (95%, 78–100) of 20 CTP-B patients (12 weeks treatment); 20 (100%, 86–100) of 20 CTP-B patients (24 weeks treatment); one (50%, 3–98) of two CTP-C patients (12 weeks treatment); and four (80%, 34–99) of five CTP-C patients (24 weeks treatment). All five patients with fibrosing cholestatic hepatitis achieved SVR12 (100%, 90% CI 55–100). Among all patients with genotype 4 HCV, SVR12 was achieved by 14 (78%, 56–92) of 18 patients (12 weeks treatment) and 16 (94%, 75–100) of 17 patients (24 weeks treatment). Seven patients (2%) discontinued ledipasvir–sofosbuvir prematurely due to adverse events. 17 patients died, mainly from complications of hepatic decompensation. Interpretation Ledipasvir–sofosbuvir and ribavirin provided high rates of SVR12 for patients with advanced liver disease, including those with decompensated cirrhosis before or after liver transplantation. Funding Gilead Sciences.