Install from source


Users who have issues about installing k2 from source are mostly installing PyTorch with conda install.

We suggest that you install PyTorch using pip install.


It supports Linux (CPU + CUDA), macOS (CPU), and Windows (CPU + CUDA).


You can pass the option -DK2_WITH_CUDA=OFF to cmake to build a CPU only version of k2. In that case, you have to install a CPU version of PyTorch; otherwise, you will get a CMake configuration error, saying that cuDNN cannot be found.

Before compiling k2, some preparation work has to be done:

  • Have a compiler supporting at least C++14, e.g., GCC >= 7.0, Clang >= 3.4.

  • Install CMake. CMake 3.11.0 and 3.18.0 are known to work.

  • Install Python3. Please pass --enabled-shared to ./configure if you install Python from source.

  • Install PyTorch.

  • Install CUDA toolkit and cuDNN. Please refer to CUDA and cuDNN.


You can use pip install cmake to install the latest version of CMake.


cudatoolkit installed by conda install cannot be used to compile k2.

Please follow CUDA and cuDNN to install cudatoolkit and cuDNN.

After setting up the environment, we are ready to build k2:

git clone
cd k2
export K2_MAKE_ARGS="-j6"
python3 install

That is all you need to run.


We use export K2_MAKE_ARGS="-j6" to pass -j6 to make to reduce compilation time. If you have many GPUs and enough RAM, you can choose a larger value.


If you plan to run k2 on a different machine than the one you used to build k2 and the two machines have different types of GPUs, please use the following commands to install k2.

git clone
cd k2
python3 install

Otherwise, you may get some error like below when running k2:

[F] /xxx/k2/k2-latest/k2/csrc/eval.h:147:void k2::EvalDevice(cudaStream_t,
int32_t, LambdaT&) [with LambdaT = __nv_dl_wrapper_t<__nv_dl_tag<k2::Array1<int>
(*)(std::shared_ptr<k2::Context>, int, int, int), k2::Range<int>, 1>, int*,
int, int>; cudaStream_t = CUstream_st*; int32_t = int] Check failed:
e == cudaSuccess (98 vs. 0)  Error: invalid device function.

To test that k2 is installed successfully, you can run:

$ python3
Python 3.8.6 (default, Dec  2 2020, 15:56:31)
[GCC 7.5.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import k2
>>> s = '''
... 0 1 -1 0.1
... 1
... '''
>>> fsa = k2.Fsa.from_str(s)
>>> print(fsa)
k2.Fsa: 0 1 -1 0.1
properties_str = "Valid|Nonempty|TopSorted|TopSortedAndAcyclic|ArcSorted|ArcSortedAndDeterministic|EpsilonFree|MaybeAccessible|MaybeCoaccessible".

To uninstall k2, run:

pip uninstall k2

Read more if you want to run the tests.

sudo apt-get install graphviz
git clone
cd k2
pip3 install -r ./requirements.txt
mkdir build_release
cd build_release
cmake -DCMAKE_BUILD_TYPE=Release ..
# If you installed cudatoolkit using conda install -y -c nvidia cudatoolkit=X cudnn=Y,
# source the conda environemt and change the cmake command to:
make -j
make test

To run tests in parallel:

make -j
ctest --output-on-failure --parallel <JOBNUM>
# e.g., ctest --output-on-failure --parallel 5