The Ultimate Guide to Installing vcpkg on Ubuntu in 2023

Are you a C or C++ developer looking to streamline your library management? Meet vcpkg – an open-source package manager that makes it a breeze to acquire and manage over 1,500 libraries in your projects. Maintained by Microsoft‘s C++ team and a community of contributors, vcpkg has become a go-to tool for many programmers.

In this in-depth guide, we‘ll walk through the process of installing vcpkg on Ubuntu step-by-step. By the end, you‘ll be ready to leverage vcpkg‘s extensive library collection in your own development work. Let‘s get started!

What is vcpkg?

Before we dive into installation, let‘s clarify what vcpkg is and why it‘s so useful. In a nutshell, vcpkg is a cross-platform package manager focused on C and C++ libraries. It allows you to easily download, build, and manage open-source libraries in a unified way.

Why is this valuable? Traditionally, using third-party libraries in C/C++ projects could be a huge headache. You‘d have to manually download the library code, configure the build system, resolve any dependencies, and integrate it into your project. Vcpkg abstracts this complexity by providing a central tool to automate fetching, building, and organizing libraries.

Microsoft created vcpkg to simplify package management for their own developers, but graciously released it as open-source. Beyond the Microsoft ecosystem, it now supports a vast collection of cross-platform and platform-specific libraries. Whether you need Boost, OpenCV, or SQLite in your Ubuntu project, vcpkg makes installation a single command away.

Prerequisites for Installing vcpkg

With the background out of the way, let‘s ensure you have the necessary tools to install vcpkg. On a standard Ubuntu system, you‘ll want to run the following commands in a terminal to install core build tools if you don‘t already have them:

sudo apt update 
sudo apt install build-essential pkg-config tar curl zip unzip

This will fetch the latest package information and install essentials like the C/C++ compilers, pkg-config for library detection, and archiving tools for download/extraction.

Installing vcpkg on Ubuntu

Now you‘re ready to fetch and install vcpkg itself. The vcpkg source is actively developed on GitHub, so we‘ll download the latest release directly. Follow these steps:

  1. Download the vcpkg source code

    curl -L -o vcpkg.tar.gz https://github.com/microsoft/vcpkg/archive/refs/heads/master.tar.gz
  2. Extract the archive

    tar xf vcpkg.tar.gz
  3. Move the extracted directory to a convenient location like /opt/

    sudo mv vcpkg-master /opt/vcpkg
  4. Navigate to the vcpkg directory

    cd /opt/vcpkg
  5. Run the vcpkg bootstrapper script

    ./bootstrap-vcpkg.sh

The bootstrapper will download the latest vcpkg release, configure it for your system, and build it. This may take a few minutes.

  1. (Optional) Add vcpkg to your shell‘s PATH
    To make running vcpkg more convenient, you can add its location to your PATH environment variable. Open your shell‘s configuration file (like .bashrc or .zshrc) and append the following line:
export PATH="/opt/vcpkg:$PATH"

Then run source ~/.bashrc (or re-open your terminal) for the change to take effect.

Congrats! Vcpkg is now fully installed on your Ubuntu system. Let‘s verify it‘s working properly.

Testing Your vcpkg Installation

To test that vcpkg is installed correctly, we can use it to download and build a sample library. Let‘s try installing the popular zlib compression library:

vcpkg install zlib

You should see vcpkg download the zlib source code, compile it, and integrate it into the vcpkg directory structure. Once it completes successfully, you can see it listed in vcpkg‘s installed libraries:

vcpkg list

Look for a line like "zlib:x64-linux" in the output to confirm zlib is installed for your system‘s architecture.

Using vcpkg in Your Projects

Now that you have vcpkg working, let‘s discuss how to actually use vcpkg-installed libraries in your own projects.

The vcpkg integrate install command sets up system-wide CMake toolchain files and environment variables to make using vcpkg seamless. After running this, any CMake-based project can find vcpkg-installed libraries automatically.

However, for non-CMake projects or more complex use cases, you‘ll need to manually specify the paths to the libraries and headers. Vcpkg organizes installed packages in the /opt/vcpkg/packages directory, with a subfolder for each library. You can find the compiled library binaries in lib/ and the headers in include/ within each package directory.

To link an installed library, add the appropriate -I include flag and -L library path flag to your compiler invocations or build system configuration pointing to the vcpkg subdirectories.

For example, to use the zlib library we installed, you might add:

-I/opt/vcpkg/packages/zlib_x64-linux/include 
-L/opt/vcpkg/packages/zlib_x64-linux/lib

Keeping vcpkg Up-to-Date

The collection of libraries available in vcpkg is constantly growing, and new versions are regularly released. To get access to the latest vcpkg has to offer, you‘ll want to periodically update your local vcpkg installation.

To do this, navigate to your vcpkg directory (cd /opt/vcpkg) and run:

git pull
./bootstrap-vcpkg.sh

This will fetch the latest vcpkg source code and rebuild your local copy. You can then update your installed libraries to the latest available versions with:

vcpkg upgrade

Uninstalling vcpkg

If you ever need to remove vcpkg from your Ubuntu system, the process is straightforward:

  1. Delete the vcpkg directory

    sudo rm -rf /opt/vcpkg
  2. (If added) Remove the vcpkg path from your shell configuration file

  3. Uninstall any auto-installed vcpkg dependencies

    sudo apt autoremove

This will completely remove vcpkg and any libraries you had installed with it.

Power-up Your C++ Development with vcpkg

Vcpkg is a potent addition to any C/C++ programmer‘s toolkit, especially when targeting multiple platforms. By delegating library management to vcpkg, you can focus on actually using those libraries to craft amazing software.

While we focused on Ubuntu in this guide, vcpkg delivers its conveniences across Windows, macOS, and Linux. It‘s just one of many tools, including CMake, Conan, and platform-specific package managers, that are valuable to master as a C/C++ developer.

Moreover, vcpkg enables access to cutting-edge and obscure libraries you may never have experimented with otherwise. Its ease of use makes diving into machine learning with LibTorch, manipulating images with OpenCV, or processing data with Abseil all the more accessible.

I encourage you to browse vcpkg‘s massive package collection and see what gems you can put to work in your next project. With vcpkg and Ubuntu, you‘re ready to harness the full potential of open-source, cross-platform C++ development.

Did you like this post?

Click on a star to rate it!

Average rating 0 / 5. Vote count: 0

No votes so far! Be the first to rate this post.