How To Clean Ruby From My Mac
In this post, we're going to show y'all how to install Crimson on your Mac. Forth the manner, we'll learn about the steps involved and various bits of cognition required to get up and going. So if you're totally new, don't worry! I'll list each step and follow it with an "actress credit" section where I'll explain that step in depth. If y'all've washed this sort of thing before and simply demand some directions, I've included a TLDR section showing what commands to run.
Before we start…What is Ruby?
Scarlet is an open-source programming language with a strong developer focus. Created in 1996 past Yukihiro Matsumoto, Ruby became really popular in the late 2000s with the introduction of the Ruby on Rails web framework. While Ruby is used quite ofttimes for web development, information technology's also pop as a scripting language.
Ruby is known for being like shooting fish in a barrel to learn and fun to use. So let'south find out how easy it is to become up and running!
What we're going to exercise
- Open upward the terminal
- Install a package director
- Use the package manager to install Reddish
- Update our PATH
- Write and run our showtime Ruby program
What you'll need
- An net connection
- Administrator privileges (yous should have this if you're working on a personal device)
- 15 minutes (or just a few if y'all just need the commands)
TLDR—Run these commands from the final
/usr/bin/ruby -due east "$(ringlet -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
brew install cerise
echo 'consign PATH="/usr/local/opt/ruby/bin:$PATH"' >> ~/.bash_profile
source ~/.bash_profile
Step 1: Open up the final
The terminal application on macOS is an operating system command line interface (CLI) that comes installed on new Macs. You can speedily open up the terminal past doing the post-obit:
- Type ⌘+space bar. This brings upward the macOS spotlight search, a search utility for opening apps on your Mac.
- Type final into the spotlight search.
- Printing Enter.
Extra credit for step ane
When you open a final (assuming zero configuration), you start at your home directory. Did y'all know that a version of Ruby comes installed on your Mac by default? We can see that by typing some commands into our final. Thewhich command allows us to run across where on the computer an executable file lives.
which ruby
We passed Ruby every bit an argument, then the command is finding the Crimson executable location. If Cerise isn't installed or available to us, thewhich command will permit us know it couldn't find Cherry-red.
Now let'southward come across which version of Ruby we have installed past typing in the following control:
ruby -v
Here, we're invoking the Ruby CLI and passing the choice-fiveto information technology. The-vpick tells the Ruby CLI to render the version of the Ruby installation to us. If y'all don't need the latest and greatest Ruby version, you lot could stop here. Simply as good software engineers and system administrators, we similar to stay on top of things and install the updated versions when nosotros can. Permit's do that past installing a package manager.
Stride 2: Install a package manager
Agree on! What's a bundle director? A parcel manager is an application whose chore is to manage software on your calculator. In this instance, managing means installing, updating, and removing software every bit needed. Package managers will install software in a consequent fashion and keep your computer tidy.
As it turns out, macOS has an awesome packet manager called Homebrew. Ironically, Homebrew is written in Cherry-red! Permit's go that installed.
The Homebrew homepage has the install information. We're going to follow that by running this control in our terminal that nosotros opened in step one:
/usr/bin/red -eastward "$(coil -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
This control is going to install Homebrew for usa. The cool matter almost the Homebrew install script is that information technology will tell you lot what information technology'south going to do and prompt you for a yes or no before proceeding. The install script is very informative. One of the important things it points out for us will exist that the install script is going to create some subdirectories in the/usr/directory. More on that subsequently.
Extra credit for step two
Whoa! What's that install command doing anyhow? I'one thousand a large proponent of understanding commands that you paste into your final earlier you run them. The terminal is a actually cool and powerful program, but nosotros can harm our machine if we paste someone'due south malicious lawmaking in at that place, so it's of import to understand what we're doing.
I expect at this command and see a few notable things:
- /usr/bin/ruby-red -e
- curl -fsSL
- curl is getting content from this accost: https://raw.githubusercontent.com/Homebrew/install/master/install
- The output of the curl command is interpolated for the Ruby control
Let'southward explore what each of these mean
The/usr/bin/rubyportion of the command is giving our terminal a specific location to an executable. If y'all recall from our earlier step, when nosotros usedwhich ruby, nosotros found that the Ruby command was coming from that same location in our bin directory. On Unix-like operating systems such as macOS,/usr/bin/is where the operating organisation installs distribution executable files. Distribution executable files are executable files that come with and are managed past your operating system. Homebrew is using the accented location to reliably call the Reddish control on a car with unknown configuration.
I too notice that the Ruby command is getting chosen with the-due east flag. If we typeman rubyinto our final, we tin can explore the documentation and notice out what the-due east flag does.
If we ringlet downward the man page to go to the-e flag, we'll meet that it says, "Specifies script from command-line while telling Ruby non to search the residual of arguments for a script file name." To leave of the man page, just hitq.
To empathise what that documentation ways, you demand to understand that the typical usage of the Ruby command would be to pass an executable Ruby file to information technology. So if I had ahello_world.rbfile, I might blazon a command likecherry-red hello_world.rb, which would execute that file using Blood-red. The-ecommand lets the Red CLI know that y'all desire to treat any comes later the flag as the script to execute instead of looking for a file. We can test this out on our ain by running an inline Ruby plan!
ruddy -e 'puts "hello world"'
hello earth
The curl command
Alright. Now let'south look at the curl command. The scroll command is really cURL, which stands for "customer URL." The Homebrew install script uses gyre to fetch data from GitHub. The command is besides using several flags. Over again, nosotros can use thehomo command and laissez passer curl to it…which, while being slightly funny, is likewise super useful and tin can aid us find out what these flags are doing. And then here's the condensed version of what we need to notice inman ringlet.
- -fmeans fail silently on server errors. This is to foreclose seeing a text representation of an HTML server error document
- -smeans utilize silent mode. Silent manner doesn't show the progress meter or error messages.
- -Due south means show an error message if curl fails. Information technology's used to alter the-soption to however be silent unless the curl command fails.
- -Fiftyis the location flag. This is a convenience flag to handle redirect HTTP response codes. If the document has moved and in that location's a response indicating its new location, curl will go and try to fetch information technology at that location.
Cool. That was pretty straightforward. We're fetching data with some flags that modify the way curl outputs status messages.
Fetching raw content from GitHub
If y'all actually paste the URL https://raw.githubusercontent.com/Homebrew/install/chief/install into your browser, you'll see that information technology brings upwardly a Ruby script file. This is GitHub's site that hosts raw files that are stored in a GitHub repository. It's useful for fetching simply the content of a script.
Interpolation, a fancy word for fancy folks
Oxford defines interpolation as "the insertion of something of a different nature into something else." In our case, we're doingstring interpolation in Bash. We're taking the output of ane control and treating it like string input. In Fustigate, the best fashion to practise string interpolation is to call a command from within a set of parenthesis preceded by a dollar sign. So since we're wrapping the scroll command in the parenthesis, like this…
$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)
…we're treating the output similar a string to the Ruby command.
Putting it all together
Ironically, we're using Ruby to install the parcel manager, which we'll apply to install a newer version of Ruby!
- Nosotros're calling the Ruby control and telling it to execute the content we're passing to it instead of looking for a file.
- We're pulling a raw Cerise script from https://raw.githubusercontent.com/Homebrew/install/master/install. And nosotros're doing it without whatsoever extra output from curl past passing those control line options. This is smart because the Red command wouldn't know how to interpret any output from coil.
- Nosotros're interpolating that script every bit input to the Ruby control!
Wow. There's a lot to understand, even about relatively small commands! What I hope you take away from this section is that taking the time to understand some of the extra stuff is of import and that themancommand is your friend.
Now that we understand this stride amend, let's actually install the latest version of Ruby.
Step 3: Utilize the package manager to install Ruby
Alright, and then now that Homebrew is installed, nosotros can use information technology to install a newer version of Ruby. The command is pretty straightforward:
mash install scarlet
With this, Homebrew will go out and fetch non only Ruby but also any dependencies Reddish needs to run. On my auto, for example, Homebrew installed LibYAML prior to installing Ruby.
Nosotros're most set to write our first Crimson program!
Extra credit for step 3
Y'all'll observe right now that if y'all run thescarlet -vcommand again, it'southward the same version of Ruby that we saw in step 1. What gives?
Homebrew was squeamish enough to tell us what happened. Here is a snippet of the output from themash install ruby control:
cherry is keg-only, which means information technology was not symlinked into /usr/local,
considering macOS already provides this software and installing another version in parallel can cause all kinds of problem.
If you need to have ruby-red beginning in your PATH run:
repeat 'consign PATH="/usr/local/opt/cherry/bin:$PATH"' >> ~/.bash_profile
A brief look at how Homebrew installs programs
Think earlier when I mentioned that package managers like Homebrew keep our machine tidy by installing things in a consistent mode? This output relates to that bit of data. When Homebrew installs a package, it volition actually install it in a directory that Homebrew owns.
We mentioned that/usr/binis for distribution executable files. There'due south another directory,/usr/local/bin, that was created past Homebrew in step 2. Homebrew actually makes a directory named "Cellar" inside this directory. Homebrew will install packages into the Cellar then brand symlinks from those packages into/usr/local/binthat are available to us. A symlink, or symbolic link, is a special type of file that acts as a redirect to where the actual file lives. This allows Homebrew to be selective virtually what executables it makes available.
What does that mean for us?
It means that the newer version of Ruby is installed, but Homebrew isn't going to put it on the PATH for us. It's not going to practice this because of the existing version of Carmine living in/usr/bin.It tells us how to fix this situation past modifying our PATH.
I've mentioned the PATH a lot, and nosotros're going to talk about it next!
Step iv: Update our PATH
The simplest way to do this is to follow the instructions Homebrew gives us. Run this command:
echo 'export PATH="/usr/local/opt/ruby/bin:$PATH"' >> ~/.bash_profile
This volition update our Bash profile but not our electric current surroundings.
Y'all could open up a new terminal at this signal, but a simpler method to update your surroundings is to run this control:
source ~/.bash_profile
The version of Ruby that is going to take precedence in the last is now the version nosotros installed with Homebrew!
Verify that the new version is installed by once more checking the version. It should be a more recent version than what was installed on the arrangement past default.
ruby -v
Extra credit for step iv
There are some more interesting tidbits to unpack here.
- PATH
- repeat and>>
- ~/.bash_profile
- source
The PATH variable
PATH is an surround variable. An environment variable is a piece of information that is available for both us and other subprocesses to utilize every time we open a shell prompt. The PATH contains a listing of directories, separated by colons. When yous type a command into your shell, it will wait for executable files in those directories, in the guild they listed.
The PATH variable serves a couple of purposes. One purpose is providing a single place to refer to and update an executable's location. Also, the PATH makes our life easier by eliminating the need to refer to executables past their verbal location. For example, if we had not followed step 4, nosotros could still access the version of Ruby we installed, but it would exist slightly annoying. If nosotros wanted to bank check our newly installed Carmine version, we would accept to refer to the command similar this:
/usr/local/opt/carmine/bin/crimson -v
Now imagine typing that in everywhere! It'due south not great, and information technology could besides lead to a mess. If you decided to move that executable to another location, you'd have to type in that new location to reference the control. This would be a nightmare if scripts were using the absolute location. They would all break.
You tin update your PATH by appending or prepending a directory to it. That'south what this section of the control is doing. Information technology'south prepending a directory to the existing PATH variable.
consign PATH="/usr/local/opt/ruby/bin:$PATH"
Echo and the >> operator
The echo control just returns whatsoever we type in the shell back to us. For instance, you can run a control to print "Hello Earth" in the last:
repeat "Hello World"
Howdy World
The redirect operator used in conjunction with echo is a handy way to alter a file'due south contents without opening the file. The>>operator tells the shell to append the output of the previous command to a given file.
This is what we're doing. We're appendingexport PATH="/usr/local/opt/ruby/bin:$PATH" to the terminate of our Bash profile.
The Bash profile
The~/.bash_profile is a special file that'ssourced when a new shell environment is created. The tilde at the start of that file is a directory alias for your dwelling directory. For our purposes, you can think of this file equally a place where you store user preferences when y'all create a new beat out environment. This ways that any time you lot open up upward the final program, anything you place in this file volition run first. In this instance, the PATH environment variable will be updated to prioritize the directory containing our version of Ruby.
Source
Thesource command takes a file and updates the current shell's surroundings to comprise any changes or additions in the sourced file. While our shell would source the Bash profile automatically when a new terminal is created, we can exercise it manually by running thesource command ourselves.
Step 5: Write and run our get-go Crimson programme
Nosotros've now installed Reddish! Allow's celebrate by running the ceremonial and obligatory "Hullo Globe" program. We're going to use Vim, a unremarkably used editor for Red developers. Permit's open a file with Vim.
vim hello_world.rb
At present yous take to type the characteri to get into "insert" mode. And then go ahead and type the following:
puts "hullo world"
Now let'due south relieve and quit. Hit the Escape key, then blazon:wqand hit Enter.
Celebrate and type
cherry hello_world.rb
And that's information technology!
There'south no actress credit here. We've already covered quite a scrap of content. We set out to install Crimson, and along the mode, we learned a little bit almost several things:
- Concluding
- Package management
- Shell commands
- PATH
- How to get out Vim 🙂
- And more!
If you're planning on using Ruby to write a cool business app, take a wait at Retrace. Retrace tin can practice all sorts of things for you, including helping you understand your awarding's performance.
Try Stackify's free code profiler, Prefix, to write better code on your workstation. Prefix works with .NET, Java, PHP, Node.js, Cherry-red, and Python.
- About the Author
- Latest Posts
Source: https://stackify.com/install-ruby-on-your-mac-everything-you-need-to-get-going/
Posted by: guilloryfolut1991.blogspot.com
0 Response to "How To Clean Ruby From My Mac"
Post a Comment