Mariel de la Garza
mcat's devblog

mcat's devblog

Environment Variables are Annoying

Photo by Markus Spiske on Unsplash

Environment Variables are Annoying

(if you don't know what you're doing yet)

Mariel de la Garza's photo
Mariel de la Garza
·Jan 5, 2022·

6 min read

Table of contents

  • Why am I even here
  • Libraries ... but make them not npm?
  • Setting Environment Variables
  • So did it work?
  • Okay, so what did work?
  • Conclusion

Environment variables are annoying.

I mean, they're not. Except they are, when you don't know what you're doing.

And I don't mean the API keys you stuff inside .env in your React project. I mean the things that have to do with your shell. Which is not a turtle shell. Also today I had to deal with Java, which is not coffee.

Why do none of the software engineering words mean what you think they will mean?

Why am I even here

I started working through Harvard's CS50x again, but for real this time since I start school in March. My background is web development - I went to a bootcamp, don't have a CS degree (which is why I'm starting school in March), and had whole other career(s) first. In my bootcamp we spent most of our time in Ruby, and as a working dev I spend most of my time in JavaScript. HTML and CSS get sprinkled in there as needed.

I don't have experience in compiled languages - JavaScript is often referrred to as an interpreted langauge, since when you're working there's not a separate compile step. It just goes to the browser and ...works. It's also referred to as "just-in-time compiled" since it's being compiled by the browser at run time, instead of having a separate compile step before being executed.

The point is I've never worked in C or its cousins or Java or anything other than Ruby, JavaScript, HTML, and CSS. This means trying to install a library for this assignment in C went about as well as you'd expect.

Libraries ... but make them not npm?

CS50 uses C for the first ~half of the course and utilizes certain libraries along the way. My usual experience with a library is running npm and then moving on with my life. I might need to import certain things but otherwise it's not really an intrusive process.

So far, the libraries we're using in CS50 get imported at the top of the .c files using #include which, okay, is similar enough to import [thing] from [library]. The difference is getting that library.

NOTE: this pain was self-inflicted. The instructions for Problem Set 1 literally tell you how to get running with VS Code in the browser. There was no reason for me to try and get running locally yet other than "I wonder if I could figure that out."

Step 1 of most things is to head to the docs. Right at the top is "Installation" - excellent. I proceeded to run the commands under Ubuntu annnnnnnd: "Unable to locate a Java Runtime that supports apt." I figure okay, I guess I need to install Java.

So I go to "java.com" as instructed and "download Java for MAC OS X". Then I run the command again.

And get the same error.

After some googling I learn there's Java and there's the Java Developer Kit. It's suggested by The Internet that my error is because I need the JDK, so I install that. Then I run the command again.

And get the same error.

I keep searching and learn the error may be because I haven't specified the JAVA_HOME environment variable. I know what environment variables are and I've used them in different projects so I figure this is probably super easy.

And it is. But only now that I know what I'm doing.

Setting Environment Variables

What the heck is an environment variable? It's exactly that - a variable that gets executed in your environment. The environment is the space you're working in.

If you open your terminal and run env you'll get back a list of things with the format VARIABLE_NAME=value. In mine I see SHELL=/bin/zsh and LANG=en_US.UTF-8.

In this case, our environment variables are used in our shell, which is what lets us interact with our computer's system when we open a terminal. There are many kinds of shells. The two you'll see most often are bash and zsh.

The thing that can be most frustrating when working with the command line is ... that it's the command line. There's no picture to interact with - no pretty UI, no icons. Just text. So much text.

It can be overwhelming initially because what you get basically looks like the default Matrix screen. But it's fine. Really.

Your shell has a profile that specifies certain settings, including your environment variables. Instead of going through all of the ways I failed we're just going to cover what worked. I'm using zsh:

Inside ~ (for me: /Users/marieldelagarza), run ls -la and you'll see alllllll the things, including your hidden files. In the list you get back you should see .zshrc - you don't want that file. What you want is the one named .zprofile.

If you've previously set up VS Code to be launched from the command line, run code ~/.zprofile. It should open the file using VS Code. Inside there, put export JAVA_HOME=$(/usr/libexec/java_home). Save and close the file, and kill and restart your terminal. Now, run echo $JAVA_HOME -- you should get back something like /Library/Java/JavaVirtualMachines/jdk-17.0.1.jdk/Contents/Home.

That's it. Things I had to learn about when figuring this out include variable persistence in shells, how to find the .zshrc file to begin with, and what JAVA_HOME is for. I'm too tired to write all the details about all the things, but those are some things to search for if you're interested. Otherwise, the few steps above - that's it.

So did it work?

No. I ran the command again annnnnd got the same error. At this point I realized the curl command is using bash, so I dropped into bash instead of zsh annnd no. That didn't work either and I got the same error. Then I thought, okay, I know "sudo" works because I've used it for other things, and I know "apt" works because I've used "sudo apt-ge...." and then it dawned on me. On my Windows machine I was running Ubuntu. But I'm on a Mac now. Am I sure I can use apt? I ran a command using sudo apt-get and got back sudo: apt-get: command not found.

I searched around some more and finally realized: Macs don't use that command or apt install. They use homebrew. The command was failing because it's literally not a thing my terminal does. Oops.

Okay, so what did work?

Going back to the instructions. When I looked at the documentation for CS50 again I read down a few more lines and ...oh. Right there: From Source (Linux and Mac). I followed those instructions and WOW IMAGEN it worked fine.

Conclusion

Nothing is that hard if you already know how to do it. Otherwise you might cry a little and then come back later but it's fine you can do this.

Read the instructions. I know it's not a thing engineers generally do until after messing things up but try it and see what happens.

As long as you're learning new things you're doing great.

 
Share this