Conan Cheat Sheet
Overview
Conan is a decentralized, open-source C/C++ package manager that supports multiple build systems, compilers, and platforms. It manages library dependencies, handles binary compatibility through profiles and settings, and integrates with CMake, Meson, MSBuild, and other build systems. Conan 2.x is the current major version with significant API changes from 1.x.
Conan uses recipes (conanfile.py or conanfile.txt) to define package requirements, build options, and configurations. It supports both local and remote package repositories, enabling teams to share prebuilt binaries and avoid redundant compilation across development environments.
Installation
Via pip
# Install latest Conan 2.x
pip install conan
# Install specific version
pip install conan==2.4.0
# Upgrade existing installation
pip install --upgrade conan
# Verify installation
conan --version
conan profile detect
macOS
brew install conan
Linux (system packages)
# Ubuntu/Debian (may be older version)
sudo apt install python3-conan
# Preferred: use pip for latest
pip3 install conan
Core Commands
| Command | Description |
|---|---|
conan install . | Install dependencies for current project |
conan create . | Build and package the current recipe |
conan search "*" | Search local cache for packages |
conan remote list | List configured remotes |
conan profile list | List available profiles |
conan profile detect | Auto-detect and create default profile |
conan graph info . | Show dependency graph |
conan remove "*" | Remove packages from local cache |
conan export . | Export recipe to local cache |
conan upload "*" | Upload packages to a remote |
Project Setup
conanfile.txt (Simple)
[requires]
zlib/1.3
boost/1.83.0
fmt/10.1.1
[generators]
CMakeDeps
CMakeToolchain
[options]
boost/*:shared=True
conanfile.py (Full Control)
from conan import ConanFile
from conan.tools.cmake import CMake, cmake_layout
class MyProjectConan(ConanFile):
name = "myproject"
version = "1.0.0"
settings = "os", "compiler", "build_type", "arch"
requires = (
"zlib/1.3",
"boost/1.83.0",
"fmt/10.1.1",
)
generators = "CMakeDeps", "CMakeToolchain"
def layout(self):
cmake_layout(self)
def build(self):
cmake = CMake(self)
cmake.configure()
cmake.build()
def package(self):
cmake = CMake(self)
cmake.install()
Installing Dependencies
# Install deps for current project
conan install . --output-folder=build --build=missing
# Install with specific profile
conan install . --profile=release --build=missing
# Install for Debug build
conan install . -s build_type=Debug --build=missing
# Install with options
conan install . -o "boost/*:shared=True" --build=missing
Profiles
Create and Manage Profiles
# Auto-detect default profile
conan profile detect
# List profiles
conan profile list
# Show profile content
conan profile show default
# Show effective profile for a path
conan profile show -pr myprofile
Custom Profile File
# ~/.conan2/profiles/gcc12-release
[settings]
os=Linux
compiler=gcc
compiler.version=12
compiler.libcxx=libstdc++11
build_type=Release
arch=x86_64
[buildenv]
CC=gcc-12
CXX=g++-12
[conf]
tools.cmake.cmaketoolchain:generator=Ninja
# Use custom profile
conan install . --profile=gcc12-release --build=missing
Configuration
Remote Repositories
# List remotes
conan remote list
# Add a remote
conan remote add myremote https://myserver.com/artifactory/api/conan/conan-local
# Remove a remote
conan remote remove myremote
# Login to remote
conan remote login myremote myuser -p mypassword
Global Configuration
# Show all config
conan config list
# Set config values
conan config set core:default_profile=myprofile
conan config set core.download:retry=5
# Install config from URL or path
conan config install https://github.com/myorg/conan-config.git
Advanced Usage
Creating Packages
# Build and create package in local cache
conan create . --build=missing
# Create with specific version
conan create . --version=2.0.0
# Test the package
conan test test_package/ mylib/1.0.0
# Export recipe only (no build)
conan export .
# Upload to remote
conan upload "mylib/1.0.0" -r myremote
Dependency Graph
# Show dependency info
conan graph info .
# Generate HTML dependency graph
conan graph info . --format=html > deps.html
# Lock dependencies
conan lock create .
# Install from lockfile
conan install . --lockfile=conan.lock
Cross-Compilation
# Create a cross-compile profile
cat > ~/.conan2/profiles/arm-linux << 'EOF'
[settings]
os=Linux
compiler=gcc
compiler.version=12
compiler.libcxx=libstdc++11
build_type=Release
arch=armv8
[buildenv]
CC=aarch64-linux-gnu-gcc
CXX=aarch64-linux-gnu-g++
EOF
# Build with host and build profiles
conan install . --profile:host=arm-linux --profile:build=default --build=missing
Editable Packages
# Make a local package editable for development
conan editable add /path/to/mylib mylib/1.0.0
# List editable packages
conan editable list
# Remove editable mode
conan editable remove mylib/1.0.0
Troubleshooting
| Issue | Solution |
|---|---|
| Missing binary for settings | Use --build=missing to build from source |
| Profile not detected | Run conan profile detect to create default |
| Version conflict in deps | Use conan graph info . to inspect the graph |
| CMake can’t find package | Ensure CMakeDeps generator is listed and build folder is correct |
| Upload authentication error | Run conan remote login <remote> <user> |
| Recipe changed but cache stale | Run conan remove "pkg/*" then reinstall |
# Clear entire local cache
conan remove "*" -c
# Rebuild specific package
conan install . --build="zlib/*"
# Debug: verbose output
conan install . -v
# Check for outdated dependencies
conan graph info . --format=json | python3 -m json.tool