This article is transcoded by 简悦 SimpRead, original address blog.you7n.com
KenLM is an efficient n-gram language model library, but encountered various issues when installing on Mac. After working through the night and morning, it was finally installed. This article records the method for successfully installing KenLM.
12 Feb 2025 •
KenLM is an efficient n-gram language model library, but encountered various issues when installing on Mac. After working through the night and morning, it was finally installed. This article records the method for successfully installing KenLM.
If you use Python version 3.10 or above, this solution might work for you. For other fixes, see:
- Modify
setup.py: Zhihu/98137373 - Modify Xcode header files: Zhihu/631044108
When trying to install kenlm using pip or pip3, it is very likely to fail. According to issues on GitHub, it seems there is some problem with the pip kenlm package (?)
Running pip3 install kenlm or pip3 install pypi-kenlm will give the following error:
![]()
Mainly showing:
Failed to build kenlm
ERROR: Failed to build installable wheels for some pyproject.toml based projects (kenlm)
So try to manually compile the GitHub version.
git clone https://github.com/kpu/kenlm
Use cd to enter the kenlm folder.
KenLM cannot compile properly under the latest Python 3.13 version, so use Python 3.10.
You can install it via brew:
Recreating the python3 symlink or setting the global Python version is too troublesome. Just install the local library directly via pip3.10.
Since you have already cd into the kenlm folder, install this folder directly:
At this point, the installation should be successful and you can test it. The kenlm/python/example.py is a test file, run it with python3.10:
python3.10 /Users/■■■/kenlm/python/example.py
If the result looks like the picture, it was successful:
![]()
That’s all.