The virtual reality (VR) market is expected to hit $25 billion this year, according to Fortune Business Insights, whose researchers forecast it will expand at a compound annual rate of 31% through 2030.

VR is by no means a new technology -- it has shown up in various forms and devices over the last few decades. However, recent advances in technologies such as chip design and artificial intelligence (AI) finally made it possible to deliver a truly immersive VR experience. The technology has come a long way, and has applications in a range of industries, from healthcare to education, design, gaming, and more.

The next few years could see the public adoption of VR surge as it finally takes its place in the mainstream, rather than just in a niche of the gaming community. As a result, this could be an excellent time to invest in the budding industry for the long term.

So, here are two stocks to buy to invest in virtual reality.

1. Apple

Apple (AAPL 0.29%) announced its first virtual reality/augmented reality (VR/AR) headset in June. The Vision Pro is expected to begin shipping in early 2024 and could become a major growth driver for the company over the long term.

The iPhone company is already the biggest name in consumer tech, and its history suggests it could eventually leapfrog current VR leaders Meta and Sony, and dominate the market. The tech giant wasn't the first company to produce smartphones, tablets, Bluetooth headphones, or smartwatches. However, public adoption of each of these devices skyrocketed once Apple launched its versions of them.

The company enjoys nearly unrivaled brand loyalty, which could help it attract consumers willing to try out a VR system created using Apple's familiar and user-friendly design.

The Vision Pro will debut at $3,499, which prices out most consumers. However, the company appears to be playing the long game, and will likely bring down the device's cost with future iterations, making it more accessible to the masses over time.

Apple shares have tumbled by around 8% since mid-July as macroeconomic headwinds caused repeated declines in its product segments. However, those economic challenges won't last forever, and Apple's dominance in tech will likely offer consistent gains over the long term. It could be worth buying the dip and investing in this VR stock ahead of the launch of the Vision Pro.

2. Microsoft

While Apple is focused on the consumer side of VR, Microsoft (MSFT 1.09%) offers an excellent way to invest in the commercial side of the industry. In 2016, the company launched its first headset, the HoloLens, and released a sequel to the device in 2019.

The HoloLens 2 retails for $3,500, similar to the Vision Pro. However, that price may be slightly more palatable as the device is geared more toward companies than consumers. Microsoft says the HoloLens 2 can boost productivity in multiple industries, such as manufacturing, engineering/construction, healthcare, and education.

The headset promises to increase manufacturing efficiency by 90%, allowing workers to collaborate instantly from anywhere. In engineering, Microsoft states the device will save organizations that use it significant amounts of money, and increase their efficiency in a range of ways.

Microsoft has become a favorite on Wall Street this year, with its shares up nearly 50% since Jan. 1. The company has rallied investors with a major push into AI, acquiring a 49% stake in ChatGPT developer OpenAI. AI will likely play a critical role in the future of VR, and Microsoft is well positioned to keep up with the market.

Microsoft shares soared by more than 230% over the last five years. The company is one of the biggest names in tech, with vast resources and seemingly endless growth potential. Its VR business is small for now, but I wouldn't bet against it investing more into the sector if demand for the technology rises. Now could be the perfect time to buy its stock and profit from the expanding VR industry.