'Install gdal using conda?

I used

conda install gdal

to install the GDAL packages. But I had the following error when importing the packages.

>>> from osgeo import gdal
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/danqing0703/anaconda/lib/python2.7/site-packages/osgeo/__init__.py", line 21, in <module>
    _gdal = swig_import_helper()
  File "/Users/danqing0703/anaconda/lib/python2.7/site-packages/osgeo/__init__.py", line 17, in swig_import_helper
    _mod = imp.load_module('_gdal', fp, pathname, description)
ImportError: dlopen(/Users/danqing0703/anaconda/lib/python2.7/site-packages/osgeo/_gdal.so, 2): Library not loaded: libgdal.20.dylib
  Referenced from: /Users/danqing0703/anaconda/lib/python2.7/site-packages/osgeo/_gdal.so
  Reason: image not found
>>> from osgeo import ogr
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/danqing0703/anaconda/lib/python2.7/site-packages/osgeo/__init__.py", line 21, in <module>
    _gdal = swig_import_helper()
  File "/Users/danqing0703/anaconda/lib/python2.7/site-packages/osgeo/__init__.py", line 17, in swig_import_helper
    _mod = imp.load_module('_gdal', fp, pathname, description)
ImportError: dlopen(/Users/danqing0703/anaconda/lib/python2.7/site-packages/osgeo/_gdal.so, 2): Library not loaded: libgdal.20.dylib
  Referenced from: /Users/danqing0703/anaconda/lib/python2.7/site-packages/osgeo/_gdal.so
  Reason: image not found

What should I do to have GDAL imported in Python?



Solution 1:[1]

For windows users (as of December 2015):

conda install gdal
conda upgrade numpy

Installing gdal will downgrade numpy, so then upgrade it back up. I recently had occasion to use windows for a change and I was pleasantly surprised that gdal "works" easily now.

Windows+python+gis people worldwide should be celebrating this. (that gdal-python goes in easily on windows...not that windows is one step closer to linux ;) )

Solution 2:[2]

I just made the mistake of executing the previously proposed command in the base environment of Conda:

conda install -c conda-forge gdal

This took ages to "solve environment" and, in the end, found numerous conflicts which halted the installation.

Given that, I instead created a separate environment with:

conda create -n gdal python=3.8

And activated it with:

conda activate gdal

And then executed the first command (as well as all others listed in the documentation). This worked fast and without any errors.

Solution 3:[3]

you can also use the channel conda-forge

conda install -c conda-forge gdal

as suggested on the anaconda website.

Solution 4:[4]

I used

conda install -c conda-forge gdal

On a Fedora 30 machine and it lead me down a path of library conflict hell!

conda install gdal

worked on my first try

Solution 5:[5]

The following works reliably for me Ubuntu 20.04:

conda update conda    
sudo apt-get install libgdal-dev gdal-bin

export CPLUS_INCLUDE_PATH=/usr/include/gdal
export C_INCLUDE_PATH=/usr/include/gdal

# check gdal version with 
gdal-config --version

echo '
name: raster-pipeline
channels:
- defaults
dependencies:
- python=3.7.*
- cython
- numpy
- ipython
- libspatialindex
- libgdal=USE_GDAL_VERSION
- gdal=USE_GDAL_VERSION
- pip
- pip:
  - numpy>=1.18.5
  - GDAL==USE_GDAL_VERSION
  - pyproj>=2.6.1.post1
  - rasterio>=1.1.5
' > raster_pipeline.yml
  
conda env create -f raster_pipeline.yml -v 
 
conda activate raster_pipeline 
python -c "from osgeo import gdal"
conda deactivate 

Solution 6:[6]

I had the same problem, and after several days trying different solutions, I found the problem in conflict between Anaconda version and python version! if you have both Python and Anaconda on your system, then uninstall the python and use anaconda prompt to run this: pip install gdal

Solution 7:[7]

This worked for me . Hope it is useful to someone . I am using Ubuntu 20.04: It install osgeo also with gdal

Step 1 : conda install -c conda-forge gdal

If there is still some error then

Step 2 : conda config --set channel_priority strict”

Step 3 : conda update gdal

Solution 8:[8]

For existing conda-env, I did do like below on Ubuntu 20.04:

conda update conda    
sudo apt-get install libgdal-dev gdal-bin

$export CPLUS_INCLUDE_PATH=/usr/include/gdal
$export C_INCLUDE_PATH=/usr/include/gdal

$gdal-config --version
gdal3.0.4

$whereis pip
/usr/local/anaconda3/bin/pip # anaconda's pip

$/usr/local/anaconda3/bin/pip install setuptools==57.4.0
$/usr/local/anaconda3/bin/pip install gdal==3.0.4

Solution 9:[9]

This works for me

> CONDA_SUBDIR=osx-64 conda create -n my_env python=3.7
> conda activate my_env
> conda env config vars set CONDA_SUBDIR=osx-64
> conda install gdal
Now use,
> python3
> from osgeo import gdal

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 user1269942
Solution 2 Jeremy Caney
Solution 3 andschar
Solution 4 Jim McManus
Solution 5
Solution 6 Saba Alipour
Solution 7 Aviator
Solution 8 shinya
Solution 9 ???pák ??p??