public function download($targetDir, $version)
    {
        $io = $this->getIO();
        $downloadManager = $this->getComposer()->getDownloadManager();
        $retries = count($this->getPhantomJsVersions());

        while ($retries--) {
            $package = $this->createComposerInMemoryPackage($targetDir, $version);

            try {
                $downloadManager->download($package, $targetDir, false);
                return true;
            } catch (TransportException $e) {
                if ($e->getStatusCode() === 404) {
                    $version = $this->getLowerVersion($version);
                    $io->warning('Retrying the download with a lower version number: "' . $version . '"');
                } else {
                    $message = $e->getMessage();
                    $code = $e->getStatusCode();
                    $io->error(PHP_EOL . '<error>TransportException: "' . $message . '". HTTP status code: ' . $code . '</error>');
                    return false;
                }
            } catch (\Exception $e) {
                $message = $e->getMessage();
                $io->error(PHP_EOL . '<error>While downloading version ' . $version . ' the following error accoured: ' . $message . '</error>');
                return false;
            }
        }
    }