Chrome OS native development

Today is the third day of my Two weeks programming on a Chromebook challenge. For the first two days I’ve played with enabling the Developer Mode on my Chromebook and installing Crouton and GCC 7 for C++17 development. Running Chrome OS and Crouton side by side is the easiest path for a complete Unix like development experience.

I think it could be interesting and more challenging for me personally, to investigate if I could do native Chrome OS development. By native I mean using only applications that run directly on Chrome OS or can be compiled to run on Chrome OS. Using a separate OS is not an option!

I assume that you start with a fresh Chromebook and Developer Mode enabled. If you need more information about enabling the Developer Mode see the first article from this series.

Chrome OS is Linux under the hood so, in principle, you can run any standalone Linux binary on it. More precisely, Chrome OS is based on Gentoo Linux. By default, after you enable the Developer Mode, you have access to a limited set of command line utilities, honorable mentions here for the Bash shell and the Vim editor.

You can also install a limited set of development tools by running dev_install in a shell tab. Read the relevant documentation if you want to know more. Running dev-install for example, will allow you to use Python 2.7 on Chrome OS. Unfortunately, at the time of this writing, dev_install does not provide a compiler toolchain. My suggestion is to not use it, at least for now.

A good approach, for native Chrome OS development, is to install the compiler toolchain from Chromebrew. Open a shell tab and install Chromebrew with:

1 wget -q -O - https://raw.github.com/skycocker/chromebrew/master/install.sh | bash

Next, let’s install a compiler toolchain with:

1 crew install buildessential 2 crew install autconf 3 crew install automake

Check the version of GCC installed with:

1 gcc --version

An interesting problem appears if you compile a C or C++ program in your home folder and try to run the executable. By default, Chrome OS marks /home/chronos/user/ as noexec, so you will get a Permission denied error. AFAIK, there are two solutions. You can copy the executable to /usr/local/ or /usr/local/bin/. Alternatively, you can mark /home/chronos/user as exec with:

1 sudo mount -i -o remount,exec /home/chronos/user

If you want to make your home directory exec permanent, save the above line at the end of your .bashrc file.

At this point, you should be able to install other packages from Chromebrew, using crew install. If a certain package is not available, you can built it from source with the usual configure, make, make install.

For example, you can install a newer version of Vim with:

1 crew install vim

As a short example, I will show you how to build from sources a command line utility, rlwrap:

1 cd ~/Downloads 2 git clone https://github.com/hanslub42/rlwrap 3 cd rlwrap 4 autoreconf --install 5 ./configure 6 make 7 make install

Please note that the line autoreconf –install is typically not required. For this particular case, we’ve used autoreconf to generate a configure file. Usually, all you need to do is configure, make, make install.

rlwrap is useful if you have a tool that was not compiled with support for using the arrow keys to get access to the last command. E.g., python from Chromebrew will be more user friendly if you start it with:

1 rlwrap python

instead of the usual:

1 python

As I’ve mentioned before, you can use, in principle, any self contained Linux binary. For example, you can install the latest version of Go and use it on your Chromebook, see the wiki. The official Go binary is usually newer than the one provided by Chromebrew.

If you intend to do Java development on your Chromebook, Oracle provides stand alone Linux binaries for JDK. As a side note, if you want to be able to use a Java IDE locally, check my previous article. You will need Crouton and a desktop environment in order to run an IDE. For the current article, I assume that you need to use java and javac exclusively from the shell.

Start by downloading the proper JDK for your system, typically the Linux x64 version. Be sure to select the file ending in .tar.gz option and not .rpm. Next, we can extract the archive with:

1 cd ~/Downloads 2 tar -zxvf jdk-8u144-linux-x64.tar.gz 3 rm jdk-8u144-linux-x64.tar.gz

Please note that the version number from the above command may be different in your case. After the extraction, you should have a new folder, something like jdk1.8.0_144. Next, we can move it to /usr/local

1 mv jdk1.8.0_144/ /usr/local/

Add Java to your path by writing the next two lines at the end of your .bashrc file:

1 export JAVA_HOME="/usr/local/jdk1.8.0_144" 2 export PATH="$PATH:$JAVA_HOME/bin"

After you’ve saved .bashrc, don’t forget to enable the changes:

1 source .bashrc

or, simply close and reopen the shell tab.

Test if the Java compiler is available in your path:

1 javac -version

If you want to learn more about the Linux command-line applications, I would recommend reading The Linux Command Line by William E.Shotts Jr.