MacOS Mac M1/M2/M3上如何安装plyvel?

MacOS Mac M1/M2/M3上如何安装plyvel?这个问题困扰了很久,终于搞定了,写一个自问自答的帖子记录一下。

喜欢这个问题 | 分享 | 新建回答

回答

jerkzhang

Oct 6, 2024
1 赞

这个问题的关键在于leveldb版本不能是1.23,现行的brew install leveldb只支持leveldb1.23,所以要选择1.21版本或者1.22版本,这里使用1.21版本,安装源码包的位置如下:

https://github.com/google/leveldb/archive/1.21.tar.gz

wget https://github.com/google/leveldb/archive/1.21.tar.gz即可下载,然后手动双击解压:

cd leveldb-1.21
mkdir build
cd build
cmake ..
make
sudo make install

之后安装好后会显示被安装的位置:

Install the project...
-- Install configuration: ""
-- Installing: /usr/local/lib/libleveldb.a
-- Installing: /usr/local/include/leveldb/c.h
-- Installing: /usr/local/include/leveldb/cache.h
-- Installing: /usr/local/include/leveldb/comparator.h
-- Installing: /usr/local/include/leveldb/db.h
-- Installing: /usr/local/include/leveldb/dumpfile.h
-- Installing: /usr/local/include/leveldb/env.h
-- Installing: /usr/local/include/leveldb/export.h
-- Installing: /usr/local/include/leveldb/filter_policy.h
-- Installing: /usr/local/include/leveldb/iterator.h
-- Installing: /usr/local/include/leveldb/options.h
-- Installing: /usr/local/include/leveldb/slice.h
-- Installing: /usr/local/include/leveldb/status.h
-- Installing: /usr/local/include/leveldb/table_builder.h
-- Installing: /usr/local/include/leveldb/table.h
-- Installing: /usr/local/include/leveldb/write_batch.h
-- Installing: /usr/local/lib/cmake/leveldb/leveldbTargets.cmake
-- Installing: /usr/local/lib/cmake/leveldb/leveldbTargets-noconfig.cmake
-- Installing: /usr/local/lib/cmake/leveldb/leveldbConfig.cmake
-- Installing: /usr/local/lib/cmake/leveldb/leveldbConfigVersion.cmake

然后再系统路径描述中去设置leveldb的位置

sudo vim ~/.zshrc

添加如下内容:

export LDFLAGS="-L/usr/local/include/leveldb/"
export CPPFLAGS="-I/usr/local/include/leveldb/"
export LD_LIBRARY_PATH=/usr/local/include/leveldb/:$LD_LIBRARY_PATH

之后关闭终端后重新打开终端即可生效。

最后pip3安装plyvel即可

CFLAGS="-I/usr/local/include/leveldb/ -fno-rtti" LDFLAGS="-L/usr/local/include/leveldb/"  pip3 install plyvel --no-cache-dir

尝试一下吧:

import plyvel

肯定成功了。