Sunday, December 3, 2017

Installing Crouton and R-Studio on a Chromebook for Beginners Pt 3: R-Base and R-Studio

R-Base

Phew, OK so now you're in your chroot and everything is all Linux-y. This will probably offend someone, but every time I'm in a Linux environment like this one, it makes me think of my grandparents' ancient Mac that we could barely use to check e-mail and was really only good for playing Spin Doctor. There are certainly other styles around, but the xfce desktop has the classic Linux look and I do like it. 

Our ultimate goal, running R-Studio, is within reach now. When I was using a different Ubuntu setup (Lucid Lynx) on a PC in my undergrad, this step required no code and felt like downloading an app for your Mac or Windows computer. I think R-Base actually came already installed and installing R-Studio was as easy as searching for it in the Ubuntu "store" equivalent and clicking the install button. As far as I could find, no such option was available in the chroot. So we get more coding practice instead!

The first step is to open your terminal, which you should see on your dashboard at the bottom of your screen. Unlike opening the crosh in your Chrome OS, we don't need to give the shell command here in order to access the computer directly. My first step was to install git with the command
sudo apt-get install git
This will prompt you for your password (which you should have set the first time that you opened your chroot) because of the sudo command. I don't think that installing git is actually a pre-requisite to installing R and R-Studio, but as I understand it's generally good practice for anyone working with their Linux environment in a "developer" capacity. Basically, git creates a history of all of the coding that you've done, such that you can track the changes that you make. 

Breaking down that command line, you should recognize sudo as your key into the computer's root system. apt stands for Advanced Package Tool, which is a software interface that simplifies the retrieval and installation process for a certain type of package (Debian) on Linux platforms. The apt-get command tells your computer to use APT to find the following string. Install is pretty straightforward, which tells your computer what to do when it finds the following string. Other parameters that can be used in this place with the apt-get command include things like update. And finally git is the name of the object that you are trying to access.

Now to install the actual r-base, which is the most basic software that you need to use the R programming language. If you were to open up your R-base after installation you can perform all of the same tasks that we can do in R-studio, but it requires a lot more knowledge of code and doesn't have any shortcuts pre-installed. It actually runs inside the terminal, which is not at all intuitive for most of us who are used to using Windows or Mac interfaces. This is why many people have moved to using R-studio, which makes programming in R much more user-friendly.

In order to start the r-base installation, we need to give the computer the information it needs to access the r-base files with the command
sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys E084DAB9
Basically, this string is searching for and downloading the necessary "key" to authenticate the r-base files, since this is not included in the APT by default. The apt-key command tells your computer that you want to manage your APT keyring (which is full of authentication details for all of the APT packages). The string adv--recv-keys tells your computer that you are looking for a new key and the intervening string keyserver keyserver.ubuntu.com specifies where you want to look for it. E084DAB9 identifies the specific key you are looking for.

Next we use the string
sudo add-apt-repository ppa:marutter/rdev
to tell the computer where to look for updates for r-base in the future, since this also does not come as a default in APT. add-apt-repository does exactly what it says and adds a repository to the APT. In this case, the repository ppa:marutter/rdev is a place where the developer of R-base ( Michael Rutter) can put updates and other relevant information to keep the package running smoothly.

I didn't run into this problem, but apparently a lot of people run into the error
sudo: add-apt-repository: command not found
in which case you can install the necessary commands using
sudo apt-get install software-properties-common
Am I the only one excited that we know what that whole string does?!? The only thing we don't necessarily know is software-properties-common, which is a package that allows you to manage the most common types of repositories.

Now we are finally ready to install R-base using these strings
sudo apt-get update
sudo apt-get upgrade
sudo apt-get install r-base
This should also look pretty familiar! Since we just added a new repository with new information, we first need to update and upgrade our APT. Now the APT should include R-base and all of the information that we need to install and authenticate it. So we install it. Woot woot! If you are feeling curious, go ahead and open it up to see the fruits of your labor using
sudo R start
It's not very interesting to look at, but I always find it satisfying to see exactly what I've been working toward.

R-Studio

And this is it! We're finally doing the thing we wanted to do in the first place!

We start with
sudo apt-get install gdebi-core
The gdebi-core package is very similar to APT in that it allows you to install Debian packages. However, APT accesses its packages via ftp or http, whereas gdebi accesses its packages locally on your computer.

Next, we need to actually download the r-studio package onto our computer using
wget https://download1.rstudio.org/rstudio-1.0.44-amd64.deb
As you might have guessed from this string, the R-studio package is found online at the above address. wget stands for web-get, and is used to download files over a network.

Once the download is complete, we then use gdebi to install the package
sudo gdebi rstudio-1.0.44-amd64.deb
You'll notice here that we don't have to specify what we want gdebi to do with the file that we downloaded like you do when using APT. Since the packages that gdebi accesses are locally stored, it's usually safe to assume that they will not be ever-changing with updates like a package that's accessed over ftp or http might be. In this way, having different parameters for install, update, or upgrade are not useful in gdebi, so the default command for gdebi is to install the following package.

And that should be it! To run R-studio you can find the icon in your list of apps, or use
sudo rstudio start
which should bring up the nice and comfortable interface of R-studio. 

In my case, when I tried to run r-studio this way nothing appeared to happen in the chroot at all, and when I looked back at my crosh (which it took me awhile to do) I found the message
rstudio: error while loading shared libraries: libxslt.so.1: cannot open shared object file: No such file or directory
This file, libxslt.so.1, is apparently necessary for some part of the r-studio function that is above my paygrade. It has something to do with the XSLT language that is used in the GNOME project, but that's about as deep as I could get before I got lost. Luckily, the solution to this problem is pretty straightforward
sudo apt-get install libxslt1-dev
We just have to install the package that R-studio wants to use! And there we go!

You now have transformed your chromebook from what it was designed for (basically internet-only) into a machine that is freer than even your average full PC or Mac. Congratulations and well-done :-)

Saturday, December 2, 2017

Installing Crouton and R-Studio on a Chromebook for Beginners Pt 2: Crouton

Crouton

So now you're in developer mode because you're a programming beast and you have taken control of your own destiny. Congratulations.

The next step in getting to the ultimate goal of running R-Studio is to install Crouton, which creates a Linux interface within your chromebook with all (or at least most) of the functionality of a completely independent Linux machine. You can download what you need to install Crouton here, and they do have basic installation instructions available there. Coming into this with basically no programming background, though, I found myself searching all over the place for better instructions to help me get set up the right way and avoid the dreaded error messages.

After a lot of trial and error and a few power washes to undo what I'd tried, I found this blog with a detailed, seemingly thorough explanation of setting up a Chromebook with Crouton in order to install and run programs like R-Studio. I'll do my best to go through the steps that I took to get my machine set up, but this will be a good resource for anyone interested in installing other programs as well.

The first step is to download the newest version of Crouton here.

Next you need to open a terminal on your chromebook (called Crosh), using Ctrl+Alt+T in a browser window. As it initially opens, this terminal allows you to change settings and perform certain functions within the Chrome OS, a lot of which you can apparently do without going into developer mode (Here's an article that describes some of the commands you can use here). The very first command you'll give in order to install Crouton is
shell
which is the reason why we're in developer mode and will give you access to your computer outside of the Chrome OS.

Next we need to install Crouton to create a "chroot" with all of the options and extensions that we want. A "chroot" is what they call the Linux interface that we're creating, of which you can have many. I'll go through what all the parts mean in just a moment, but here is the command string that we can use for this:
sudo sh ~/Downloads/crouton -t core,xiwi,keyboard,x11,cli-extra,extension,xfce
This command has all sorts of extras and extensions that are probably unnecessary for our immediate needs, but the source that I ended up using for my code was setting up a user to do a variety of developer tasks within Crouton, and I figured I'd rather have this all set up now and not have to go back later to add it when I need it.

The very first part of the command, sudo, is used to give you access to the root system of the computer, meaning that you can make changes to basically anything. Once you're in Linux, this will prompt you to give a root password and will allow you to make changes to the inner workings of your computer. If you went against my recommendations and "Enabled Debugging" when you were setting up in developer mode, Crosh will ask you for the root password here (presumably to comply with the sudo command) and I wish you the best of luck in figuring out what that password may be. If you didn't enable debugging, Crosh doesn't ask for a password and simply starts the install.

The next part, sh, tells your computer that the following string needs to be interpreted by the shell before it can be executed, meaning that it is not a command in itself. ~/Downloads/crouton identifies the item you are accessing by name and location. The rest of the string is particular to Crouton and all of the information your computer needs to know this is internal to the Crouton package that you downloaded.

-t tells your computer that the following items are "targets" within the Crouton package that you would like to have installed. These are all of the options, extensions, extras, etc. that will affect how your chroot runs. Here are brief explanations of each of the targets included in this installation as described in the Crouton help section, with some interpretations from me in italics:
core      Performs core system configuration. Most users would want this.
xiwi      X.org X11 backend running unaccelerated in a Chromium OS window. Allows your chroot to run like an app/program within your Chrome OS
keyboard  Adds support for Chromebook keyboard special keys.
x11       X11 via autodetected backend. Does not install any desktop environment.
cli-extra Basic CLI tools such as ssh.
extension Clipboard synchronization and URL handling with Chromium OS. Allows you to copy and paste between your Chrome OS and your chroot
xfce      Installs the Xfce desktop environment. This is the part that gives you the visual and functional Linux environment

There are more targets available, which you can see using the command
sh ~/Downloads/crouton -t help
inside your shell. There are also additional parameters available, some of which are listed on the github website for Crouton. Some popular ones are:
-r       which tells your computer which version of Ubuntu (the Linux platform) to install. At the time of this publication, the default version that gets installed is Xenial
-e       which encrypts your chroot
-n       which gives your chroot a specific name, necessary when you have multiple chroots

The installation process takes anywhere between 15 and 30 minutes to complete, but once it's done you're ready to open your chroot! There are various ways to do this, but the simplest way that I've found is using the command
sudo startxfce4
This brings you to your full screen Linux environment, which you can manipulate just like a program or app window within your Chrome OS.

Congratulations! The next step is to get into the Linux terminal and install R and R-Studio, as seen in Part 3.

Installing Crouton and R-Studio on a Chromebook for Beginners Pt.1: Decisions and Developer Mode

The Decision

I have a chromebook that I love. I knew that I wanted one, but didn't have the time/interest in figuring out which one was best for my uses, so my husband did the research and got it for me. At the time, I was working as a animal behavior consultant and really only needed access to the internet and a word processor, and I was tired of having to use my ancient laptop from undergrad or borrow my husband's laptop which is functional, but also kind of falling apart. I ended up with an ASUS C202S -- the rugged version since I was bringing it to clients' houses and you could never tell what might happen.

Fast forward to a month ago when I was struggling with my career and decided I wanted to go back to school and get my masters in business analytics. I'll be using R and Python for sure, neither of which can run directly on my chromebook. So we brainstormed and came up with a few options:

1. We can figure out how to run a remote desktop off of my husband's beastly video-editing computer

2. I can use the browser-based R and Python platforms available for now, and count on using university computers for school-related projects

3. I can hack into the Chrome OS to make a Linux partition and run the programs directly on my computer


The remote desktop option is great in theory, since I wouldn't be limited by the Chromebook's minimal processing power and storage. However, the Chrome remote desktop app that exists is really more of a screenshare than anything else, which wouldn't work for us since we would basically be sharing the computer a lot of the time. The other apps available weren't working easily (something about the way our network is set up which is above my current technical understanding), and really I liked the idea of being a little bit more independent than that anyway. If he needed to shut down his computer while I was working at school, for example, I would be stuck and might lose what I'd been working on. Also, this was his solution, and while it was a good one, this seemed like an opportunity for me to stretch my computer skills and take ownership of the problem/solution myself.

Option 2 is obviously not ideal. The browser-based platforms are probably fine in a crunch, but not being able to save my work and being limited to doing all of my classwork on campus is not really a realistic solution for the next two years.

So option 3 it was, even though it was the biggest and scariest option of them all. 

Developer Mode

The very first step for this is to switch your chromebook into developer mode, which the internet says is fine if you know what you're doing (I didn't) and understand the security risks (I definitely didn't). I hesitated at this step because it felt so permanent (it isn't). They tell you to do scary things like "powerwash" your computer and ignore flashy beepy warning signs, all of which I would normally avoid. Then I grew a pair and did it anyway, and it wasn't that bad.

To get into developer mode, you have to hold down Esc + refresh (where f3 usually lives) and then tap the power button. Simple enough, right? But the first 3 times I did it all I got was a forced restart. So apparently they mean that you have to keep holding down the Esc + refresh buttons even after you tap the power button. Who knew?

Then you're in recovery mode which brings up a screen that says "Chrome OS is missing or damaged." Which sounds scary, but is a lie. So you ignore that and hit Ctrl+D, which brings up a screen that says, "To turn OS Verification OFF, press ENTER." What they mean is, "To enter Developer Mode, press Enter". After you press Enter, you get another scary white screen that says "OS verification is OFF", which you should get used to, because it will show up every time that you reboot now and will apparently beep at you if you wait long enough (I have been able to avoid this so far). Every time that you see this screen from now on, just hit Ctrl + D to reassure your computer that you know what you're doing (because you do, don't you?) and everything will be OK.

Then your chromebook restarts and takes awhile to figure out developer mode (or whatever it's doing) and that's it! Developer Mode is UNLOCKED! WOOHOO! The internet may tell you to consider Enabling Debugging Features while you're setting up your new fancy super-technical-coder-hacker-genius OS, but DON'T LISTEN! IT'S A TRAP! Just ignore that little box that you can click that does some things that I don't really understand but that sound appealing--I had to re-powerwash my chromebook several times because of this mistake since it seems to disagree with the Crouton installation. There's probably a solution out there that will fix the problem, but I couldn't find one other than don't click the box. So just don't click it.

Now, pleasantly, you'll find that your chromebook looks just like it did when you first got it. You sign in with your google account like normal, and everything is shiny and clean and not at all scary. Congrats :-)

Now on to the Crouton installation in Pt. 2!

English Syntax Trees and Question Creation with Flex and Bison

In the first (official) semester of my PhD program this spring, I was able to take a Computer Science class called NLP Methods in which we m...