The administrative court in the western city of Cologne said that while cannabis remained illegal for general use in Germany, it may be cultivated at home by some patients with medical permits for the drug.
The court said in a statement that applications must be reviewed on a case-by-case basis, adding that a key condition was that "access by third parties to the plants and products must be sufficiently restricted".
The decision came as many parts of the world are relaxing laws on cannabis use and medicinal marijuana is gaining popularity to ease suffering from cancer, glaucoma, HIV or AIDS, Hepatitis C, Parkinson's disease and other serious conditions.
Five patients with chronic pain had filed suits to gain permission from Germany's Federal Institute for Drugs and Medical Devices to grow their own medical marijuana.
While each plaintiff already had a permit to consume the drug to alleviate pain and nausea, they sought to cultivate it at home to keep costs down because the drug is not covered by their health insurance.
The court rejected two of the five cases, one because the plaintiff's living situation did not guarantee that access to the marijuana plants could be restricted to the patient alone.
In the second, the court said that alternatives to marijuana as a treatment option for the patient's illness had not been exhausted.
The ruling may be appealed.
The issue of recreational and medical cannabis use is controversial in many countries, as opponents fear crime connected to drug abuse and addiction. Some argue pot use could lead to dependence on harder drugs.
But the tide appears to be turning in many Western countries. New York this month became the 23rd state in the United States to legalise marijuana for medical use.