Introduction

These instructions will hopefully assist you to start with a stock Motorola Moto G 4G, unlock the bootloader (if necessary), and then download the required tools as well as the very latest source code for LineageOS (based on Google’s Android operating system) for your device. Using these, you can build both LineageOS and LineageOS Recovery image from source code, and then install them both to your device.

It is difficult to say how much experience is necessary to follow these instructions. While this guide is certainly not for the very very very uninitiated, these steps shouldn’t require a PhD in software development either. Some readers will have no difficulty and breeze through the steps easily. Others may struggle over the most basic operation. Because people’s experiences, backgrounds, and intuitions differ, it may be a good idea to read through just to ascertain whether you feel comfortable or are getting over your head.

Remember, you assume all risk of trying this, but you will reap the rewards! It’s pretty satisfying to boot into a fresh operating system you baked at home :). And once you’re an Android-building ninja, there will be no more need to wait for “nightly” builds from anyone. You will have at your fingertips the skills to build a full operating system from code to a running device, whenever you want. Where you go from there– maybe you’ll add a feature, fix a bug, add a translation, or use what you’ve learned to build a new app or port to a new device– or maybe you’ll never build again– it’s all really up to you.

What you’ll need

Let’s begin!

Build LineageOS and LineageOS Recovery

Install the SDK

If you haven’t previously installed adb and fastboot, you can download them from Google. Extract it running:

unzip platform-tools-latest-linux.zip -d ~

Now you have to add adb and fastboot to your PATH. Open ~/.profile and add the following:

# add Android SDK platform tools to path
if [ -d "$HOME/platform-tools" ] ; then
    PATH="$HOME/platform-tools:$PATH"
fi

Then, run source ~/.profile to update your environment.

Install the build packages

Several packages are needed to build LineageOS. You can install these using your distribution’s package manager.

To build LineageOS, you’ll need:

For Ubuntu versions older than 16.04 (xenial), substitute:

Java

Different versions of LineageOS require different JDK (Java Development Kit) versions.

* Ubuntu 16.04 and newer do not have OpenJDK 1.7 in the standard package repositories. See Ask Ubuntu question How do I install openjdk 7 on Ubuntu 16.04 or higher? Note that the suggestion to use PPA openjdk-r is outdated (the PPA has never updated their offering of openjdk-7-jdk, so it lacks security fixes); skip that answer even if it is the most upvoted.

Create the directories

You’ll need to set up some directories in your build environment.

To create them:

mkdir -p ~/bin
mkdir -p ~/android/lineage

Install the repo command

Enter the following to download the repo binary and make it executable (runnable):

curl https://storage.googleapis.com/git-repo-downloads/repo > ~/bin/repo
chmod a+x ~/bin/repo

Put the ~/bin directory in your path of execution

In recent versions of Ubuntu, ~/bin should already be in your PATH. You can check this by opening ~/.profile with a text editor and verifying the following code exists (add it if it is missing):

# set PATH so it includes user's private bin if it exists
if [ -d "$HOME/bin" ] ; then
    PATH="$HOME/bin:$PATH"
fi

Then, run source ~/.profile to update your environment.

Initialize the LineageOS source repository

The following branches are officially supported for the Motorola Moto G 4G:

Enter the following to initialize the repository:

cd ~/android/lineage
repo init -u https://github.com/LineageOS/android.git -b cm-14.1

Download the source code

To start the download of the source code to your computer:

repo sync

The LineageOS manifests include a sensible default configuration for repo, which we strongly suggest you use (i.e. don’t add any options to sync). For reference, our default values are -j 4 and -c. The -j 4 part means that there will be four simultaneous threads/connections. If you experience problems syncing, you can lower this to -j 3 or -j 2. On the other hand, -c will ask repo to pull in only the current branch, instead of the entire LineageOS history.

Prepare the device-specific code

After the source downloads, ensure you’re in the root of the source code (cd ~/android/lineage), then type:

source build/envsetup.sh
breakfast peregrine

This will download your device’s device specific configuration and kernel.

Extract proprietary blobs

Now ensure your Moto G 4G is connected to your computer via the USB cable, with ADB and root enabled, and that you are in the ~/android/lineage/device/motorola/peregrine folder. Then run the extract-files.sh script:

./extract-files.sh

The blobs should be pulled into the ~/android/lineage/vendor/motorola folder. If you see “command not found” errors, adb may need to be placed in ~/bin.

Turn on caching to speed up build

Make use of ccache if you want to speed up subsequent builds by running:

export USE_CCACHE=1

and adding that line to your ~/.bashrc file. Then, specify the maximum amount of disk space you want ccache to use by typing this from the top of your Android tree:

prebuilts/misc/linux-x86/ccache/ccache -M 50G

where 50G corresponds to 50GB of cache. This needs to be run once. Anywhere from 25GB-100GB will result in very noticeably increased build speeds (for instance, a typical 1hr build time can be reduced to 20min). If you’re only building for one device, 25GB-50GB is fine. If you plan to build for several devices that do not share the same kernel source, aim for 75GB-100GB. This space will be permanently occupied on your drive, so take this into consideration. See more information about ccache on Google’s Android build environment initialization page.

You can also enable the optional ccache compression. While this may involve a tiny performance slowdown, it increases the number of files that fit in the cache. To enable it, run:

export CCACHE_COMPRESS=1

or add that line to your ~/.bashrc file.

Configure jack

Jack is the new Java compiler used when building LineageOS 14.1. It is known to run out of memory - a simple fix is to run this command:

export ANDROID_JACK_VM_ARGS="-Dfile.encoding=UTF-8 -XX:+TieredCompilation -Xmx4G"

Adding that command to your ~/.bashrc file will automatically configure Jack to allocate a sufficient amount of memory.

Start the build

Time to start building! Now, type:

croot
brunch peregrine

The build should begin.

Install the build

Assuming the build completed without errors (it will be obvious when it finishes), type the following in the terminal window the build ran in:

cd $OUT

There you’ll find all the files that were created. The two files of more interest are:

  1. recovery.img, which is the LineageOS recovery image.
  2. lineage-14.1-20170519-UNOFFICIAL-peregrine.zip, which is the LineageOS installer package.

Success! So… what’s next?

You’ve done it! Welcome to the elite club of self-builders. You’ve built your operating system from scratch, from the ground up. You are the master/mistress of your domain… and hopefully you’ve learned a bit on the way and had some fun too.

To get assistance