2015-02-05

Have you ever tried to go with Linux on a consistent basis but found it hard to
get used to? Do you feel confused when having to choose a Linux distribution in
order to develop software? Are you afraid of incompatibilities that might come
from the switch, or unsure what tools to use and how to set them up with minimal effort?

In this article I'll show you how to set up a Linux machine for developing
software. I'll be using VirtualBox, but should you install it on your physical
machine right next to or over Windows you should be fine. However, make sure
you backup your important data, as overwriting your entire disk to hold Linux
will result in all its data being wiped out. If you're unsure wether you want
to go 100% with Linux, then I would strongly advise using VirtualBox first, as
a warmup.

Why Should You Choose Linux?

Jumping to the Linux ship is not a decision to be made lightly. As you
know, Linux gives hackers a hard time because viruses hardly ever work, and
also it's free (as in beer, there's no need to pay for the system). For the
consumer market these are indeed a great couple of reasons to switch, but for us
as developers we need to consider more than that. We need certain needs
fulfilled in order to provide value to our businesses.

So why should you choose Linux to develop software? Here's a set of reasons
that might support the decision:

You always stay up to date. Usually in closed operating systems (i.e.
Windows or Mac OS X), major upgrades require you to pay a certain amount of
money to access them. That doesn't happen in Linux, as upgrades are free too.

Along with the previous item, you get a massive software repository.
Thousands of packages are available from the moment you install Linux, so you
don't need to ask Google for a particular software product in order to
download it and use it, as you do in Windows. Most Linux systems come with a
package manager of some sort that allows you to seamlessly install software—anything from a music player to a text editor to software development tools.
And they get updated too, with no extra fees. Of course not
everything is included, but most open source software is, and it already brings
you a lot of value.

It is lightweight. Using a Linux system will help you save your
machine's resources, as most software that runs on Linux demands less memory
and disk space from your computer. Let me give you a specific example that
I've come across. I run all major three systems: Windows for leisure, Mac OS
X for recording work, and Linux for general development. I've run a very
basic, layman statistic on how much memory my computer requires on boot.
Linux was always the winner by far. This resulted in a much smoother work
experience, as I have more memory available to accomplish my goals.

It is open. The fact that Linux is open relieves a lot of pressure in
regards to your dependencies. Because it is open, Linux is subject to a lot
more people's and companies' contributions. Problems are solved for the sake of
openness and sharing. If one company provides a solution to improve Linux,
you and many more people get it free. This reduces the chance of problems occurring, and when they do they get fixed a lot faster, which improves our
business.

It is flexible. Unlike Windows or Mac OS X, you can customize Linux to
your liking, whether to make it a powerful terminal-based system or a
gorgeous, integrated desktop environment. All sorts of setups are possible
because of the way Linux works inside. The kernel is the very core of Linux
and everything around it is just an extension, from the graphical desktop
environment to the set of applications it comes bundled with: music
player, office suite, programming language setups, text editors, etc.

Servers use it. What better way to simulate a production environment than
to actually use it? Most web product technologies usually run in Linux for
all of the reasons above and more. Closing the gap between development and
production is ideal as it reduces the chance for problems to occur because of
all of the differences.

Install a Linux Distribution

In this specific article we'll use the Ubuntu Linux
distribution, as it is pointed towards beginners and Windows users who want to
consider the switch. The installation walkthrough below will refer to Ubuntu, but you are free to use any other distribution. Depending on which you go for, the installation and package management instructions change, so keep that in mind when using
something like Fedora, CentOS, or Arch.

Creating Installation Media

As with any operating system, you'll need to have installation media to
start with. You'll need to download the disk
image and burn it to either a DVD or a
USB flash drive. I personally would use the latter as it is fast and easy to get
a 2 or 4GB flash drive to put the installation image on.

Skip the rest of this step if you want to use VirtualBox.

For Windows you can use Linux Live USB
Creator to create a USB flash drive really
easily. After following the installation steps, you will see a dialog where you
tell it which disk image to use and the location of the flash drive. Remember
that you need the flash drive's full space; partitions won't work. If you have
important data there, take it out first.



As for Mac OS X, take the disk image and use Disk Utility to burn it to a
flash drive. You will need its whole disk space to burn the image, so make sure
you back up your data before you do this.


Setting Up VirtualBox

Skip this step if you're installing Linux on a physical machine.

If you don't know about
VirtualBox, it is a way of
emulating an entire operating system inside what you already use. It's
available in most platforms and it provides a safe way of trying a Linux
distribution without committing too much, which is exactly what is required.

After downloading and following the installation steps, you'll be ready to
create a new virtual machine. Follow these steps to create a more than
capable setup for running Ubuntu:

Click New in the top menu bar.

Type the name of the Linux distribution as the name of the machine. It will
specify the type and version for you accordingly. In this case, type "Ubuntu".

Set the amount of RAM to 1024MB. Most likely you won't need more than this, but if you're unsure, pump it up to 1536MB—that's 50% more.

Set the hard drive option to create a new one. When clicking Next you'll
want to select at least 8GB of size. I usually go for 12GB to give me a
comfortable margin.

Hit Create to create the virtual machine.

In order to give it a little more responsiveness we're going to tweak its
settings. Select the machine and click on Settings.

Under System > Processor, set the number of processors to 2. Do this only if
you selected a 64-bit version of Ubuntu. If not, stick to a single processor.

Under Network > Adapter 1, use Bridged Adapter. This will make network
operations faster.

Under Display > Video, use 64MB of memory and enable 3D acceleration. This
will keep the system from freezing sometimes.

Under Storage, select the empty IDE controller. That's where you'll point
to the disk image you downloaded earlier.

Click on the tiny CD icon under Attributes and choose a virtual disk
file. Locate and select the disk image you've downloaded before.

Click OK and you're done.

Once you follow these steps you'll be able to select the virtual machine and
run it. Click on the button in the top menu bar and the machine will run. After
that, follow the instructions to install Linux on your machine. Once everything
is installed, you can restart it.

Remember to eject the disk image from the
virtual CD drive so that it doesn't run from it again. A safe way to do this is to
shut down the virtual machine upon installation, go to Settings > Storage, and
select no disk image whatsoever.

Guest Additions

The next and final step to make Ubuntu work well is to install Virtualbox's
guest additions. These will allow you to use your screen to the fullest and
take full advantage of your host computer, amongst other things.

Before anything else it's important you install a package from Ubuntu's
repositories. Open a terminal window by clicking on the top left button—the
dash—and typing "Terminal". Select it and type this instruction into it:

Type in your password and press Enter. The package will be installed. Then,
at the top of the machine's window, select the Devices > Install Guest Additions CD
image. This will pop up a dialog box that tells you to run a package. Click Run and after issuing your password (if it comes up) the guest additions will be
installed. All you'll need to do next is to restart the virtual machine.

Should everything go as planned you should be ready, with something like this.


Setting Up the Environment

Congratulations, you have Linux up and running! The hardest part is over. Now
we just need to go over some important aspects of this new system so that you can
understand how it works and how it will affect your software development work
later.

Desktop Environments

You're probably used to the fact that both Windows and Mac OS X have a strict
set of design guidelines for applications and their look and feel. Windows has
gone under an overhaul with version 8, and Mac OS X Yosemite is going through a subtler change.

There are also design guidelines for desktop environments
in Linux, but the important thing is you still get to choose. Linux's nature
is modular, as you can tell by the kernel that focuses on the bare minimum set
of features. Everything else extends the kernel with new features: firmware,
desktop environments, browsers, word processors, music and video players, etc.

Because of this nature, you have the freedom to pick whichever graphical
environment you like best. For example, Ubuntu picks Unity as the default
environment, as you saw in the previous picture. There are others though.

I
personally like to use KDE, because it is very close to Windows in
terms of major design and experience guidelines, but it is far more
customizable. There are loads of themes you can put in, so it fits your style
better. A lot of settings can be adjusted through a single control panel. If
you want to install KDE in Ubuntu, you should open a terminal and type this
instruction:

This special package will adapt your Ubuntu system in order to use KDE. Feel
free to try it out, and if you're not up to it, you can always uninstall it and go
back to what it was before:

KDE and Unity are just two of them but there are a ton more:
Gnome, XFCE, and LXDE are
examples of desktop environments. Simpler window managers are different in that they are smaller and have less features. They are also a little harder
to configure by yourself, so remember that. Examples of these are
i3, Openbox,
Fluxbox, awesome,
xmonad and many more. These are more suited for experienced, power users who rely a lot more heavily on terminals and terminal-based applications.

Users, Groups, and Permissions

The way Linux works in regards to user management is universal across
distributions; it's part of the Linux kernel. Much like Windows and Mac OS X,
each user in Linux gets a home folder (usually under /home/my_user/), and it
has permissions to manipulate files and folders that live under this directory.
The most significant difference between Windows and Linux (Mac OS X works
pretty much the same as Linux) is the permissions system. In fact, let's look
at how they work.

Consider the following example:

Notice how three distinct categories show up: Owner, Group, and Others.
Each file or folder has specific permissions for all three.

Owner matches the user that created the file, often referred to as
the owner.

Group matches the group that the file belongs to. Usually it begins with
the owner's main group, with the same username.

Others matches everyone else.

What permissions can we set for each category? There are three different
actions that can combine up to eight different ways: read, write, and
execute. Usually, the owner and group have permission to read and write to a
file, while everyone else might just only read.

Judging by the image above, you can change the permissions in the GUI. Usually
Linux veterans rely on the terminal to change permissions on files. You do it
with the chmod command. An example to make the previous file writable by
everyone else might be:

o stands for "other". +w says it wants to make the file writable. If you
wanted the file not to be writable, you would type -w instead. More
information can be obtained through the manual
page for the command. Yes, most of Linux's
commands have documentation that can be retrieved by typing man <command>.

The reason I'm showing you this example is so you can understand how users and
permissions work in Linux. Later on, when setting up a development environment,
you might need this and other commands to complement the setup process.

Writing Code

Software development is in its core about writing code. Choosing the best tool
to do it is essential, and the open source community provides a lot of different
alternatives.

Text Editors

Editors such as Notepad++,
jEdit and Sublime Text are
popular choices for the Windows platform. They are easy to install, and have a
minimal set of features and a low learning curve. If you use either jEdit or
Sublime Text, you'll be happy to know that they are also available for
Linux. Instead of downloading the Windows version, go for the
Linux
one
instead. You can find jEdit here and Sublime Text here.

However, if you feel you want to step up your game with power editors such as
Vim or Emacs, you can always
install either of them. Specifically for Ubuntu, you can open the Software
Center that's on the left side of your screen, search for either Vim or Emacs
and install them. Of course you can also go to the terminal and install them through
there. These two editors have been used throughout the years and have proven to be
extremely powerful and more than capable of assisting you in your code editing
tasks.

Integrated Development Environments (IDEs)

If standard text editors are not your thing, you can always reach out to fully-fledged environments. The most well-known contenders are
NetBeans, Eclipse,
IntelliJ and others. Most of them are available
for Linux as well, so just follow the same procedure as you did when installing
them on Windows. The experience should be pretty much the same. I recall having
used NetBeans on the three major systems, and almost nothing changed for me;
after all that's the main point in using IDEs.

The Terminal

Definitely one of the best features in the *NIX ecosystem, the terminal is
essential to any developer. Even though most IDEs take care of almost
everything for you behind the scenes, essentially what they do is call commands
that you could run yourself in a terminal window.

In Linux, you have a terminal application ready. In fact you have already used
it to run some commands. No matter which Linux distribution you choose, you can
always rely on a terminal. You can customize its appearance by selecting Edit >
Profiles, choosing the default profile, and editing its colors and font.

Web Development Setups

Now that you are comfortable enough around Linux's tools, I can explain to you how
to set up a development environment. We'll consider the most common setups for
Ruby, JavaScript, and PHP. Depending on which one you go with, feel free to
jump ahead.

Ruby Setup

Ruby has always been easy to run under *NIX based systems. If you use Mac OS X,
you'll know that Ruby comes already bundled with the system. Even though
most distributions have Ruby packages, working with them on a regular basis is
not as easy as installing a version manager in your own home folder and
installing a Ruby version from there. Remember when we talked about
permissions? Ruby packages are installed only when you've provided your password. If
you choose to go down that path, you'll have a hard time doing your job because
of all of the permissions you don't have.

There are three major Ruby version managers: RVM,
rbenv, and
chruby. In this article we'll go with
chruby, but if you're used to another one then go ahead and follow their
installation instructions.

To install chruby we'll visit the install
section in the README and run
the instructions mentioned there in a terminal window. You already know how to
open one, so go ahead and do it and paste the code snippet:

After typing your password you should have chruby installed, but we still have
more things to do. Indeed we have just installed a means of using Ruby, but we
have no rubies yet. How do we actually install a version? If you visit the
section on installing rubies
you'll be presented with several options to install a Ruby installer. We'll go
with ruby-install, so you'll need to visit its
documentation and type
some more commands into the terminal:

When the commands finish running, you should have ruby-install in your system.
One final step before installing a Ruby version is to make these utilities
ready to be executed. You do that my editing a file in your home folder called
.bashrc.

Open the Files application on your dock at the left and navigate to Home.
You will see all visible files and folders but not the hidden ones. .bashrc
is hidden from you because its name begins with a dot. To reveal hidden files, select View > Show Hidden Files, as in the
image below.

You will have a whole lot of other translucent files. Search for .bashrc and
edit that file by simply double-clicking it. At the end of the file add these
two lines, which will enable chruby and ruby-install in your system:

The first line will enable chruby, whereas the second one will enable
auto-switching between different rubies depending on having a special file
called .ruby-version in one particular folder, whose content includes the
Ruby version you specify.

To install the latest version of Ruby (Matz's version), go to your terminal and
type the following instruction:

ruby-install will assume it's the latest stable version, but you can specify a
version at the end of the command. When you install Ruby you will be able to
use it by typing:

The last line will print your Ruby version, which means that Ruby is up and
running.

You are now ready to develop Ruby projects! You can start by installing either
Ruby on Rails, Sinatra, or any other gems you might need for your project.

JavaScript Setup

Working with JavaScript is usually very easy to get started with, due to browsers
being an active development asset. However, that's not the case in the server
side. Node.js is available in most Linux distributions but the
latest version is not always ready to be installed. For that purpose we'll follow a
similar route to Ruby's: install a Node version manager, install a version, and
go from there.

The version manager that we'll use for this article is
nvm, but you can also look up
n as an alternative. If you go through the
README you will need to
paste the first instruction into a terminal:

If you don't have curl installed, the command will complain. Type sudo
apt-get install curl in your terminal, and then run the previous command again.

The command retrieves a script from GitHub that is run directly on your
system. You will have nvm installed in your home folder so you won't have any
trouble with permissions, unlike when you install it from the Ubuntu packages. You
can check if nvm is correctly working by typing nvm in the terminal. If an
error occurs, probably you'll need to edit your shell configuration file. Edit
the .bashrc file as we did for chruby, and add the following code at the end
of the file:

The next step is to actually install a Node version. Now that we have nvm we
can type nvm ls-remote to retrieve all Node versions available. The last
stable version is at the bottom, look it up, and install it by typing nvm
install <version>; replace <version> accordingly. After that you'll be able to
run Node.js, but only temporarily, because we still haven't configured our system
to always use that Node version. We do that with:

You're done! You can now start developing JavaScript code in Node.js. Remember
that npm comes bundled in so you can start installing packages straight away.
An example of installing grunt-cli would be:

PHP Setup

PHP has always played a big part in the software development industry over the
years. You know for a fact that most hosting providers run on Linux with an
Apache server along with the PHP extension enabled. All you have to do is paste
PHP files inside your own remote folder and you're done. This is what makes it
so popular.

We'll cover two specific ways of setting PHP up in Linux: one for Laravel and
another one for WordPress. These tend to be the most used platforms for
publishing content and building web products, respectively.

Laravel

Laravel has proven to be a capable platform for building products for the web
without having to learn a new language for those that are used to writing PHP
code for a long time.

We'll install PHP first. The way you do that is by installing some PHP packages
available in Ubuntu. Most likely other distributions will have similar
packages, so be sure to look it up. To install the package, go to your terminal
and type:

All of these packages will support Laravel. After installing these we can
install Laravel by visiting its website and
following the instructions there. It tells us to download the respective
PHAR binary and put it in an appropriate
place. Move the binary to /usr/local/bin/laravel and make it so that it can be
executed:

Now we can type laravel and we get a welcome message. You're all set to build
your new Laravel application!

XAMPP + WordPress

The reason I use two different approaches for PHP is because I found this
second one to be a lot easier for building WordPress websites, as it simulates a
hosted environment with just an installer.

Installation

Visit XAMPP's download section and
download the 64-bit version of the package. If you selected a single core for
your VirtualBox machine, then select the 32-bit version.

Once it's done you'll need to make the package executable and run it:

Tip: Keep pressing

Tab

to find the correct file's location in between
slashes.

With this package you will have an isolated web system with an Apache server, a
MySQL database, and PHP 5.

The next step is to actually download WordPress. Grab a
copy and put it wherever you prefer. I usually create a projects folder in my home directory.

There are still a couple of things you need to configure before you start
building a WordPress website.

Configure a Virtual Host

Virtual hosts are the best way to isolate that website you're trying to build.
Your project stays in one folder, so you'll just need to configure Apache to refer
to it, along with an entry in the hosts file.

Let's consider the example of a project called my_blog:

We'll want to type my_blog.dev into the browser's address bar.

It should point to our WordPress project.

First, let's edit /etc/hosts. We'll use Gedit to add my_blog.dev as a
reference to our own machine instead of just localhost. In the terminal,
type:

Where you see 127.0.0.1 localhost, change it so it looks like this:

That way your system will respond to both names. Now it's time we let Apache
associate that name with our project.

First, the same way we edited the hosts file, edit
/opt/lampp/etc/httpd.conf. Look for Include etc/extra/httpd-vhosts.conf and
uncomment it by removing the pound sign (#). Save and leave the editor.

Second, edit /opt/lampp/etc/extra/httpd-vhosts.conf so that it contains
this configuration:

Notice how we are relating the server's name to the name we gave in the hosts
file. Also, we are setting the root of the URL to our project's folder. This is
the configuration we need to tell Apache so it goes to our project's folder and
runs a standard WordPress website, or any other PHP platform for that matter.

All you need to do is restart the server. In the terminal, type the following
instructions in order to stop and start it right after:

Remember that you'll need to set up a database. Type localhost/phpmyadmin in
your browser to accomplish it.

That's it! If you type my_blog.dev into the address bar, you should be greeted
by the WordPress installation steps and move forward.

Maintain Your Linux Desktop

Coming from a Windows perspective, most likely you're aware of the historical
times when previous versions of the system didn't always run smoothly.
Sometimes applications would crash without prior notice, or run rather faultily
for no particular reason. Not to say that Linux is perfect, but it does give you the ability to control how your system is running with simple applications.

If you want to know how much memory your system is consuming, you can use the
bundled system monitor. Click on the Dash and type System Monitor. The
application should pop up, so click it and notice the information that's
displayed.

The middle section shows you how much memory in total you are using. If you're
unsure about what applications are using the most memory, you can check the
Processes tab and glance at all the processes currently running. If you suspect
something with a particular process is running badly, you can select it and kill it,
freeing memory and allowing you to run it again if needed.

The same kind of monitoring can be done for disk space. In the File Systems
tab you can see all disk partitions being used for Linux and how much space
they're taking out of your hard drive. If you want more details on how the hard
drive is being used, you can resort to the Disk Usage Analyzer. Find it in the
Dash, select it, and then look for the disk partition you wish to scan. You will
have a detailed, recursive chart that looks like this:

Conclusion

Even though the article is relatively extensive, it only covers a drop in the
ocean that is the Linux operating system and community. I've just given you the
tools to get started with development in three distinct environments, but still
there's a lot that can be done. From my experience, I can tell you that
practice alone will get you ahead in getting along with this open experience.

Don't get ahead of yourself early on. Rely on the vast community out there and seek
help from anyone. Usually the Linux community is friendly and welcomes
newcomers. A lot of content is available on the Internet and it should be rather
easy to solve basic to moderate setbacks.

Give yourself some time to embrace the Linux way of doing things. Only after
some time will you be able to grasp the full power of the command line and its
abilities. Use different editors, install packages, try multiple desktop
environments, and crete your own comfortable development experience.

Show more