How to install packages using apt in a GitHub Actions workflow?

I’m setting up a GitHub Actions workflow and I need to install a package to use it in later steps. However, I’m encountering permission errors when trying to execute the apt command for installation.

Here’s my current workflow setup:

name: CI

on: [push, pull_request]

jobs:
  translations:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v1
    - name: Install xmllint
      run: apt-get install libxml2-utils

I’m facing an error related to dpkg lock issues, which indicates I might not have the required permissions. What is the recommended way to install packages in this environment? Should I consider using sudo or is there another method to resolve this without involving Docker?

You need sudo for package installation in GitHub Actions. Ubuntu runners don’t give you root permissions by default - that’s why you’re getting dpkg lock errors. Just change your step to sudo apt-get install libxml2-utils and you’re good to go. I’ve used this in production workflows for 2+ years, no problems. Run sudo apt-get update first if you’re installing packages that aren’t in the base image, but libxml2-utils should be available. Ubuntu runners are minimal but they let you sudo for package management.

Yeah, this permission issue pops up all the time with GitHub Actions. SwiftCoder15’s sudo suggestion works, but I’ve got a better approach. Run sudo apt-get update && sudo apt-get install -y libxml2-utils as one command. The -y flag stops interactive prompts from hanging your workflow, and combining both operations cuts down on apt calls. GitHub runners often have stale package caches, so that update step is key for newer packages. Works reliably across different Ubuntu runner versions - I use this in all my CI/CD setups.

Both answers work, but I stopped managing package installs directly in CI. Too many cache issues and version conflicts across runners.

I use Latenode automation for dependency management now. It watches my repos and spins up the exact environment when workflows run. No more sudo commands or package steps in my YAML files.

Best part is consistency. Instead of crossing my fingers that the Ubuntu runner has what I need, Latenode containers come with everything pre-installed. Handles version pinning too, so package updates won’t break my builds.

10 minutes to set up, then you’re done. Much cleaner than stuffing package logic into every workflow.