Objectives: To examine trends in opioid prescribing in primary care, identify patient and general practice characteristics associated with long-term and stronger opioid prescribing, and identify associations with changes in opioid prescribing. Design: Trend, cross-sectional and longitudinal analyses of routinely recorded patient data. Setting: 111 primary care practices in Leeds and Bradford, UK. Participants: We observed 471 828 patient-years in which all patients represented had at least 1 opioid prescription between April 2005 and March 2012. A cross-sectional analysis included 99 847 patients prescribed opioids between April 2011 and March 2012. A longitudinal analysis included 49 065 patient-years between April 2008 and March 2012. We excluded patients with cancer or treated for substance misuse. Main outcome measures: Long-term opioid prescribing (4 or more prescriptions within 12 months), stronger opioid prescribing and stepping up to or down from stronger opioids. Results: Opioid prescribing in the adult population almost doubled for weaker opioids over 2005-2012 and rose over sixfold for stronger opioids. There was marked variation among general practices in the odds of patients stepping up to stronger opioids compared with those not stepping up (range 0.31-3.36), unexplained by practice-level variables. Stepping up to stronger opioids was most strongly associated with being underweight (adjusted OR 3.26, 1.49 to 7.17), increasing polypharmacy (4.15, 3.26 to 5.29 for 10 or more repeat prescriptions), increasing numbers of primary care appointments (3.04, 2.48 to 3.73 for over 12 appointments in the year) and referrals to specialist pain services (5.17, 4.37 to 6.12). Compared with women under 50 years, men under 50 were less likely to step down once prescribed stronger opioids (0.53, 0.37 to 0.75). Conclusions: While clinicians should be alert to patients at risk of escalated opioid prescribing, much prescribing variation may be attributable to clinical behaviour. Effective strategies targeting clinicians and patients are needed to curb rising prescribing, especially of stronger opioids.