Walgreens plans to open hundreds of primary-care clinics in the coming years as the drugstore giant continues to expand into medical care.

The pharmacy chain said Wednesday it has reached a deal with VillageMD to staff and operate 500 to 700 clinics in 30 U.S. markets within five years. Though the company did not identify specific locations, it said most will be in underserved areas.

VillageMD, a primary-care provider that employs more than 2,800 physicians serves patients through free-standing clinics. It plans to recruit more than 3,600 primary-care providers under its partnership with Walgreens.

Walgreens will invest $1 billion in equity and debt in VillageMD over three years and at the end of that period will claim a 30 percent ownership stake.

The clinics will be about 3,300 square feet, about a quarter of the typical Walgreens store, although some will be as large as 9,000 square feet, the company said. Though the retailer, which has more than 18,000 locations, will continue to sell its array of drugstore goods, it said it will scale back on grocery and wellness merchandise at stores with larger VillageMD clinics.

The clinics will offer a host of services, from annual checkups to preventive care to the treatment of chronic diseases. Nurse practitioners, physicians’ assistants and social workers will work out of some locations. At-home visits and telehealth options will also be made available.

These clinics at our conveniently located stores are a significant step forward in creating the pharmacy of the future, meeting many essential health needs all under one roof as well as through other channels,” Stefano Pessina, chief executive of Walgreens Boots Alliance, said in a statement.

The partnership comes as drugstores are consolidating to offer more services. In 2018, CVS acquired Aetna, the nation’s third-largest health insurer, in a $69 billion deal. The mega tie-up was designed to pull CVS’s brick-and-mortar locations closer to front-line clinics for basic care and patient monitoring.

Walgreens said expanding medical care at its drugstores is both a convenience for its customers and a better way to treat chronic conditions that require a host of daily medications. Combining services also leads to better health outcomes, the company said, based on its trial run involving a handful of clinics in Houston.

The clinics will offer comprehensive health care and will accept a range of health insurance plans, the company said, while patients without health insurance will be charged on a sliding scale. It did not disclose what it might charge for specific services.

More than half of the clinics will be located in areas where the federal government has designated a shortage of health-care professionals and where communities are underserved, Walgreens said.

The companies envision the in-store clinic system as an improvement over the “episodic” nature of visiting the doctor, aiming to build a model that emphasizes convenient follow-ups and lowered costs.

Walgreens shares were nearly flat Wednesday, trading above $42 a share. The company releases quarterly results Thursday.