333

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?

1
  • 9
    sudo apt-get install libxml2-utils Commented Sep 17, 2019 at 23:27

3 Answers 3

468

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 install -y libxml2-utils
Sign up to request clarification or add additional context in comments.

10 Comments

it would be nice if someone could offer insight on why this might be done...
@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...
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)
@NavidKhan Without sudo: Could not open lock file /var/lib/dpkg/lock-frontend - open (13: Permission denied)
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.
|
28

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

3 Comments

This action is not maintained and buggy. For example, it complains that package list is empty when it's actually not.
At the moment it looks like it's actively maintained. So there's a good chance that your issue has also been resolved by now.
Happy to see that, thanks for noticing.
2

The error is hinting the problem

E: Could not open lock file /var/lib/dpkg/lock-frontend - open (13: Permission denied)

Its a permission issue. So nice and simple, run it with sudo.

Sample example below

jobs:
  test:
    name: Unit test
    runs-on: ubuntu-latest
    steps:
      - name: Checkout code
        uses: actions/checkout@v4
      - name: ⚙️ Install lcov
        run: |
          sudo apt-get update
          sudo apt-get -y install lcov

Here the github action is installing lcov a tool to perform code coverage.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.