Unleash the Power of vcpkg on Ubuntu

default image

As a fellow programmer and technology enthusiast, I want to let you in on a game-changing tool that could seriously improve your C/C++ development experience – especially on Linux. I‘m talking about Microsoft‘s vcpkg package manager.

Let me tell you, vcpkg is an absolute must-have for any C++ dev working on Ubuntu or other Linux distros. I wish I had discovered it years ago! It would have saved me countless hours of headache dealing with library dependencies, incompatible versions, and build issues across projects.

In this comprehensive guide, I‘ll show you everything you need to get up and running with vcpkg on Ubuntu. I‘ll explain what makes vcpkg so useful, take you through a step-by-step installation, share plenty of tips, and arm you with the knowledge to wield this extremely handy tool. Let‘s get started!

Why vcpkg is a Game Changer for C++ Developers

As C++ developers, we know how frustrating dependency management can be. The language itself provides no built-in package management. That means every time you want to use a new library, you have to manually:

  • Find the right version of the library for your needs
  • Make sure it works on your target platforms
  • Download or build it from source
  • Handle any dependencies it requires
  • Integrate it into your build system
  • Repeat this process anytime you update the library or change target platforms!

And that‘s assuming the library even has releases pre-built for your platform. Often you‘ll be stuck building from source and pulling your hair out over missing headers or configs. Plus good luck getting all those transitive dependencies sorted.

Does this sound familiar? If so, you‘ll be delighted to learn there‘s now a much better way – vcpkg.

vcpkg fundamentally solves these problems by giving you:

  • A unified package manager – No more hunting for libraries. Just run vcpkg install <lib>.

  • Cross-platform by default – Libraries are built for Linux, Windows, MacOS, and more.

  • Automatic dependency resolution – Just declare the libraries you need, vcpkg handles the rest.

  • Simple integration with CMake – Link prebuilt binaries or embed vcpkg directly.

  • A vast library catalog – Over 1,500 of the most popular C++ libraries are available as "ports".

  • Straightforward command line interfacevcpkg install, vcpkg remove, etc. Easy to use.

  • Lightning fast updates – Already built binaries mean no more waiting for full builds.

  • Active open source project – Backed by Microsoft and a community of contributors.

With over 30 million downloads so far, vcpkg has quickly become the package manager of choice for C++ on Windows and Linux. It‘s made dependency hell a thing of the past for countless developers.

Let‘s look at a quick example. Say you‘re developing a game and decide you want to add physics using the popular open source Bullet library. Here‘s all it takes with vcpkg:

vcpkg install bullet3d

One simple command gives you the latest version of Bullet built and ready for your Ubuntu machine, along with any dependencies it needs like linear math lib. Just #include and start using it in your game! No messing with CMake, configs, or Manual builds.

This ease of use for acquiring and updating libraries is an absolute game-changer compared to the old ways of doing C++ dependency management. You get to focus on just writing code instead of builds.

Now let‘s get vcpkg installed on your Ubuntu system so you can start enjoying this convenience and productivity boost!

vcpkg Installation Prerequisites

Before we install vcpkg itself, there are just a few standard developer tools we need as prerequisites:

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

This grabs the latest package lists, essential build tools like GCC and Make, utilities for handling compressed files, Git for source control, and pkg-config for managing library configurations.

With those basics covered, we‘re ready to install vcpkg. There are a couple different options, but I prefer using the GitHub repository which ensures you get the latest version.

Installing vcpkg from GitHub

Here are the steps to cleanly install the latest vcpkg on Ubuntu from GitHub:

  1. Grab the GitHub archive and extract the contents:
wget -O
  1. Rename the extracted directory and move it to your preferred install location (I use /opt):
mv vcpkg-master /opt/vcpkg
  1. Build the vcpkg bootstrapping script:
  1. Optionally, link vcpkg to your path for easy access:
sudo ln -s /opt/vcpkg/vcpkg /usr/local/bin/vcpkg

And that‘s it! vcpkg is now installed and ready to manage your C++ libraries. Be sure to run the bootstrap script whenever you update vcpkg to rebuild it with the latest changes.

Testing vcpkg with a Simple Install

Let‘s confirm everything is working by installing a library with vcpkg. zlib is a good test case since it‘s a common dependency for other projects:

vcpkg install zlib

After downloading the source and building it, vcpkg will report zlib was installed successfully. Now you can check your installed packages:

vcpkg list

You should see zlib listed! This means vcpkg is ready to start automating your library management.

Utilizing vcpkg‘s Amazing Library Catalog

Okay, now for the fun part – let‘s talk about the huge selection of libraries available through vcpkg!

Over 1,500 of the most popular C++ libraries have been ported for easy installation. You‘ll find options for just about any task including:

  • App frameworks – Qt, SDL, SFML, Boost
  • Video/audio – FFmpeg, OpenAL, GStreamer
  • Image/video processing – OpenCV, OpenImageIO
  • 3D graphics – DirectXTex, OpenGL
  • Physics engines – Box2D, Bullet, PhysX
  • Math – Eigen, Ceres Solver
  • Machine Learning – Caffe2, Tensorflow, Torch
  • Game engines – Chipmunk2D, raylib
  • Simulations – Simbody, RVO2
  • Scripting – Lua, ChaiScript

Plus utility libraries for IO, compression, containers, logging, testing, and more. Browse the full list of vcpkg ports to see everything available.

Here are some of my personal favorites to supercharge C++ projects:

  • OpenCV – State of the art computer vision algorithms
  • Vulkan – High performance graphics and compute
  • nlohmann/json – Excellent JSON library in C++17
  • spdlog – Super fast logging with terrific docs
  • abseil – Collection of useful C++ helper libraries
  • openssl – Encryption, decryption, and SSL/TLS

With just a simple vcpkg install, you can tap into these amazing libraries that would normally take ages to properly build and integrate in C++. It opens up so many possibilities!

Let‘s say I‘m building a robotics project and want to add object recognition. Just one line gives me the full power of OpenCV‘s advanced vision algorithms:

vcpkg install opencv4

Now I can stay focused on my robot‘s logic instead of wrestling with building OpenCV and all its dependencies like Protobuf and VGG. That productivity boost adds up to huge time savings.

Smooth Sailing with vcpkg Triplets

Now for a pro tip! By default vcpkg builds "triplets" targeting 64-bit Linux. A triplet defines the target platform and architecture, for example x64-windows or x64-linux.

You can customize the target triplet to ensure all your libraries build for the same platform:

vcpkg install <lib> --triplet x64-linux

This consistent triplet approach prevents a lot of headaches when library versions mismatch or get built for the wrong platform. I definitely recommend it for smooth sailing with vcpkg!

Expert Tips for Mastering vcpkg

Alright, you‘ve got vcpkg installed and are ready to start simplifying your dependency management. Here are some expert tips I‘ve gathered to help you master vcpkg like a pro:

  • Use vcpkg as a Git submodule – Makes it easier to update vcpkg itself when building projects.

  • Enable the triplet file – Creates a triplets.json file to explicitly set your target platform.

  • Link libraries statically – Avoid portability issues with dynamic/shared libraries.

  • Designate a vcpkg root directory – Keeps library builds consolidated instead of polluting your system.

  • Create a common "baseline" – Specify a set of core libraries to always install upfront.

  • Use vcpkg with CI/CD systems – Automate library restores and caching to speed up builds.

  • Employ tripwire when publishing releases – Get alerted about incompatible library updates that could break clients.

  • Contribute new ports – If your favorite library isn‘t available, submit a port yourself to the community catalog.

Following modern C++ package management practices with vcpkg unlocks huge productivity benefits compared to old-school manual dependency wrangling. I hope these tips help you on your journey to dependency management bliss!

Frequently Asked vcpkg Questions

Let‘s wrap up with answers to some common questions about vcpkg from fellow developers:

How do I update vcpkg itself?

Simply git pull in the vcpkg installation directory to fetch the latest changes. Then re-run the bootstrap script.

Can I install vcpkg system-wide for all users?

Yes, by default vcpkg is installed locally. But you can pass –system to the bootstrap script to install it globally.

What is the "CONTROL" file used for?

This contains metadata for each port such as the version and dependencies.

How do I remove a library?

Use vcpkg remove <lib> to uninstall the library and binaries.

Can I build libraries from a private repository?

Absolutely! See the docs on registries for details.

What‘s the difference between buildtrees and packages?

Buildtrees contain the temporary build artifacts while packages hold the installed binaries.

I hope those clear up some common questions! The vcpkg docs are also a fantastic resource for any other issues.

Go Forth and Use vcpkg for C++ Greatness!

In closing, I sincerely hope this guide has empowered you to finally tame C++ dependency management with the amazing vcpkg. No more grueling hours wasted debugging arcane build issues just to use a new library. Let vcpkg handle the hard work for you!

Now that you‘ve got vcpkg mastered, I can‘t wait to see the next-level C++ projects you‘ll be able to build faster than ever. This tool will open up so many possibilities for your code. Whenever you need a new library, vcpkg has got your back.

So go forth and show the world what C++ and vcpkg can do! I‘m rooting for you to create something truly great. If you run into questions along the way, I‘m always happy to help a fellow developer. The C++ community is lucky to have awesome contributors making tools like vcpkg.

Happy coding my friend!


Written by Alexis Kestler

A female web designer and programmer - Now is a 36-year IT professional with over 15 years of experience living in NorCal. I enjoy keeping my feet wet in the world of technology through reading, working, and researching topics that pique my interest.