280

In the new GitHub Actions, I am trying to install a package in order to use it in one of the next steps.

name: CI

on: [push, pull_request]

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

However this fails with

Run apt-get install libxml2-utils
  apt-get install libxml2-utils
  shell: /bin/bash -e {0}
E: Could not open lock file /var/lib/dpkg/lock-frontend - open (13: Permission denied)
E: Unable to acquire the dpkg frontend lock (/var/lib/dpkg/lock-frontend), are you root?
##[error]Process completed with exit code 100.

What's the best way to do this? Do I need to reach for Docker?

Timur Shtatland
  • 12,024
  • 2
  • 30
  • 47
Niklas
  • 3,753
  • 4
  • 21
  • 29
  • 9
    `sudo apt-get install libxml2-utils` – runwuf Sep 17 '19 at 23:27
  • 12
    The [docs](https://help.github.com/en/articles/virtual-environments-for-github-actions) say "The Linux and macOS virtual machines both run using passwordless `sudo`", so simply doing `sudo apt-get` should work, as @runwuf suggested. – rmunn Sep 18 '19 at 07:08

2 Answers2

398

The docs say:

The Linux and macOS virtual machines both run using passwordless sudo. When you need to execute commands or install tools that require more privileges than the current user, you can use sudo without needing to provide a password.

So simply doing the following should work:

- name: Install xmllint
  run: sudo apt-get install -y libxml2-utils
David Gardiner
  • 16,892
  • 20
  • 80
  • 117
rmunn
  • 34,942
  • 10
  • 74
  • 105
  • 4
    it would be nice if someone could offer insight on _why_ this might be done... – MichaelChirico Jun 14 '20 at 16:51
  • 8
    @Mike'Pomax'Kamermans: One explanation for `sudo: command not found` is that you created an environment variable named `PATH` in your action file overriding the system path used to locate commands. Ask me how I know... – Martin Liversage Jun 18 '20 at 20:12
  • 22
    not in this case though - I was using `act` for offline testing, and `act` apparently uses an image that doesn't _have_ sudo, and it's pretty nonsense (it's missing `lsb-release`, too, and a whoooole bunch of other things that make testing your github action basically meaningless) – Mike 'Pomax' Kamermans Jun 18 '20 at 20:31
  • 1
    `sudo` shouldn't be needed here. – Navid Khan Aug 27 '20 at 11:14
  • 11
    @NavidKhan Without sudo: `Could not open lock file /var/lib/dpkg/lock-frontend - open (13: Permission denied)` – 030 Apr 03 '21 at 13:35
  • 5
    If you use just this line, without having the environment set to automatically confirm the installation, it won't work. Please consider editing to add `-y` option to `sudo apt-get install`. – user Aug 11 '21 at 12:47
  • @Mike'Pomax'Kamermans Thanks for the heads up, I've been debating whether it would be worth my time to install and use `act` for the last few days. – Hashim Aziz Mar 07 '23 at 17:06
  • @HashimAziz to be fair, that was in mid-2020, so check their docs instead of trusting me from three years ago. – Mike 'Pomax' Kamermans Mar 07 '23 at 18:28
12

Please see this answer here: https://stackoverflow.com/a/73500415/2038264

cache-apt-pkgs-action can both install and cache the apt package, so your subsequent builds are fast. It is also easier to configure, just add the packages you want:

      - uses: awalsh128/cache-apt-pkgs-action@latest
        with:
          packages: dia doxygen doxygen-doc doxygen-gui doxygen-latex graphviz mscgen
          version: 1.0
congusbongus
  • 13,359
  • 7
  • 71
  • 99