Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ChromeHeadless can't run as root with --no-sandbox anymore #158

Open
boboldehampsink opened this issue Oct 25, 2017 · 25 comments
Open

ChromeHeadless can't run as root with --no-sandbox anymore #158

boboldehampsink opened this issue Oct 25, 2017 · 25 comments

Comments

@boboldehampsink
Copy link

boboldehampsink commented Oct 25, 2017

After updating to Chrome 62 and Chromedriver 2.33, I get the following error when running karma tests with ChromeHeadless:

  | 25 10 2017 08:47:40.197:ERROR [launcher]: Cannot start ChromeHeadless
  | [1025/084740.006078:ERROR:zygote_host_impl_linux.cc(88)] Running as root without --no-sandbox is not supported. See https://crbug.com/638180.

This is in Docker running Ubuntu 16

@boboldehampsink
Copy link
Author

boboldehampsink commented Oct 25, 2017

Workaround:

    browsers: ['ChromeHeadlessNoSandbox'],
    customLaunchers: {
      ChromeHeadlessNoSandbox: {
        base: 'ChromeHeadless',
        flags: ['--no-sandbox']
      }
    },

@tariqbuilds
Copy link

@boboldehampsink Is it assumed that your actual configuration also has --headless and --remote-debugging=9222 in the flags array?

@boboldehampsink
Copy link
Author

When specifying the browser as ChromeHeadless this is automatically added, yes.

@tariqbuilds
Copy link

This worked for us. Thanks, @boboldehampsink

@sebastian-burdun
Copy link

Worked for me too. Thanks.

gsalaun1 added a commit to gsalaun1/tech-radar-jhipster that referenced this issue Dec 3, 2017
@germs12
Copy link

germs12 commented Dec 19, 2017

Worked for me too. Thanks guys!

Keywords: CodeBuild, Karma, Chrome

@myluckagain
Copy link

myluckagain commented Dec 23, 2017

boboldehampsink, sorry, but where should this workaround be written? Is it about docker?

@boboldehampsink
Copy link
Author

In karma config

@DragonRus
Copy link

DragonRus commented Dec 31, 2017

Sorry for the rookie question: and where should I put this text?

browsers: ['ChromeHeadlessNoSandbox'],
customLaunchers: {
ChromeHeadlessNoSandbox: {
base: 'ChromeHeadless',
flags: ['--no-sandbox']
}
},

Where is this karma config?

@alexbainter
Copy link

@DragonRus By default, most projects have Karma configuration stored in a file named "karma.conf.js." However, that is just the default; the configuration file could be named anything and passed to Karma like this: karma start some-karma-config.js.

If your project doesn't have a configuration file, you can generate it by running karma init from a command line. See the Karma documentation.

@oliversalzburg
Copy link

I was not aware of the ability to define custom launchers. I would consider it very helpful if this was added to the README.

openstack-gerrit pushed a commit to openstack-archive/openstack-health that referenced this issue Mar 30, 2018
Motivation for this change:
  As a tester, I want to execute npm unit tests in a headless
  chrome environment.

Notes on implementation:
  * Running headless chrome requires verion chrome 59 or greater[1]
  * --no-sandbox flag is a workaround for running as root[2]
  * HeadlessChrome base was added in karma-chrome-launcher v2.1.0[3]
    - upgrading to this package caused a CHROME_BIN not set error[4].
      Setting CHROME_BIN to chromium-browser fixes the issue.
  * Using Chrome without the upgraded packages causes an error during
    the coverage report generation
    - TypeError: Cannot read property 'start' of undefined[5]

[1] https://github.com/karma-runner/karma-chrome-launcher#available-browsers
[2] karma-runner/karma-chrome-launcher#158
[3] https://github.com/karma-runner/karma-chrome-launcher/releases/tag/v2.1.0
[4] http://paste.openstack.org/show/715433/
[5] https://review.openstack.org/#/c/456556/9

Change-Id: I907708d73b5fe5345f3a56d8d638c51e7d3aa6f7
wmfgerrit pushed a commit to wikimedia/oojs-core that referenced this issue Apr 3, 2018
The Travis CI build for wikimedia/oojs has been failing since
January 2018 due to an incompatibility between Travis' Docker
configuration, and Chrome's sandboxing feature.

The --no-sandbox flag is a documented workaround. The alternative
being to switch to sudo-enabled builds, but those are slower, and
we don't need sandboxing within Travis CI docker builds anyway.

https://docs.travis-ci.com/user/chrome
karma-runner/karma-chrome-launcher#158

Also:
* Rename `saucelabs.browsers.js` to `karma.browsers.js`.
* Rename 'qunit karma:other' to 'qunit karma:firefox'.

Change-Id: I3b688c9ef12e0c156bb9908d61d959adbc41276f
@AlexanderKozhevin
Copy link

I keep getting smth like this in bitbucket pipeline.

02 05 2018 19:40:16.221:WARN [karma]: No captured browser, open http://localhost:9876/
02 05 2018 19:40:16.230:INFO [karma]: Karma v2.0.2 server started at http://0.0.0.0:9876/
02 05 2018 19:40:16.230:INFO [launcher]: Launching browser Chrome with unlimited concurrency
02 05 2018 19:40:16.236:INFO [launcher]: Starting browser Chrome
02 05 2018 19:40:16.656:ERROR [launcher]: Cannot start Chrome
	[79:79:0502/194016.598825:ERROR:zygote_host_impl_linux.cc(90)] Running as root without --no-sandbox is not supported. See https://crbug.com/638180.
02 05 2018 19:40:16.656:ERROR [launcher]: Chrome stdout: 
02 05 2018 19:40:16.656:ERROR [launcher]: Chrome stderr: [79:79:0502/194016.598825:ERROR:zygote_host_impl_linux.cc(90)] Running as root without --no-sandbox is not supported. See https://crbug.com/638180.
02 05 2018 19:40:16.676:INFO [launcher]: Trying to start Chrome again (1/2).
02 05 2018 19:40:16.883:ERROR [launcher]: Cannot start Chrome
	[90:90:0502/194016.759919:ERROR:zygote_host_impl_linux.cc(90)] Running as root without --no-sandbox is not supported. See https://crbug.com/638180.
02 05 2018 19:40:16.883:ERROR [launcher]: Chrome stdout: 
02 05 2018 19:40:16.883:ERROR [launcher]: Chrome stderr: [90:90:0502/194016.759919:ERROR:zygote_host_impl_linux.cc(90)] Running as root without --no-sandbox is not supported. See https://crbug.com/638180.
02 05 2018 19:40:16.919:INFO [launcher]: Trying to start Chrome again (2/2).
02 05 2018 19:40:17.145:ERROR [launcher]: Cannot start Chrome
	[101:101:0502/194017.008599:ERROR:zygote_host_impl_linux.cc(90)] Running as root without --no-sandbox is not supported. See https://crbug.com/638180.
02 05 2018 19:40:17.145:ERROR [launcher]: Chrome stdout: 
02 05 2018 19:40:17.145:ERROR [launcher]: Chrome stderr: [101:101:0502/194017.008599:ERROR:zygote_host_impl_linux.cc(90)] Running as root without --no-sandbox is not supported. See https://crbug.com/638180.
02 05 2018 19:40:17.152:ERROR [launcher]: Chrome failed 2 times (cannot start). Giving up.
02 05 2018 19:40:23.683:WARN [karma]: No captured browser, open http://localhost:9876/

@marksyzm
Copy link

marksyzm commented May 8, 2018

^^ I had something like that in Jenkins. Turned out I had to set the --browsers flag in the command as it was still defaulting to plain headless Chrome without the --no-sandbox flag. Here's one example of what I used: yarn test --single-run --no-progress --browsers Chrome_without_security

Where Chrome_without_security was set up as this:

module.exports = function (config) {
  process.env.CHROME_BIN = require('puppeteer').executablePath()

  config.set({

    // You may use 'ChromeCanary', 'Chromium' or any other supported browser
    browsers: ['Chrome_without_security'], 
    // you can define custom flags
    customLaunchers: {
      Chrome_without_security: {
        base: 'ChromeHeadless',
        flags: ['--no-sandbox']
      }
    }
  });
};

Puppeteer is there to handle any missing Chrome package, of course.

@yuri-wisestamp
Copy link

If you're running Puppeteer directly from Node code, you can provide this flag as follows:
const browser = await puppeteer.launch({headless: true, args:['--no-sandbox']});

@victorguerra95
Copy link

Thanks! @yuri-wisestamp

@westandy-dcp
Copy link

Workaround:

    browsers: ['ChromeHeadlessNoSandbox'],
    customLaunchers: {
      ChromeHeadlessNoSandbox: {
        base: 'ChromeHeadless',
        flags: ['--no-sandbox']
      }
    },

Don't forget to add --headless ChromeHeadlessNoSandbox to your test command. For me, that meant changing

ng test ${library} --browsers ChromeHeadless --watch=${watch} to

ng test ${library} --browsers ChromeHeadlessNoSandbox --watch=${watch}

It's probably obvious to you, but it took me a minute to figure that out. I hope that helps.

@Zicrou
Copy link

Zicrou commented Aug 6, 2019

Hello I just tried the following line code and it works perfectly 💯

run this line in the terminal:
google-chrome --no-sandbox --user-data-dir

or you go here : https://stackoverflow.com/questions/12258086/how-do-i-run-google-chrome-as-root

@rabindranathforcast
Copy link

rabindranathforcast commented Aug 21, 2019

@Zicrou or any can give some advice about How it works for you?

i will show mine config for compare

karma.config

// Karma configuration file, see link for more information
// https://karma-runner.github.io/1.0/config/configuration-file.html
const process = require('process');
process.env.CHROME_BIN = require('puppeteer').executablePath();

module.exports = function(config) {
    config.set({
        basePath: '',
        frameworks: ['jasmine', '@angular-devkit/build-angular'],
        plugins: [
            require('karma-jasmine'),
            require('karma-chrome-launcher'),
            require('karma-jasmine-html-reporter'),
            require('karma-coverage-istanbul-reporter'),
            require('@angular-devkit/build-angular/plugins/karma')
        ],
        client: {
            clearContext: false // leave Jasmine Spec Runner output visible in browser
        },
        coverageIstanbulReporter: {
            dir: require('path').join(__dirname, './coverage/angCICD'),
            reports: ['html', 'lcovonly', 'text-summary'],
            fixWebpackSourcePaths: true
        },
        reporters: ['progress', 'kjhtml'],
        port: 9876,
        colors: true,
        logLevel: config.LOG_INFO,
        autoWatch: true,
        browsers: ['ChromeHeadlessNoSandbox'],
        customLaunchers: {
            ChromeHeadlessNoSandbox: {
                base: 'ChromeHeadless',
                flags: ['--no-sandbox']
            }

        },
        singleRun: false,
        restartOnFileChange: true,
        concurrency: Infinity
    });
};

protractor.config

// @ts-check
// Protractor configuration file, see link for more information
// https://github.com/angular/protractor/blob/master/lib/config.ts
const puppeteer = require('puppeteer');
const { SpecReporter } = require('jasmine-spec-reporter');

/**
 * @type { import("protractor").Config }
 */
exports.config = {
    allScriptsTimeout: 11000,
    specs: [
        './src/**/*.e2e-spec.ts'
    ],
    capabilities: {
        browserName: 'chrome',
        chromeOptions: {
            args: ['--headless', '--no-sandbox'],
            binary: puppeteer.executablePath()
        }
    },
    directConnect: true,
    baseUrl: 'http://localhost:4200/',
    framework: 'jasmine',
    jasmineNodeOpts: {
        showColors: true,
        defaultTimeoutInterval: 30000,
        print: function() {}
    },
    onPrepare() {
        require('ts-node').register({
            project: require('path').join(__dirname, './tsconfig.json')
        });
        jasmine.getEnv().addReporter(new SpecReporter({ spec: { displayStacktrace: true } }));
    }
};

gitlab-ci.yml

variables:
  IMG_BUILD: node:latest
  IMG_TEST: trion/ng-cli-karma
  IMG_TESTING: alekzonder/puppeteer:latest
  IMG_TESTING_FINAL: node:8.9.1
  IMG_GITLAB_CI: juristr/angular-ci-build:1.0.0

deploy_test:
  stage: test
  image: ${IMG_BUILD}
  environment: Production
  artifacts:
    paths:
      - node_modules/
  only:
    - master
  script:
    - rm ./package-lock.json
    - apt-get update
    - apt-get install gconf-service libasound2 libatk1.0–0 libc6 libcairo2 libcups2 libdbus-1–3 libexpat1 libfontconfig1 libgcc1 libgconf-2–4 libgdk-pixbuf2.0–0 libglib2.0–0 libgtk-3–0 libnspr4 libpango-1.0–0 libpangocairo-1.0–0 libstdc++6 libx11–6 libx11-xcb1 libxcb1 libxcomposite1 libxcursor1 libxdamage1 libxext6 libxfixes3 libxi6 libxrandr2 libxrender1 libxss1 libxtst6 ca-certificates fonts-liberation libappindicator1 libnss3 lsb-release xdg-utils wget -y
    - npm install
    - npm run test-ci

definition for npm run test-ci is:

"test-ci": "ng test --no-watch --no-progress --browsers=ChromeHeadlessNoSandbox",

and that is the error give me gitlab jobs in console

21 08 2019 17:30:16.241:WARN [karma]: No captured browser, open http://localhost:9876/
21 08 2019 17:30:16.357:INFO [karma-server]: Karma v4.1.0 server started at http://0.0.0.0:9876/
21 08 2019 17:30:16.358:INFO [launcher]: Launching browsers ChromeHeadlessNoSandbox with concurrency unlimited
21 08 2019 17:30:16.361:INFO [launcher]: Starting browser ChromeHeadless
21 08 2019 17:30:17.276:ERROR [launcher]: Cannot start ChromeHeadless
	/app/node_modules/puppeteer/.local-chromium/linux-674921/chrome-linux/chrome: error while loading shared libraries: libX11-xcb.so.1: cannot open shared object file: No such file or directory

21 08 2019 17:30:17.276:ERROR [launcher]: ChromeHeadless stdout: 
21 08 2019 17:30:17.277:ERROR [launcher]: ChromeHeadless stderr: /app/node_modules/puppeteer/.local-chromium/linux-674921/chrome-linux/chrome: error while loading shared libraries: libX11-xcb.so.1: cannot open shared object file: No such file or directory

21 08 2019 17:30:17.307:INFO [launcher]: Trying to start ChromeHeadless again (1/2).
21 08 2019 17:30:17.358:ERROR [launcher]: Cannot start ChromeHeadless
	/app/node_modules/puppeteer/.local-chromium/linux-674921/chrome-linux/chrome: error while loading shared libraries: libX11-xcb.so.1: cannot open shared object file: No such file or directory

21 08 2019 17:30:17.359:ERROR [launcher]: ChromeHeadless stdout: 
21 08 2019 17:30:17.359:ERROR [launcher]: ChromeHeadless stderr: /app/node_modules/puppeteer/.local-chromium/linux-674921/chrome-linux/chrome: error while loading shared libraries: libX11-xcb.so.1: cannot open shared object file: No such file or directory

21 08 2019 17:30:17.382:INFO [launcher]: Trying to start ChromeHeadless again (2/2).
21 08 2019 17:30:17.436:ERROR [launcher]: Cannot start ChromeHeadless
	/app/node_modules/puppeteer/.local-chromium/linux-674921/chrome-linux/chrome: error while loading shared libraries: libX11-xcb.so.1: cannot open shared object file: No such file or directory

21 08 2019 17:30:17.437:ERROR [launcher]: ChromeHeadless stdout: 
21 08 2019 17:30:17.437:ERROR [launcher]: ChromeHeadless stderr: /app/node_modules/puppeteer/.local-chromium/linux-674921/chrome-linux/chrome: error while loading shared libraries: libX11-xcb.so.1: cannot open shared object file: No such file or directory

21 08 2019 17:30:17.469:ERROR [launcher]: ChromeHeadless failed 2 times (cannot start). Giving up.
21 08 2019 17:30:22.314:WARN [karma]: No captured browser, open http://localhost:9876/

I have this version of puppeteer --> "puppeteer": "^1.19.0",

i am missing something?

thx

^^ I had something like that in Jenkins. Turned out I had to set the --browsers flag in the command as it was still defaulting to plain headless Chrome without the --no-sandbox flag. Here's one example of what I used: yarn test --single-run --no-progress --browsers Chrome_without_security

Where Chrome_without_security was set up as this:

module.exports = function (config) {
  process.env.CHROME_BIN = require('puppeteer').executablePath()

  config.set({

    // You may use 'ChromeCanary', 'Chromium' or any other supported browser
    browsers: ['Chrome_without_security'], 
    // you can define custom flags
    customLaunchers: {
      Chrome_without_security: {
        base: 'ChromeHeadless',
        flags: ['--no-sandbox']
      }
    }
  });
};

Puppeteer is there to handle any missing Chrome package, of course.

@rabindranathforcast
Copy link

Workaround:

    browsers: ['ChromeHeadlessNoSandbox'],
    customLaunchers: {
      ChromeHeadlessNoSandbox: {
        base: 'ChromeHeadless',
        flags: ['--no-sandbox']
      }
    },

Don't forget to add --headless ChromeHeadlessNoSandbox to your test command. For me, that meant changing

ng test ${library} --browsers ChromeHeadless --watch=${watch} to

ng test ${library} --browsers ChromeHeadlessNoSandbox --watch=${watch}

It's probably obvious to you, but it took me a minute to figure that out. I hope that helps.

about this solution i have this setup

karma.config

const process = require('process');
process.env.CHROME_BIN = require('puppeteer').executablePath();

.
.
.
.

 browsers: ['ChromeHeadlessNoSandbox'],
        customLaunchers: {
            ChromeHeadlessNoSandbox: {
                base: 'ChromeHeadless',
                flags: ['--no-sandbox', '--headless']
            }

        },

in package.json apply npm run test-ci

"test-ci": "ng test --no-watch --no-progress --browsers=ChromeHeadlessNoSandbox",

gitlab-ci results

21 08 2019 20:01:29.751:WARN [karma]: No captured browser, open http://localhost:9876/
21 08 2019 20:01:29.822:INFO [karma-server]: Karma v4.1.0 server started at http://0.0.0.0:9876/
21 08 2019 20:01:29.822:INFO [launcher]: Launching browsers ChromeHeadlessNoSandbox with concurrency unlimited
21 08 2019 20:01:29.849:INFO [launcher]: Starting browser ChromeHeadless
21 08 2019 20:01:30.732:ERROR [launcher]: Cannot start ChromeHeadless
	/app/node_modules/puppeteer/.local-chromium/linux-674921/chrome-linux/chrome: error while loading shared libraries: libX11-xcb.so.1: cannot open shared object file: No such file or directory

AllanZhengYP added a commit to AllanZhengYP/aws-sdk-js-v3 that referenced this issue Jan 15, 2020
AllanZhengYP added a commit to AllanZhengYP/aws-sdk-js-v3 that referenced this issue Jan 15, 2020
AllanZhengYP added a commit to AllanZhengYP/aws-sdk-js-v3 that referenced this issue Jan 15, 2020
AllanZhengYP added a commit to AllanZhengYP/aws-sdk-js-v3 that referenced this issue Jan 15, 2020
AllanZhengYP added a commit to AllanZhengYP/aws-sdk-js-v3 that referenced this issue Jan 15, 2020
AllanZhengYP added a commit to AllanZhengYP/aws-sdk-js-v3 that referenced this issue Jan 15, 2020
@xinnxz
Copy link

xinnxz commented Apr 26, 2020

i solved after see this videos https://youtu.be/tc379GTTgys

100% problem has been fixed :) ..

@lpender
Copy link

lpender commented May 1, 2020

That results in a SIGSEGV for me, although the tests appear to pass.

01 05 2020 15:15:01.930:INFO [karma-server]: Karma v5.0.2 server started at http://0.0.0.0:9876/
01 05 2020 15:15:01.932:INFO [launcher]: Launching browsers ChromeNoSandbox with concurrency unlimited
01 05 2020 15:15:01.951:INFO [launcher]: Starting browser ChromeHeadless
01 05 2020 15:15:02.241:INFO [Chrome Headless 81.0.4044.129 (Mac OS 10.14.6)]: Connected on socket PylVnfHy8jmDMhm2AAAA with id 97738507
Chrome Headless 81.0.4044.129 (Mac OS 10.14.6): Executed 13 of 14 (skipped 1) SUCCESS (0.01 secs / 0.042 secs)
TOTAL: 13 SUCCESS
error Command failed with signal "SIGSEGV".
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
Command failed: /Users/lpender/.yvm/versions/v1.22.4/bin/yarn.js test --browsers ChromeNoSandbox

@edgar-slalom
Copy link

For me, my karma.config.js was missing this:

 customLaunchers: {
      chromeHeadlessNoSandbox: {
        base: 'ChromeHeadless',
        flags: ['--no-sandbox']
      }
    },

@ankitkpathak04
Copy link

ankitkpathak04 commented Aug 18, 2021

My karma.conf.js looks like
// Karma configuration file, see link for more information
// https://karma-runner.github.io/1.0/config/configuration-file.html

module.exports = function (config) {
config.set({
basePath: '',
frameworks: ['jasmine', '@angular-devkit/build-angular'],
plugins: [
require('karma-jasmine'),
require('karma-chrome-launcher'),
require('karma-jasmine-html-reporter'),
require('karma-coverage-istanbul-reporter'),
require('@angular-devkit/build-angular/plugins/karma')
],
client:{
clearContext: false // leave Jasmine Spec Runner output visible in browser
},
coverageIstanbulReporter: {
dir: require('path').join(__dirname, 'coverage'), reports: [ 'html', 'lcovonly' ],
fixWebpackSourcePaths: true
},

reporters: ['progress', 'kjhtml'],
port: 9876,
colors: true,
logLevel: config.LOG_INFO,
autoWatch: true,
browsers: ['ChromeHeadlessNoSandbox'],
  customLaunchers:{
    ChromeHeadlessNoSandbox:{
      base: 'ChromeHeadless',
      flags: ['--no-sandbox']
    }
},
singleRun: false

});
};

but after running npm test
getting error as

Disconnected (0 times) , because no message in 30000 ms.
Chrome Headless 92.0.4515.131 (Windows 10) ERROR
Disconnected , because no message in 30000 ms.

Chrome Headless 92.0.4515.131 (Windows 10) ERROR
Disconnected , because no message in 30000 ms.

npm ERR! Test failed. See above for more details.

@deepansh39
Copy link

Workaround:

    browsers: ['ChromeHeadlessNoSandbox'],
    customLaunchers: {
      ChromeHeadlessNoSandbox: {
        base: 'ChromeHeadless',
        flags: ['--no-sandbox']
      }
    },

bro where i have to save this code i mean can you share location

@deepansh39
Copy link

i solved after see this videos https://youtu.be/tc379GTTgys

100% problem has been fixed :) ..

THANKS BRO FOR THIS VIDEO MY PROBLEM SOLVED;
<<<print("thanks for this")

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests