Conan安装依赖报错,如何配置使其使用.conan目录而非.conan2?
问题背景
执行以下命令安装依赖时:
conan install .. --build=missing --settings compiler.libcxx="libstdc++11"
出现报错:
ERROR: The default build profile '/home/sohail/.conan2/profiles/default' doesn't exist.
You need to create a default profile (type 'conan profile detect' command)
或需指定自定义配置文件:'--profile:build=<myprofile>'
排查发现家目录同时存在 .conan(包含 profiles 文件夹)和 .conan2(无 profiles 文件夹),在 ~/.bashrc 中设置 CONAN_USER_HOME=$HOME/.conan 后变量生效,但执行命令仍报相同错误;将 .conan 中的 profiles 复制到 .conan2 后,又因 profile 格式不兼容报错:
ERROR: Error reading '/home/sohail/.conan2/profiles/default' profile:
ConfigParser: Unrecognized field 'build_requires'
使用环境为 CLion 2021.2.4 内置终端,需配置让 Conan 使用 .conan 目录。
解决步骤
1. 替换环境变量(核心操作)
Conan 2.x 已废弃 CONAN_USER_HOME 环境变量,改用 CONAN_HOME 指定配置目录。修改 ~/.bashrc:
export CONAN_HOME=$HOME/.conan
执行以下命令使配置立即生效:
source ~/.bashrc
验证变量是否生效:
echo $CONAN_HOME
返回 /home/sohail/.conan 即为正确。
2. 修复 Profile 格式兼容问题
.conan 中的 profile 是 Conan 1.x 格式,Conan 2.x 不支持 build_requires 字段,需调整:
- 打开
~/.conan/profiles/default文件 - 删除或注释掉所有包含
build_requires的行 - 若存在
[build_requires]区块,替换为 Conan 2.x 支持的[tool_requires]区块(按需调整内容)
3. 验证配置
重新执行原 conan install 命令,确认报错消失,依赖正常构建。
备选方案:临时指定 Profile
若不想全局修改环境变量,可在每次执行命令时直接指定 .conan 下的 profile:
conan install .. --build=missing --settings compiler.libcxx="libstdc++11" --profile:build=$HOME/.conan/profiles/default --profile:host=$HOME/.conan/profiles/default
补充说明
CLion 内置终端会读取系统环境变量,若修改 .bashrc 后配置未生效,可重启 CLion 或在终端手动执行 source ~/.bashrc 加载最新配置。
内容的提问来源于stack exchange,提问作者Muhammad Sohail

