Learn to Code the Easy Way (Part 2)

This is a continuation of Learning to Code the Easy Way (Part 1).

We ended by talking about a program that comes with your Mac called “Terminal”. It’s basically a little white box that pops up that you can write words in, and it does stuff on your computer like install programs and run code.

Developers use Terminal to connect to computers in remote locations, to install programming languages, and to run commands that start and stop programs. When you’re developing a program, there won’t be a nice little icon that you can click that starts the program, so you need a way to start things up and that’s where Terminal comes in.

At the end of this article, after we’ve covered some basics about Terminal and installed Elixir, we’re going to type a command into Terminal that will run the code we wrote in Part 1, and print out “Hello World!” in the Terminal.

Step 1 — Terminal

Let’s open Terminal! Go to your apps view and look in the “Other” folder for Terminal. Or, open Finder and go to Applications -> Utilities -> Terminal.

Pro tip: a fast way to open programs on a Mac once you’re used to it is to press the command key plus the space bar, then start typing the name of the program.

The first thing we’ll do with Terminal is install a program called “Homebrew”. This program helps you install other programs from Terminal more easily (instead of the garble of text I’ll ask you to copy and paste into terminal in a second). When we use Homebrew, the commands we use to install programs will be much more human readable (like ).

So open up Terminal, and copy + paste the following command into it:

/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

Now hit the “return” button. You’ll see a bunch of lines written, and a prompt to hit “return.” Hit return again, then it will ask you for your password (the one you log in to the computer with). You’ll have to enter it without the luxury of seeing the characters you type or even a placeholder. Just trust that it’s recording your keystrokes; enter the password and hit “return.”

Suddenly Terminal springs to life! You’ll see some lines of output really fast. You’ll also probably have to wait for a few minutes (5–10 when I’ve tested) while things download and install. Don’t exit out of the program, just wait until you see your username and a dollar sign appear again once everything stops.

One more quick note: when you’re looking at anything programming related online, you’ll see commands that you need to copy and paste into terminal, and they’ll usually be highlighted or outlined some way like we’ve done here, but they will also have a dollar sign before the part that you need to copy and paste. Like this:

$ brew install elixir

The dollar sign is just an indication that the code is meant to be entered into Terminal, instead of somewhere else. So when you see that, don’t copy and paste the dollar sign with the rest of the code.

Step 2 — Install Elixir

Even though we’ve already written a line of Elixir code in Part 1, we can’t actually run the code till we’ve installed Elixir on our computer.

All you need to do to install the Elixir programming language on your computer is to enter the following command in Terminal and hit “return”:

$ brew install elixir

Voila! Your computer now has Elixir installed and you have full access to a programming language!

The little file that we created in Part 1 is a program (a very simple one), and we are going to run it from Terminal. When we run the code, it will print a line in our terminal screen that says “Hello World!”. But before we can run our code, we need to cover a few more things about Terminal.

Step 3 — A Little More About Terminal

Your computer has folders and files, and you usually look at them with Finder. However, Terminal actually is a file browser just like Finder, but it’s not as visually appealing. When you first open Terminal, you are in a folder called your “home directory”, and the name of the directory is your username. That’s why you see your username displayed in Terminal (before the dollar sign) when you first open it!

Within your “home directory” you have a few familiar “folders” (also called “directories”): Downloads, Desktop, Documents, and a few more.

If you’d like, open Finder and find your home directory with the Downloads, Desktop, and other folders inside! It might help you visualize what we’re doing inside Terminal.

At any time in Terminal, you can type the command and hit return, and it will show you everything in the folder. Try that now! (remember to not copy and paste the with the command)

$ ls

You should see a bunch of folder names, like Applications and Library and Music and so on. You can go into any of those folders (the same as double-clicking on the folder in Finder) by typing , where you replace <folder_name> with the name of the folder.

In Terminal, type in the following (remember not to copy the ):

$ cd Downloads

The stands for “change directory”, (directory means the same thing as folder), and this is an important part of Terminal.

Now we’re in the Downloads folder! Type and hit “return” and you should see all of the files and folders in Downloads displayed. Hopefully there aren’t too many and you can see your file from Part 1, “hello.exs” in there.

The reason we’ve gone through this exercise with and is because we have to be in the same directory (folder) as our file in order to run it. Think about it like this: if you were to open a file in Finder, you would start at the top level and double-click folders on your way down to where the file is. Once you clicked into the folder, you would be able to double-click the file and it would open. In Terminal, instead of double-clicking on folders we’re going to run the command on our way down to where the file is at. If you want to check what’s in the current folder, you can run the command .

We can go down by running , but what if we want to go back up to the parent folder of the one we’re currently in? The answer is that we run the following command:

$ cd ..

Now, back in Terminal, type and hit “return”. We’re back in the home directory! Run the command again, and verify that you see the folders like Desktop and Downloads and Documents again.

Run again to get back to where our file is, so that we’re in the same folder as so we can run our program.

Step 4— Running The Program

To run your program you need to be in the same folder or “directory” as the file. In this case, we saved our “hello.exs” file in the “Downloads” folder, so we need to our way to the Downloads folder to be able to run the file from Terminal (if you’ve followed along you should already be there). You can see which folder you’re currently in because the folder name will be displayed in Terminal right before your username and the dollar sign.

Now, run the following command in Terminal to run your program:

$ elixir hello.exs

If everything worked, you should see the line “Hello World!” printed out in Terminal!

Step 5— Do Something Useful

It’s great that we can create a program that prints out some text in Terminal, but it’s not terribly useful. In Part 3 (link coming soon!), I’ll show you a few Elixir basics. We’ll build a foundation so that we can use just a few basic Elixir skills to create and do interesting things with a website, powered by Elixir’s awesome website framework called Phoenix. You’re well on your way! These skills you’re learning are foundational to much of what you’ll do in programming, so even though we haven’t written much code yet you’ll have much more context as you keep learning.

Written by

Elixir dev building for the web with Phoenix

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store