A beautiful python and terminal setup for your beautiful M1 Macbook

Kewal Krishna
Dev Genius
Published in
7 min readJul 23, 2021

--

M1 Macbook
Photo by Martin Katler on Unsplash

Now that you have got yourself a beautiful and extremely fast M1 MacBook, let us ensure the setup is as visually pleasing as the system itself. This post would also help you work around the intricacies of setting up the dev environment on a relatviley new M1 chip set.

Like always, the first step is to install brew. Go to brew.sh and if you can donate as well. It is just so nifty and elegant. Do remember to put brew in your path. This is a step which some people tend to forget.

Once you have homebrew installed, go ahead and install pyenv . Pyenv is the best way to manage multiple versions of python and helps ease the development process. Real Python has a fantastic article on this topic which I would encourage you to go through and follow. Don’t forget to add pyenv in your startup script in ~/.zshrc. Yes, the newer operating systems have all zsh as the default shell. Yay! Below were the instructions I needed to add but check the output of your pyenv install command which should give you the exact details or just type pyenv initto find your relevant version specific details.

At this point, I would highly recommend to play around with pyenv and familiarise yourself with it. Once done, create a virtual environment and that should suffice for you to start your work. I do always have a confusion that if inside a local directory I can setup pyenv local then why the need for virtualenv. Yes I know the pros and cons but I always think and fascinate about this. An important point to note about virtual environemnt here is that while the real python article and the offical document says that it should be auto installed with pyenv I had to manually install it. The below snippet shows you the error I got followed by the installation step. It does look like there is an error here in pyenv and there is a ticket for the same.

This may take some time especially if your brew has not been updated for a while; yes we can directly install the package without the whole update but that never is a good idea. Incase you dowant to do that, HOMEBREW_NO_AUTO_UPDATE=1 brew install <formula> is the way to do so but that is generally not recommened as there are various security patches which you may miss out on.

Now that we have virtual environment installed, we can create and activate one.

Yes, I am trying to improve my Discrete Optimization skills :).

At this point I am quite happy as python is properly setup with the capability to handle multiple versions and virtual environments with ease and the terminal shows me the virtual environment name which I like to see in my prompt. So if you want, you can stop here. However, at one of my earlier orgs i really got down and spent a day customising the prompt with fancy themes, github branch identifiers, time, venv python versions and what not and will do that next. Let us continue. The first step is to install oh-my-zsh.

Oh My Zsh will not make you a 10x developer…but you may feel like one.”,

Installing is quite simple.

Now this does not use to happen earlier but now all my settings from ./zshrc were moved to a newly created file ./zshrc_pre_oh_my_zsh. And obviously, pyenvand other stuff were not working now. For a while I was getting confused what happened and then saw this file. I copied the content from here to ./zshrcat the top and it worked again. I was thinking that since the name of the file was like this, it would have all the command which will run before loading up oh-my-zsh but that was not the case and I had to copy and paste the contents to the /.zshrc file. You live, you learn. Also, before messing around with zsh, I prefer to install, the ever so diverse and amazing, PowerLine fonts.

Once done, verify the fonts are installed. Open terminal preferences and go to profiles and then font and select change font. You should see many powerline fonts installed. I like the Fira Code fonts which you can download from their Github. Be sure to follow the instructions there and enable it for your favourite editor which for me is still sublime-text. I have tried pycharmand vscode and while they are solid and extremely powerful, dare i say even better, Sublime holds a special place in my heart. That was the editor using which I wrote all the code for my startup, various hackathons I won and interviews I have cleared. And by the way, Sublime Text 4 is quite solid itself. #SublimeTillTheEnd

Enough with the fan-boy speak now. Once I set the fonts for sublime text I noticed a weird issues — the cursor size was too big for my liking. And a simple google search helped me fix it. We just have to edit the config for Sublime.

Let us move back to setting up the terminal. Once the fonts are installed, select a theme. I do not have a favourite theme. I have used powerline10kearlier but wanted to try something else and found spaceshipwhich looked really cool. The instructions are pretty clearly laid out in the readme file so feel free to give it a try. Now my terminal looks like below and yes, I moved on from Discrete Optimization to sharpen my skills on CI/CD and hence the terminal has CI in it. So many things to learn and so little time. Where is the TVA when you need them ?

Image of how the terminal looks with spaceship theme
Not bad! Can we improve this ?

Looks cool and conveys a lot of information. However I prefer to change this a little and it is quite easy and fun to do so. After tweaks, here is my prompt — mostly enabling right prompt for spaceship inside your ./zshrc file.

terminal with right prompt enabled showing time and virtual env
Now we are talking!

Once this is done, I install a few plugins like zsh-autosuggestions, zsh-completions, zsh-syntaxhighlight and git. I am not a power user and feel I can definitely learn more here but that would be a topic and task for another day. One small note though — when you do try to install zsh-completions you may get an error about permissions and the way to fix that is quite simple.

If you have some time do read through this wonderful SO post that explains this in detail.

Another gotcha is on zsh-autosuggestions. While the official documents does say that we can use brew install for this, it did not work for me and I had to manually clone the repo.

While the sequence of plugins did not matter to me, for some users it worked only after having autosuggestions at the end of the plugins in the ./zsrc file. Check out this Github thread for reference.

And finally everything is working. As you can see in the screenshot below, I just typed cd and zsh is suggesting me the directory. Nice!

Auto-suggestions working on terminal
Auto-suggest working its magic

From the official docs

If you press the → key (forward-char widget) or End (end-of-line widget) with the cursor at the end of the buffer, it will accept the suggestion, replacing the contents of the command line buffer with the suggestion. If you invoke the forward-word widget, it will partially accept the suggestion up to the point that the cursor moves to.

So this is how everything is looking now. You can see auto-suggestions working its magic very well.

Auto-suggestions in full flow
Terminal with everything set

Syntax highlighting via zsh-syntaxhighlightis another helpful feature. This would highlight if you have typed a wrong command before you even press enter. Awesome!

Error getting highlighted
Nicely showing the command is correct

Now, everywhere I read, I found that we need to specially source syntax highligthing at the end of the file(/.zshrc) but I did not need to do that and it worked for me. Just keep that in mind incase you are having some diffculties.

The last powerup is to install z. Rather than going cd ../../../workspace you can just do z workspace and voila you are inside that folder. Z keeps a track of all folders you have cd’ed into, in a file called ~/.z and after you have visited a folder once you can do z folder name to go directly into that. To know more do head over to their repo — they have a lot of options and configurations available.

And that is all. Do try out different themes and let me know what works best for you. There is one thing which I have not been able to do — showing the version of the python next to the virtual environment name in the right prompt.I tried many things and while the official docs does mention that if the global python version and virtual environment python version are different, they would show the python version along the environment name, I was not able to get this working. Can you help ? Pretty Please!

In case you want to have a look at my /.zshrc you can find that here.

--

--

Product Engineering Guy; ExFounder, Present Manager, Always an Enthusiast