File: building.rst

package info (click to toggle)
apache-arrow 23.0.1-1
  • links: PTS
  • area: main
  • in suites: sid
  • size: 76,220 kB
  • sloc: cpp: 654,608; python: 70,522; ruby: 45,964; ansic: 18,742; sh: 7,365; makefile: 669; javascript: 125; xml: 41
file content (645 lines) | stat: -rw-r--r-- 21,693 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
.. Licensed to the Apache Software Foundation (ASF) under one
.. or more contributor license agreements.  See the NOTICE file
.. distributed with this work for additional information
.. regarding copyright ownership.  The ASF licenses this file
.. to you under the Apache License, Version 2.0 (the
.. "License"); you may not use this file except in compliance
.. with the License.  You may obtain a copy of the License at

..   http://www.apache.org/licenses/LICENSE-2.0

.. Unless required by applicable law or agreed to in writing,
.. software distributed under the License is distributed on an
.. "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
.. KIND, either express or implied.  See the License for the
.. specific language governing permissions and limitations
.. under the License.

.. currentmodule:: pyarrow
.. highlight:: console
.. _build_pyarrow:

================
Building PyArrow
================


This page provides source build instructions for PyArrow for all platforms.

System Requirements
===================

.. tab-set::
   :sync-group: language

   .. tab-item:: Linux and macOS
      :sync: linux-macos

      On macOS, any modern XCode or Xcode Command Line Tools (``xcode-select --install``)
      are sufficient.

      On Linux, for this guide, we require a minimum of gcc or clang 9.
      You can check your version by running

      .. code-block::

         $ gcc --version

      If the system compiler is older than gcc 9, it can be set to a newer version
      using the ``$CC`` and ``$CXX`` environment variables:

      .. code-block::

         $ export CC=gcc-9
         $ export CXX=g++-9


   .. tab-item:: Windows
      :sync: wins

      Building on Windows requires one of the following compilers to be
      installed:

      - `Build Tools for Visual Studio 2022 <https://aka.ms/vs/17/release/vs_BuildTools.exe>`_ or
      - Visual Studio 2022

      During the setup of Build Tools, ensure at least one Windows SDK
      is selected.

Environment setup
=================

First, start from a fresh clone of Apache Arrow:

.. code-block::

   $ git clone https://github.com/apache/arrow.git

There are two supported ways to set up the build environment for PyArrow: using
**Conda** to manage the dependencies or using **pip** with manual dependency
management.

Both methods are shown bellow for Linux and macOS. For Windows, only the
Conda-based setup is currently documented, skipping some of the
Linux/macOS-only packages.

Note that in case you are not using conda on a Windows platform, Arrow C++
libraries need to be bundled with ``pyarrow``. For extra information see the
Windows tab under the :ref:`pyarrow_build_section` section.

.. tab-set::
   :sync-group: language

   .. tab-item:: Linux and macOS using conda
      :sync: linux-macos

      Pull in the test data and setup the environment variables:

      .. code-block::

         $ pushd arrow
         $ git submodule update --init
         $ export PARQUET_TEST_DATA="${PWD}/cpp/submodules/parquet-testing/data"
         $ export ARROW_TEST_DATA="${PWD}/testing/data"
         $ popd

      The `conda <https://conda.io/>`_ package manager allows installing build-time
      dependencies for Arrow C++ and PyArrow as pre-built binaries, which can make
      Arrow development easier and faster.

      Let's create a conda environment with all the C++ build and Python dependencies
      from conda-forge, targeting development for Python 3.13:

      .. code-block::

         $ conda create -y -n pyarrow-dev -c conda-forge \
               --file arrow/ci/conda_env_unix.txt \
               --file arrow/ci/conda_env_cpp.txt \
               --file arrow/ci/conda_env_python.txt \
               --file arrow/ci/conda_env_gandiva.txt \
               compilers \
               python=3.13 \
               pandas

      As of January 2019, the ``compilers`` package is needed on many Linux
      distributions to use packages from conda-forge.

      With this out of the way, you can now activate the conda environment

      .. code-block::

         $ conda activate pyarrow-dev


      We need to set some environment variables to let Arrow's build system know
      about our build toolchain:

      .. code-block::

         $ export ARROW_HOME=$CONDA_PREFIX


   .. tab-item:: Linux and macOS using pip

      .. warning::

         If you installed Python using the Anaconda distribution or `Miniconda
         <https://conda.io/miniconda.html>`_, you cannot currently use a
         pip-based virtual environment. Please follow the conda-based development
         instructions instead.

      Pull in the test data and setup the environment variables:

      .. code-block::

         $ pushd arrow
         $ git submodule update --init
         $ export PARQUET_TEST_DATA="${PWD}/cpp/submodules/parquet-testing/data"
         $ export ARROW_TEST_DATA="${PWD}/testing/data"
         $ popd

      **Using system and bundled dependencies**

      If not using conda, you must arrange for your system to provide the required
      build tools and dependencies.  Note that if some dependencies are absent,
      the Arrow C++ build chain may still be able to download and compile them
      on the fly, but this will take a longer time than with pre-installed binaries.

      On macOS, use Homebrew to install all dependencies required for
      building Arrow C++:

      .. code-block::

         $ brew update && brew bundle --file=arrow/cpp/Brewfile

      See :ref:`here <cpp-build-dependency-management>` for a list of dependencies you
      may need.

      On Debian/Ubuntu, you need the following minimal set of dependencies:

      .. code-block::

         $ sudo apt-get install build-essential ninja-build cmake python3-dev

      Now, let's create a Python virtual environment with all Python dependencies
      in the same folder as the repositories, and a target installation folder:

      .. code-block::

         $ python3 -m venv pyarrow-dev
         $ source ./pyarrow-dev/bin/activate
         $ pip install -r arrow/python/requirements-build.txt

         $ # This is the folder where we will install the Arrow libraries during
         $ # development
         $ mkdir dist

      If your CMake version is too old on Linux, you could get a newer one via
      ``pip install cmake``.

      We need to set some environment variables to let Arrow's build system know
      about our build toolchain:

      .. code-block::

         $ export ARROW_HOME=$(pwd)/dist
         $ export LD_LIBRARY_PATH=$(pwd)/dist/lib:$LD_LIBRARY_PATH
         $ export CMAKE_PREFIX_PATH=$ARROW_HOME:$CMAKE_PREFIX_PATH

   .. tab-item:: Windows
      :sync: wins

      Let's create a conda environment with all the C++ build and Python dependencies
      from conda-forge, targeting development for Python 3.13:

      .. code-block::

         $ conda create -y -n pyarrow-dev -c conda-forge ^
               --file arrow\ci\conda_env_cpp.txt ^
               --file arrow\ci\conda_env_python.txt ^
               --file arrow\ci\conda_env_gandiva.txt ^
               python=3.13
         $ conda activate pyarrow-dev

      Now, we can build and install Arrow C++ libraries.

      We set the path of the installation directory of the Arrow C++
      libraries as ``ARROW_HOME``. When using a conda environment,
      Arrow C++ is installed in the environment directory, which path
      is saved in the `CONDA_PREFIX <https://docs.conda.io/projects/conda-build/en/latest/user-guide/environment-variables.html#environment-variables-that-affect-the-build-process>`_
      environment variable.

      .. code-block::

         $ set ARROW_HOME=%CONDA_PREFIX%\Library

Build
=====

First we need to configure, build and install the Arrow C++ libraries.
Then we can build PyArrow.

Build C++
---------

.. tab-set::
   :sync-group: language

   .. tab-item:: Linux and macOS
      :sync: linux-macos

      Now build the Arrow C++ libraries and install them into the directory we
      created above (stored in ``$ARROW_HOME``):

      .. code-block::

         $ cmake -S arrow/cpp -B arrow/cpp/build \
               -DCMAKE_INSTALL_PREFIX=$ARROW_HOME \
               --preset ninja-release-python
         $ cmake --build arrow/cpp/build --target install

      **About presets**

      ``ninja-release-python`` is not the only preset available - if you would like a
      build with more features like CUDA, Flight and Gandiva support you may opt for
      the ``ninja-release-python-maximal`` preset. If you wanted less features, (i.e.
      removing ORC and dataset support) you could opt for
      ``ninja-release-python-minimal``. Changing the word ``release`` to ``debug``
      with any of the aforementioned presets will generate a debug build of Arrow.

      **Individual components**

      The presets are provided as a convenience, but you may instead opt to
      specify the individual components:

      .. code-block::

         $ cmake -S arrow/cpp -B arrow/cpp/build \
               -DCMAKE_INSTALL_PREFIX=$ARROW_HOME \
               -DCMAKE_BUILD_TYPE=Debug \
               -DARROW_BUILD_TESTS=ON \
               -DARROW_COMPUTE=ON \
               -DARROW_CSV=ON \
               -DARROW_DATASET=ON \
               -DARROW_FILESYSTEM=ON \
               -DARROW_HDFS=ON \
               -DARROW_JSON=ON \
               -DARROW_PARQUET=ON \
               -DARROW_WITH_BROTLI=ON \
               -DARROW_WITH_BZ2=ON \
               -DARROW_WITH_LZ4=ON \
               -DARROW_WITH_SNAPPY=ON \
               -DARROW_WITH_ZLIB=ON \
               -DARROW_WITH_ZSTD=ON \
               -DPARQUET_REQUIRE_ENCRYPTION=ON
         $ cmake --build arrow/cpp/build --target install -j4

      If multiple versions of Python are installed in your environment, you may have
      to pass additional parameters to CMake so that it can find the right
      executable, headers and libraries.  For example, specifying
      ``-DPython3_EXECUTABLE=<path/to/bin/python>`` lets CMake choose the
      Python executable which you are using.

      .. note::

         On Linux systems with support for building on multiple architectures,
         ``make`` may install libraries in the ``lib64`` directory by default. For
         this reason we recommend passing ``-DCMAKE_INSTALL_LIBDIR=lib`` because the
         Python build scripts assume the library directory is ``lib``

      .. note::

         If you have conda installed but are not using it to manage dependencies,
         and you have trouble building the C++ library, you may need to set
         ``-DARROW_DEPENDENCY_SOURCE=AUTO`` or some other value (described
         :ref:`here <cpp-build-dependency-management>`)
         to explicitly tell CMake not to use conda.

   .. tab-item:: Windows
      :sync: wins

      There are presets provided as a convenience for building C++ (see Linux and macOS
      tab). Here we will instead specify the individual components:

      .. code-block::

         $ mkdir arrow\cpp\build
         $ pushd arrow\cpp\build
         $ cmake -G "Ninja" ^
               -DCMAKE_INSTALL_PREFIX=%ARROW_HOME% ^
               -DCMAKE_UNITY_BUILD=ON ^
               -DARROW_COMPUTE=ON ^
               -DARROW_CSV=ON ^
               -DARROW_CXXFLAGS="/WX /MP" ^
               -DARROW_DATASET=ON ^
               -DARROW_FILESYSTEM=ON ^
               -DARROW_HDFS=ON ^
               -DARROW_JSON=ON ^
               -DARROW_PARQUET=ON ^
               -DARROW_WITH_LZ4=ON ^
               -DARROW_WITH_SNAPPY=ON ^
               -DARROW_WITH_ZLIB=ON ^
               -DARROW_WITH_ZSTD=ON ^
               ..
         $ cmake --build . --target install --config Release
         $ popd

Optional build components
^^^^^^^^^^^^^^^^^^^^^^^^^

There are several optional components that can be enabled or disabled by setting
specific flags to ``ON`` or ``OFF``, respectively. See the list of
:ref:`python-dev-env-variables` below.

You may choose between different kinds of C++ build types:

* ``-DCMAKE_BUILD_TYPE=Release`` (the default) produces a build with optimizations
  enabled and debugging information disabled;
* ``-DCMAKE_BUILD_TYPE=Debug`` produces a build with optimizations
  disabled and debugging information enabled;
* ``-DCMAKE_BUILD_TYPE=RelWithDebInfo`` produces a build with both optimizations
  and debugging information enabled.

.. seealso::
   :ref:`Building Arrow C++ <cpp-building-building>`.

   For any other C++ build challenges, see :ref:`cpp-development`.

In case you may need to rebuild the C++ part due to errors in the process it is
advisable to delete the build folder, see :ref:`python-dev-env-variables`.
If the build has passed successfully and you need to rebuild due to latest pull
from git main, then this step is not needed.

.. _pyarrow_build_section:

Build PyArrow
-------------

If you did build one of the optional components in C++, the equivalent components
will be enabled by default for building pyarrow. This default can be overridden
by setting the corresponding ``PYARROW_WITH_$COMPONENT`` environment variable
to 0 or 1, see :ref:`python-dev-env-variables` below.

To build PyArrow run:

.. tab-set::
   :sync-group: language

   .. tab-item:: Linux and macOS
      :sync: linux-macos

      .. code-block::

         $ pushd arrow/python
         $ python setup.py build_ext --inplace
         $ popd

   .. tab-item:: Windows
      :sync: wins

      .. code-block::

         $ pushd arrow\python
         $ python setup.py build_ext --inplace
         $ popd

      .. note::

         If you are using Conda with Python 3.9 or earlier, you must
         set ``CONDA_DLL_SEARCH_MODIFICATION_ENABLE=1``.

      .. note::

         With the above instructions the Arrow C++ libraries are not bundled with
         the Python extension. This is recommended for development as it allows the
         C++ libraries to be re-built separately.

         If you are using the conda package manager then conda will ensure the Arrow C++
         libraries are found. **In case you are NOT using conda** then you have to:

         * add the path of installed DLL libraries to ``PATH`` every time before
           importing ``pyarrow``, or
         * bundle the Arrow C++ libraries with ``pyarrow``.

         **Bundle Arrow C++ and PyArrow**

         If you want to bundle the Arrow C++ libraries with ``pyarrow``, set the
         ``PYARROW_BUNDLE_ARROW_CPP`` environment variable before building ``pyarrow``:

         .. code-block::

            $ set PYARROW_BUNDLE_ARROW_CPP=1
            $ python setup.py build_ext --inplace

         Note that bundled Arrow C++ libraries will not be automatically
         updated when rebuilding Arrow C++.

To set the number of threads used to compile PyArrow's C++/Cython components,
set the ``PYARROW_PARALLEL`` environment variable.

If you build PyArrow but then make changes to the Arrow C++ or PyArrow code,
you can end up with stale build artifacts. This can lead to
unexpected behavior or errors. To avoid this, you can clean the build
artifacts before rebuilding. See :ref:`python-dev-env-variables`.

By default, PyArrow will be built in release mode even if Arrow C++ has been
built in debug mode. To create a debug build of PyArrow, run
``export PYARROW_BUILD_TYPE=debug`` prior to running  ``python setup.py
build_ext --inplace`` above. A ``relwithdebinfo`` build can be created
similarly.

Self-Contained Wheel
^^^^^^^^^^^^^^^^^^^^

If you're preparing a PyArrow wheel for distribution (e.g., for PyPI), you’ll
need to build a self-contained wheel (including the Arrow and Parquet C++
libraries). This ensures that all necessary native libraries are bundled inside
the wheel, so users can install it without needing to have Arrow or Parquet
installed separately on their system.

To do this, pass the ``--bundle-arrow-cpp`` option to the build command:

.. code-block::

   $ pip install wheel  # if not installed
   $ python setup.py build_ext --build-type=$ARROW_BUILD_TYPE \
            --bundle-arrow-cpp bdist_wheel

This option is typically only needed for releases or distribution scenarios,
not for local development.

Editable install
^^^^^^^^^^^^^^^^

To install an editable PyArrow build, run the following command from the
``arrow/python`` directory:

.. code-block::

   pip install -e . --no-build-isolation

This creates an *editable install*, meaning changes to the Python source code
will be reflected immediately without needing to reinstall the package.
The ``--no-build-isolation`` flag ensures that the build uses your current
environment's dependencies instead of creating an isolated one. This is
especially useful during development and debugging.

.. _stale_artifacts:

Deleting stale build artifacts
------------------------------

When there have been changes to the structure of the Arrow C++ library or PyArrow,
a thorough cleaning is recommended as a first attempt to fixing build errors.

.. note::

   It is not necessarily intuitive from the error itself that the problem is due to stale artifacts.
   Example of a build error from stale artifacts is
   ``Unknown CMake command "arrow_keep_backward_compatibility"``.

To delete stale Arrow C++ build artifacts:

.. code-block::

   $ rm -rf arrow/cpp/build

To delete stale PyArrow build artifacts:

.. code-block::

   $ git clean -Xfd python

If using a Conda environment, there are some build artifacts that get installed in
``$ARROW_HOME`` (aka ``$CONDA_PREFIX``). For example, ``$ARROW_HOME/lib/cmake/Arrow*``,
``$ARROW_HOME/include/arrow``, ``$ARROW_HOME/lib/libarrow*``, etc.

These files can be manually deleted. If unsure which files to erase, one approach
is to recreate the Conda environment.

Either delete the current one, and start fresh:

.. code-block::

   $ conda deactivate
   $ conda remove -n pyarrow-dev

Or, less destructively, create a different environment with a different name.

Docker examples
---------------

If you are having difficulty building the Python library from source, take a
look at the `python/examples/minimal_build <https://github.com/apache/arrow/tree/main/python/examples/minimal_build>`_
directory which illustrates a complete build and test from source both with
the conda- and pip-based build methods.

Test
====

Now you are ready to install test dependencies and run :ref:`python-unit-testing`, as
described in development section.

.. _python-dev-env-variables:

Relevant components and environment variables
=============================================

List of relevant environment variables that can be used to build
PyArrow are:

.. list-table::
   :widths: 20 20 20
   :header-rows: 1

   * - PyArrow environment variable
     - Description
     - Default value
   * - ``PYARROW_BUILD_TYPE``
     - Build type for PyArrow (release, debug or relwithdebinfo), sets ``CMAKE_BUILD_TYPE``
     - ``release``
   * - ``PYARROW_CMAKE_GENERATOR``
     - Example: ``'Visual Studio 17 2022 Win64'``
     - ``''``
   * - ``PYARROW_CMAKE_OPTIONS``
     - Extra CMake and Arrow options (ex. ``"-DARROW_SIMD_LEVEL=NONE -DCMAKE_OSX_ARCHITECTURES=x86_64;arm64"``)
     - ``''``
   * - ``PYARROW_CXXFLAGS``
     - Extra C++ compiler flags
     - ``''``
   * - ``PYARROW_GENERATE_COVERAGE``
     - Setting ``Xlinetrace`` flag to true for the Cython compiler
     - ``false``
   * - ``PYARROW_BUNDLE_ARROW_CPP``
     - Bundle the Arrow C++ libraries
     - ``0`` (``OFF``)
   * - ``PYARROW_BUNDLE_CYTHON_CPP``
     - Bundle the C++ files generated by Cython
     - ``0`` (``OFF``)
   * - ``PYARROW_BUILD_VERBOSE``
     - Enable verbose output from Makefile builds
     - ``0`` (``OFF``)
   * - ``PYARROW_PARALLEL``
     - Number of processes used to compile PyArrow’s C++/Cython components
     - ``''``

The components being disabled or enabled when building PyArrow is by default
based on how Arrow C++ is build (i.e. it follows the ``ARROW_$COMPONENT`` flags).
However, the ``PYARROW_WITH_$COMPONENT`` environment variables can still be used
to override this when building PyArrow (e.g. to disable components, or to enforce
certain components to be built):

.. list-table::
   :widths: 30 30
   :header-rows: 1

   * - Arrow flags/options
     - Corresponding environment variables for PyArrow
   * - ``ARROW_GCS``
     - ``PYARROW_WITH_GCS``
   * - ``ARROW_S3``
     - ``PYARROW_WITH_S3``
   * - ``ARROW_AZURE``
     - ``PYARROW_WITH_AZURE``
   * - ``ARROW_HDFS``
     - ``PYARROW_WITH_HDFS``
   * - ``ARROW_CUDA``
     - ``PYARROW_WITH_CUDA``
   * - ``ARROW_SUBSTRAIT``
     - ``PYARROW_WITH_SUBSTRAIT``
   * - ``ARROW_FLIGHT``
     - ``PYARROW_WITH_FLIGHT``
   * - ``ARROW_ACERO``
     - ``PYARROW_WITH_ACERO``
   * - ``ARROW_DATASET``
     - ``PYARROW_WITH_DATASET``
   * - ``ARROW_PARQUET``
     - ``PYARROW_WITH_PARQUET``
   * - ``PARQUET_REQUIRE_ENCRYPTION``
     - ``PYARROW_WITH_PARQUET_ENCRYPTION``
   * - ``ARROW_ORC``
     - ``PYARROW_WITH_ORC``
   * - ``ARROW_GANDIVA``
     - ``PYARROW_WITH_GANDIVA``

Installing Nightly Packages
===========================

.. warning::
    These packages are not official releases. Use them at your own risk.

PyArrow has nightly wheels for testing purposes hosted at
`scientific-python-nightly-wheels
<https://anaconda.org/scientific-python-nightly-wheels/pyarrow>`_.

These may be suitable for downstream libraries in their continuous integration
setup to maintain compatibility with the upcoming PyArrow features,
deprecations, and/or feature removals.

To install the most recent nightly version of PyArrow, run:

.. code-block:: bash

    pip install \
      -i https://pypi.anaconda.org/scientific-python-nightly-wheels/simple \
      pyarrow