With linux how do Iinstall vcpkg?
- User
With linux how do Iinstall vcpkg?
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
With linux how do Iinstall vcpkg?
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Okay, installing vcpkg on Linux is straightforward. It involves cloning the repository and then running a bootstrap script.
Here's how to do it:
1. Install Prerequisites: vcpkg needs a few tools to build itself and the packages. The most common ones are Git, a C++ compiler (like g++), CMake, curl, zip, unzip, and tar.
For Debian/Ubuntu-based systems (like Ubuntu, Mint, Pop!_OS):
sudo apt update
sudo apt install git curl zip unzip tar pkg-config build-essential cmake ninja-build
(build-essential installs g++, make, etc. ninja-build is recommended for faster builds with CMake.)
For Fedora/RHEL-based systems (like Fedora, CentOS, Rocky Linux):
sudo dnf install git curl zip unzip tar pkgconfig gcc-c++ make cmake ninja-build
2. Clone the vcpkg Repository:
Choose a directory where you want to install vcpkg. A common place is your home directory or a dedicated development tools directory (e.g., ~/dev/tools/). Do not use sudo for this step.
# Navigate to where you want to clone it, e.g., your home directory
cd ~
# Clone the repository
git clone https://github.com/microsoft/vcpkg.git
# Navigate into the cloned directory
cd vcpkg
3. Bootstrap vcpkg: This step compiles vcpkg itself.
./bootstrap-vcpkg.sh
./bootstrap-vcpkg.sh -disableMetrics
4. (Highly Recommended) Integrate vcpkg with your build systems: This makes it easier for CMake (and MSBuild, though less common on Linux for C++) to find packages installed by vcpkg.
User-wide integration (for CMake): This is the easiest way to get started. It modifies your user-level CMake configuration.
./vcpkg integrate install
This will output a path to a CMake toolchain file. When you configure your CMake projects, vcpkg-installed libraries will generally be found automatically if you've done this.
Manual Toolchain (for CMake - More Control/Project-Specific): Alternatively, for more control or on CI systems, you can specify the vcpkg toolchain file when configuring your CMake project:
cmake -B build -S . -DCMAKE_TOOLCHAIN_FILE=[path-to-vcpkg]/scripts/buildsystems/vcpkg.cmake
Replace [path-to-vcpkg] with the actual path to your vcpkg installation (e.g., ~/vcpkg).
5. (Optional) Add vcpkg to your PATH:
This allows you to run vcpkg commands from any directory without needing to specify the full path to ~/vcpkg/vcpkg.
Open your shell's configuration file (e.g., ~/.bashrc, ~/.zshrc):
nano ~/.bashrc # or ~/.zshrc, or your preferred editor
Add the following line to the end of the file:
export PATH="$HOME/vcpkg:$PATH" # Adjust $HOME/vcpkg if you cloned it elsewhere
Save the file and then source it or open a new terminal:
source ~/.bashrc # or source ~/.zshrc
How to Use vcpkg:
Search for a package:
vcpkg search <package_name>
# Example:
vcpkg search fmt
Install a package:
vcpkg install <package_name>
# Example for the fmt library:
vcpkg install fmt
By default, this installs the x64-linux triplet. You can specify other triplets:
vcpkg install fmt:x86-linux # For 32-bit Linux
Using installed packages in CMake:
If you've run ./vcpkg integrate install, CMake should find packages automatically.
In your CMakeLists.txt:
cmake_minimum_required(VERSION 3.18) # Or your desired version
project(MyAwesomeProject LANGUAGES CXX)
find_package(fmt CONFIG REQUIRED) # Example for fmt library
add_executable(MyAwesomeProject main.cpp)
target_link_libraries(MyAwesomeProject PRIVATE fmt::fmt)
If you are using the manual toolchain file method, remember to pass -DCMAKE_TOOLCHAIN_FILE=... when configuring CMake.
List installed packages:
vcpkg list
Remove a package:
vcpkg remove <package_name>
Updating vcpkg: To update vcpkg to the latest version and get new package versions:
cd ~/vcpkg # Or your vcpkg directory
git pull
./bootstrap-vcpkg.sh
After updating, you might want to update your installed packages:
vcpkg upgrade --no-dry-run
That's it! You now have vcpkg installed and ready to manage your C/C++ dependencies on Linux.