DQ_happy
DQ_happy

Reputation: 505

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?

Upvotes: 21

Views: 77865

Answers (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

Upvotes: 2

shinya
shinya

Reputation: 31

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

Upvotes: 0

Aviator
Aviator

Reputation: 431

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

Upvotes: 0

mmann1123
mmann1123

Reputation: 5295

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 

Upvotes: 2

Christopholis
Christopholis

Reputation: 159

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.

Upvotes: 15

Saba Alipour
Saba Alipour

Reputation: 1

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

Upvotes: 0

Jim McManus
Jim McManus

Reputation: 31

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

Upvotes: 3

andschar
andschar

Reputation: 3973

you can also use the channel conda-forge

conda install -c conda-forge gdal

as suggested on the anaconda website.

Upvotes: 6

user1269942
user1269942

Reputation: 3852

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 ;) )

Upvotes: 22

Related Questions