Dear student,
Magma forms from melting of mantle rocks. As the rocks move upward during melting. These molten rocks migrate upward and coalesce into larger volumes that continue to move upward. They may collect in a magma chamber or they may just come straight up. As they rise, gas molecules in the magma come out of solution and form bubbles and as the bubbles rise they expand. Eventually the pressure from these bubbles is stronger than the surrounding solid rock and this surrounding rock fractures, allowing the magma to get to the surface.
Regards,