All Products
Search
Document Center

Elastic Compute Service:Install GRUB on a Linux server

Last Updated:May 07, 2025

GRand Unified Bootloader (GRUB) is the default bootloader for Linux operating systems. When system boot files are damaged or the boot menu needs to be repaired, you must install or repair GRUB to restore the system's boot functionality.

Background information

When you use Server Migration Center (SMC) to migrate a Linux server to Alibaba Cloud, you must install or upgrade to GRUB v2.02 or later on the server in the following situations:

  • GRUB is not installed on the server.

  • The server runs an early Linux distribution, such as CentOS 5 or Debian 7.

  • The GRUB version is earlier than v2.02.

  • The "Do Grub Failed" error message is displayed in the log file.

This topic describes how to install GRUB on a Linux server. In this example, GRUB v2.02 is used. The procedure for installing different GRUB versions is the same. However, the source code package may vary based on the GRUB version. For more information, visit Index of /gnu/grub.

Procedure

Step 1: Check the GRUB version

  1. Log on to the Linux server as the root user.

  2. Run the following commands to check the file paths of the current GRUB version and related tools:

    which grub
    which grub-install
    which grub-mkconfig
    • If the outputs of the preceding commands indicate that one or more of the file paths do not exist, GRUB is not installed on the server or the corresponding files are missing. In this case, perform the following steps to install GRUB.

    • If you find the paths of all the files, run the following commands to back up the files by renaming the files. Then, install a new GRUB version to overwrite the current version.

      mv /sbin/grub /sbin/grub-old
      mv /sbin/grub-install /sbin/grub-install-old
      mv /sbin/grub-mkconfig /sbin/grub-mkconfig-old
      Note

      After you use SMC to migrate the server, you can restore the files by renaming the files to their original names.

Step 2: Install dependencies

Run one of the following commands to install GRUB dependencies, including bison, gcc, and make, based on the operating system.

CentOS or Red Hat Enterprise Linux

yum install -y bison gcc make flex

Debian or Ubuntu

apt-get update && apt-get install -y bison gcc make flex

Step 3: Install flex

  1. Create the tools folder if the folder does not exist.

    test -d /home/testuser/tools || mkdir -p /home/testuser/tools
  2. Go to the tools folder and download the flex installation package.

    cd /home/testuser/tools
    wget https://github.com/westes/flex/releases/download/v2.6.4/flex-2.6.4.tar.gz
    tar xzf flex-2.6.4.tar.gz
  3. Compile and install flex and create a symbolic link.

    cd flex-2.6.4
    mkdir -p build
    cd build
    ../configure
    make && make install
    ln -s /usr/local/bin/flex /usr/bin/flex

Step 4: Install GRUB

  1. Download and decompress the GRUB v2.02 installation package.

    cd /home/testuser/tools
    wget https://alpha.gnu.org/gnu/grub/grub-2.02~rc1.tar.gz
    tar xzf grub-2.02~rc1.tar.gz
  2. Compile and install GRUB and create a symbolic link.

    cd grub-2.02~rc1
    mkdir -p build
    cd build
    ../configure
    # If the -Werror error is reported, modify the Makefile file and remove the -Werror option from the file. 
    sed -i -e "s/-Werror//" ./grub-core/Makefile
    sed -i -e "s/-Werror//" ./Makefile
    make && make install
    ln -s /usr/local/sbin/grub-install /sbin/grub-install
    ln -s /usr/local/sbin/grub-mkconfig /sbin/grub-mkconfig

Step 5: Verify the installation

Run the following command to check whether GRUB v2.02 is installed or GRUB is upgraded to v2.02:

grub-install --version

What to do next

  • If GRUB v2.02 is installed or GRUB is upgraded to v2.02, use SMC to migrate the server to Alibaba Cloud. For more information, see Migration guide.

  • (Optional) If you want to use the previous version of GRUB on the server after the server is migrated, run the following commands to restore GRUB to the previous version:

    rm /sbin/grub-install
    rm /sbin/grub-mkconfig
    rm /boot/grub/grub.cfg
    mv /sbin/grub-old /sbin/grub
    mv /sbin/grub-install-old /sbin/grub-install