A smart, automated script that detects your geographical location and configures the most appropriate Debian/Ubuntu package mirrors for optimal download speeds. Perfect for Docker builds, CI/CD pipelines, and system administration.
- π Automatic Geographical Detection - Uses IP geolocation to determine your location
- π Optimized Mirrors - Pre-configured fast mirrors for China, Japan, Korea, and other regions
- π Fallback System - Graceful fallback to reliable default mirrors if detection fails
- π‘οΈ Safe Execution - Creates automatic backups before making changes
- π Comprehensive Logging - Colored output with detailed progress information
- π§ Multi-Distribution Support - Works with both Debian and Ubuntu systems
- β‘ Speed Testing - Optional mirror speed testing for verification
- Docker Builds - Optimize package downloads in container builds
- CI/CD Pipelines - Faster builds in automated environments
- System Administration - Quick mirror optimization for servers
- Development Environments - Faster package installation for developers
- International Teams - Works optimally from any location worldwide
- Operating System: Debian, Ubuntu, or compatible distributions
- Dependencies:
curl(for IP geolocation) - Permissions: Root access (for writing to
/etc/apt/sources.list) - Network: Internet connectivity for geolocation and mirror testing
For Docker, CI/CD, or server environments, you can download and run the script in one command:
# Basic one-liner (download and execute)
curl -fsSL https://raw.githubusercontent.com/AlienGen/debian-ubuntu-mirror-geoselect/main/auto-select-mirror.sh | sudo bash# Simple one-liner in Dockerfile
RUN curl -fsSL https://raw.githubusercontent.com/AlienGen/debian-ubuntu-mirror-geoselect/main/auto-select-mirror.sh | bash# GitHub Actions
- name: Optimize package mirrors
run: curl -fsSL https://raw.githubusercontent.com/AlienGen/debian-ubuntu-mirror-geoselect/main/auto-select-mirror.sh | sudo bash
# GitLab CI
mirror_optimization:
script:
- curl -fsSL https://raw.githubusercontent.com/AlienGen/debian-ubuntu-mirror-geoselect/main/auto-select-mirror.sh | bash
# Jenkins Pipeline
stage('Optimize Mirrors') {
steps {
sh 'curl -fsSL https://raw.githubusercontent.com/AlienGen/debian-ubuntu-mirror-geoselect/main/auto-select-mirror.sh | sudo bash'
}
}# Download directly
curl -O https://raw.githubusercontent.com/AlienGen/debian-ubuntu-mirror-geoselect/main/auto-select-mirror.sh
# Or clone the repository
git clone https://github.com/AlienGen/debian-ubuntu-mirror-geoselect.git
cd debian-ubuntu-mirror-geoselectchmod +x auto-select-mirror.sh# Run with sudo (recommended)
sudo ./auto-select-mirror.sh
# Or run as root
sudo su -
./auto-select-mirror.shsudo ./auto-select-mirror.shOutput:
[INFO] Starting mirror auto-selection...
[INFO] Detected: debian 12 (bookworm)
[WARNING] No existing /etc/apt/sources.list found - this is normal in some Docker images
[INFO] No .list files found in sources.list.d
[INFO] Thoroughly cleaning APT sources...
[INFO] Searching for all APT sources locations...
[INFO] Found: /etc/apt/sources.list.d/
[INFO] - /etc/apt/sources.list.d//debian.sources
[INFO] Found: /var/lib/apt/lists/
[INFO] Found: /etc/apt/apt.conf.d/
[INFO] - /etc/apt/apt.conf.d//01autoremove
[INFO] - /etc/apt/apt.conf.d//70debconf
[INFO] - /etc/apt/apt.conf.d//docker-autoremove-suggests
[INFO] - /etc/apt/apt.conf.d//docker-clean
[INFO] - /etc/apt/apt.conf.d//docker-gzip-indexes
[INFO] - /etc/apt/apt.conf.d//docker-no-languages
[INFO] Searching for files containing mirror references...
[INFO] Found mirror reference in: /etc/apt/sources.list.d/debian.sources
[INFO] Removing all sources.list.d files...
[INFO] Checking APT configuration files...
[SUCCESS] APT sources cleaned
[INFO] Detecting geographical location...
[INFO] Trying service: https://ipapi.co/country_code
[SUCCESS] Location detected: CN
[INFO] Selecting mirrors for CN...
[INFO] Using Chinese mirrors (Tsinghua University)
[INFO] Writing new sources.list...
[SUCCESS] Sources.list written successfully
[INFO] Contents preview:
deb https://mirrors.tuna.tsinghua.edu.cn/debian/ bookworm main contrib non-free non-free-firmware
deb https://mirrors.tuna.tsinghua.edu.cn/debian/ bookworm-updates main contrib non-free non-free-firmware
deb https://mirrors.tuna.tsinghua.edu.cn/debian/ bookworm-backports main contrib non-free non-free-firmware
[INFO] Updating package lists...
Get:1 https://mirrors.tuna.tsinghua.edu.cn/debian bookworm InRelease [151 kB]
Get:2 https://mirrors.tuna.tsinghua.edu.cn/debian bookworm-updates InRelease [55.4 kB]
Get:3 https://mirrors.tuna.tsinghua.edu.cn/debian bookworm-backports InRelease [59.4 kB]
Get:4 https://mirrors.tuna.tsinghua.edu.cn/debian-security bookworm-security InRelease [48.0 kB]
Get:5 https://mirrors.tuna.tsinghua.edu.cn/debian bookworm/non-free amd64 Packages [102 kB]
Get:6 https://mirrors.tuna.tsinghua.edu.cn/debian bookworm/non-free-firmware amd64 Packages [6372 B]
Get:7 https://mirrors.tuna.tsinghua.edu.cn/debian bookworm/main amd64 Packages [8793 kB]
Get:8 https://mirrors.tuna.tsinghua.edu.cn/debian bookworm/contrib amd64 Packages [53.5 kB]
Get:9 https://mirrors.tuna.tsinghua.edu.cn/debian bookworm-updates/main amd64 Packages [756 B]
Get:10 https://mirrors.tuna.tsinghua.edu.cn/debian bookworm-backports/main amd64 Packages [291 kB]
Get:11 https://mirrors.tuna.tsinghua.edu.cn/debian bookworm-backports/non-free-firmware amd64 Packages [3828 B]
Get:12 https://mirrors.tuna.tsinghua.edu.cn/debian bookworm-backports/contrib amd64 Packages [5852 B]
Get:13 https://mirrors.tuna.tsinghua.edu.cn/debian bookworm-backports/non-free amd64 Packages [13.3 kB]
Get:14 https://mirrors.tuna.tsinghua.edu.cn/debian-security bookworm-security/main amd64 Packages [268 kB]
Get:15 https://mirrors.tuna.tsinghua.edu.cn/debian-security bookworm-security/non-free-firmware amd64 Packages [688 B]
Get:16 https://mirrors.tuna.tsinghua.edu.cn/debian-security bookworm-security/contrib amd64 Packages [896 B]
Fetched 9852 kB in 3s (3479 kB/s)
Reading package lists...
[SUCCESS] Package lists updated successfully
[SUCCESS] Mirror configuration completed successfully!
[INFO] Testing mirror speed...
[SUCCESS] Mirror test completed in 1s
[INFO] You can now use 'apt-get update' and 'apt-get install' with optimized mirrors
# In your Dockerfile
COPY auto-select-mirror.sh /auto-select-mirror.sh
RUN chmod +x /auto-select-mirror.sh && \
/auto-select-mirror.sh# GitHub Actions example
- name: Optimize package mirrors
run: |
curl -O https://raw.githubusercontent.com/AlienGen/debian-ubuntu-mirror-geoselect/main/auto-select-mirror.sh
chmod +x auto-select-mirror.sh
sudo ./auto-select-mirror.shThe script automatically selects the best mirrors based on your detected location:
| Region | Countries | Mirror Provider | Speed |
|---|---|---|---|
| China | CN, HK, TW, MO | Tsinghua University | β‘β‘β‘β‘β‘ |
| Japan/Korea | JP, KR | Japanese Debian | β‘β‘β‘β‘ |
| Southeast Asia | SG, MY, TH, VN, ID, PH | Singapore Debian | β‘β‘β‘β‘ |
| Australia/NZ | AU, NZ | Australian Debian | β‘β‘β‘ |
| UK/Ireland | GB, IE | UK Debian | β‘β‘β‘ |
| Europe | DE, AT, CH, NL, BE, FR, IT, ES, PT | Official Debian | β‘β‘β‘ |
| Rest of World | All others | US Debian | β‘β‘ |
You can customize the script behavior with environment variables:
# Force a specific country (bypasses geolocation)
export FORCE_COUNTRY=CN
sudo ./auto-select-mirror.sh
# Disable speed testing
export DISABLE_SPEED_TEST=1
sudo ./auto-select-mirror.shTo add custom mirrors for your region, edit the get_mirrors() function in the script:
# Add your custom region
case "$country" in
YOUR_COUNTRY_CODE)
log_info "Using your custom mirrors"
# Add your mirror configuration here
;;
# ... existing cases
esac[ERROR] This script must be run as root (use sudo)Solution: Run with sudo ./auto-select-mirror.sh
[WARNING] Geolocation detection failed, using default (US)Solution: Check your internet connection or use FORCE_COUNTRY environment variable
[ERROR] Failed to update package listsSolution: The script automatically restores your backup. Check your network connection.
[WARNING] curl not available, using default mirrorsSolution: Install curl: apt-get install curl
Enable verbose output for troubleshooting:
export DEBUG=1
# Run with debug output
bash ./auto-select-mirror.shIf you need to restore your original sources.list:
# List available backups
ls -la /etc/apt/sources.list.backup.*
# Restore a specific backup
sudo cp /etc/apt/sources.list.backup.20250621_143022 /etc/apt/sources.listTypical speed improvements by region:
| Region | Default Speed | Optimized Speed | Improvement |
|---|---|---|---|
| China | 50 KB/s | 5 MB/s | 100x faster |
| Japan | 500 KB/s | 10 MB/s | 20x faster |
| Europe | 2 MB/s | 8 MB/s | 4x faster |
| US | 5 MB/s | 8 MB/s | 1.6x faster |
- Backup Creation: Automatic backup before any changes
- Error Handling: Graceful fallback on failures
- Input Validation: Sanitized inputs and outputs
- Minimal Dependencies: Only requires
curlfor geolocation - Open Source: Full transparency of all operations
We welcome contributions! Please feel free to:
- Fork the repository
- Create a feature branch
- Add your improvements
- Test thoroughly
- Submit a pull request
# Clone the repository
git clone https://github.com/AlienGen/debian-ubuntu-mirror-geoselect.git
cd debian-ubuntu-mirror-geoselect
# Make changes and test
./auto-select-mirror.shThis project is licensed under the MIT License - see the LICENSE file for details.
- Tsinghua University for providing excellent mirrors in China
- Debian Project for maintaining the official mirrors
- Ubuntu Project for their mirror infrastructure
- Open Source Community for feedback and contributions
- Issues: GitHub Issues
- Website AlienGen
- Email: opensource@aliengen.com
- v1.0.0 (2025-06-21)
- Initial release
- Support for Debian and Ubuntu
- Automatic geolocation detection
- Regional mirror optimization
- Comprehensive error handling
Made with β€οΈ by the AlienGen Team
Optimizing infrastructure worldwide, one server at a time.