Installing Arduino tools on Gentoo

emerge arduino, done? No.

Getting set up to write & upload Arduino sketches using Gentoo Linux is a bit more involved than on the friendlier distributions like Ubuntu. I found the available documentation to be somewhat incomplete, dated, and fragmented. Fortunately, I got it figured out and distilled down to a fairly simple process. Read on to learn my method.

The first step, naturally, is just to install the Arduino IDE from Portage:

emerge --ask arduino

That will leave you with a functional Arduino IDE, but no ability to actually compile sketches. For that, we need a functional AVR toolchain. This is an example of cross-compiling; generating code for an AVR processor from your x86 or x86_64 machine. Fortunately, Gentoo is just about the friendliest Linux distro for setting up a cross compiler. Most of the time, anyway.

We’ll need to take these steps to get this working:

  1. Create a local overlay for crossdev to use when it builds the AVR toolchain.
  2. Build the AVR toolchain.
  3. Wrap up a couple loose ends

Creating the overlay

Here’s the fast way, taken directly from the Gentoo Wiki. This assumes you want to put the overlay at /usr/local/cross-avr

mkdir -p /usr/local/cross-avr/{metadata,profiles}
echo 'cross-avr' > /usr/local/cross-avr/profiles/repo_name
echo 'masters = gentoo' > /usr/local/cross-avr/metadata/layout.conf
chown -R portage:portage /usr/local/cross-avr

Now, create /etc/portage/repos.conf/cross-avr.conf with the following contents:

location = /usr/local/cross-avr
masters = gentoo
auto-sync = no

That’s done, let’s build a toolchain!

Building the AVR toolchain

By default, the version of GCC that crossdev will build has the ‘sanitize’ use flag enabled, and it will fail to compile your Arduino code. Add cross-avr/gcc -sanitize to your package.use.
If you don’t already have crossdev installed, do it now. Run emerge --ask crossdev and make sure portage isn’t planning on doing anything insane before allowing it to proceed.
Once you’ve got crossdev installed, all you need to do to set up the AVR toolchain is run crossdev -s4 avr
If you’re running ~arch, it’ll pull in the unstable version of GCC, 5.x. That won’t work for AVR, so you don’t want that. Do crossdev -S -s4 avr instead. When GCC 5.x eventually gets marked stable, you’ll have to manually add >=cross-avr/gcc-4.9.3 to your package.mask. Once crossdev finishes building the compiler and linker and such, that’s the tricky bit done. Congratulations, you have a working AVR cross-compilation environment.

Loose ends

There’s just a couple remaining issues to wrap up before everything works properly. Firstly, you might find that you get this error message when you try to compile a sketch:

/usr/libexec/gcc/avr/ld: cannot open linker script file ldscripts/avr5.x: No such file or directory

This is Gentoo bug #147155, which is still open as of this writing. To fix it, you need to create a symlink:

ln -s /usr/x86_64-pc-linux-gnu/avr/lib/ldscripts /usr/avr/lib/ldscripts

Substitute ‘x86’ for ‘x86_64’ if you’re running a 32-bit system.
You might also run into an issue where the menus in the Arduino IDE appear in the wrong place, jump around, and are generally a pain in the ass. To fix that, you’ll need to install the wmname utility and run wmname LG3D before starting the IDE. You’ll probably want to have that command autorun when your X session starts up. If you’re running a full desktop environment like Gnome or KDE you should have some built in settings for startup programs. Otherwise, if you’re starting up your own window manager and session with .xinitrc you can just add the command there.
Additionally, in order to actually upload a compiled sketch to the Arduino, you’ll need to add your user account to the uucp group.
Assuming I didn’t leave anything out of my notes, that should do it. If I’ve missed something, let me know in the comments below.
Next (eventually): Building Arduino sketches from the command line and getting the obnoxious IDE out of you way.

Thanks for reading


Leave a Reply

Your email address will not be published. Required fields are marked *